Thread: How do I add a window to my program?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    How do I add a window to my program?

    Thank you for all help.
    Code:
    /*
    #pragma once
    
    
    #include "stdafx.h"
    #include "targetver.h"
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <process.h>
    #include <time.h>
    #include <conio.h>
    #include <sstream>
    #include <vector>
    using namespace std;
    */
    #include "stdafx.h"
    
    
    #define DEBUG
    
    
    // Answer class
    class CAnswers {
    public:
        vector<string> answer;
        CAnswers(void);
        CAnswers(string ans);
        ~CAnswers(void);
        int size(void);
    } Answers;
    CAnswers::CAnswers(void) {
    }
    CAnswers::CAnswers(string ans) {
        stringstream ss(ans);
        string temp_answer;
        while (getline(ss, temp_answer, ','))
            answer.push_back(temp_answer);
    }
    CAnswers::~CAnswers(void){
    }
    int CAnswers::size(void) {
        return answer.size();
    }
    
    
    // Question class
    class CQuestion {
        string _question;
    
    
    public:
        CAnswers* answers;
        CQuestion(void);
        CQuestion(string q_a);
        ~CQuestion(void);
        string question(void);
    } Question;
    CQuestion::CQuestion(void) {
    }
    CQuestion::CQuestion(string q_a) {
        string ans;
    
    
        stringstream ss(q_a);
        getline(ss, _question, ':');
        ss >> ans;
        answers = new CAnswers(ans);
    }
    string CQuestion::question(void) {
        return _question;
    }
    CQuestion::~CQuestion(void){
        delete answers;
    }
    
    
    //Editor class
    class CEditor {
        vector<CQuestion> questions;
        ofstream myFile;
        int age, address;
        char selection, character[1];
        string name, response;
        HANDLE np;
        unsigned notepadID;
    
    
    public:
        CQuestion* q;
        CEditor(void);
        ~CEditor(void);
        int menu(void);
        void question(void);
        void reload(void);
        int time(void);
        static unsigned int __stdcall notepadThread(void* arg);
        void notepad(void);
    } Editor;
    CEditor::~CEditor(void){
        delete q;
    }
    CEditor::CEditor(void) {
        np = 0;
    }
    unsigned int __stdcall CEditor::notepadThread(void* arg) {
        system("notepad.exe AI.txt");
        _endthreadex(0);
        return 0;
    }
    void CEditor::notepad(void) {
        _beginthreadex(NULL, 0, &CEditor::notepadThread, np, 0, &notepadID);
    }
    int CEditor::menu(void) {
        system("CLS");
        cout << "Menu:\n9 = Basic Information.\n8 = DATALOG\n7 = Reload \n6 = Time\n0 = Leave\n";
        cin >> selection;
    
    
        switch (selection)
        {
            // Open text in notepad
        case '8': {
            notepad();
            break;
        }
            // Exit program
        case '0': {
            cout << "- Please close Notepad to exit - ";
            return 0;
        }
            // Ask questions and place answers into AI.txt
        case '9': {
            question();
            break;
        }
            // Simulate reloading program,
        case '7': {
            reload();
            break;
        }
            // Clock
        case '6': {
            do {
                time();
                cout << "Press Enter to exit";
                Sleep(750);
            } while (_getch() != '\n');
            break;
        }
            // Invalid response
        default: {
            cout << "Nope" << endl;
            break;
        }
        }
        return 1;
    }
    void CEditor::question(void) {
        // Open file and move to end of file.
        myFile.open("AI.txt", ios_base::app);
        myFile << "\n----- new entry -----\n";
        // Clear console and ask questions.
        system("CLS");
        cout << "AI: What is your name?" << endl;
        cin >> name;
        system("CLS");
        myFile << "Datalog" << "\nName: " << name << endl;
    
    
        cout << "AI: How are you feeling?" << endl << name << ": ";
        cin >> response;
        system("CLS");
        if (response == character) {
            cout << "Invalid answer";
        }
        myFile << name << " is feeling " << response << endl;
        cout << "AI: What is your age " << name << "?" << endl << name << ": ";
        cin >> age;
        system("CLS");
        myFile << name << "'s age is " << age;
        myFile.close();
        if (!myFile)
        {
            cout << "Error";
        }
    }
    void CEditor::reload(void) {
        system("CLS");
        cout << "Reloading";
        Sleep(1000);
        cout << ".";
        Sleep(1000);
        cout << ".";
        Sleep(1000);
        cout << ".";
    }
    int CEditor::time(void) {
        struct tm newtime;
        char am_pm[] = "AM";
        __time64_t long_time;
        char timebuf[26];
        errno_t err;
    
    
        // Clear the screen
        system("CLS");
        // Get time as 64-bit integer.
        _time64(&long_time);
        // Convert to local time.
        err = _localtime64_s(&newtime, &long_time);
        if (err)
        {
            printf("Invalid argument to _localtime64_s.");
            exit(1);
        }
        if (newtime.tm_hour > 12)        // Set up extension.
            strcpy_s(am_pm, sizeof(am_pm), "PM");
        if (newtime.tm_hour > 12)        // Convert from 24-hour
            newtime.tm_hour -= 12;    // to 12-hour clock.
        if (newtime.tm_hour == 0)        // Set hour to 12 if midnight.
            newtime.tm_hour = 12;
    
    
        // Convert to an ASCII representation.
        err = asctime_s(timebuf, 26, &newtime);
        if (err)
        {
            printf("Invalid argument to asctime_s.");
            exit(1);
        }
        printf("%.19s %s\n", timebuf, am_pm);
        return 1;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        CEditor editor;
        editor.q = new CQuestion("How are you?:Good,Bad");
        cout << editor.q->question() << endl;
        for (int i = 0; i<editor.q->answers->size(); ++i) {
            cout << editor.q->answers->answer[i] << endl;
        }
        while (editor.menu());
        return 0;
    }
    Last edited by cppprogrammer; 09-18-2014 at 05:57 PM.

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Do you mean like a graphical window? You can do it with windows.h, if everything is linked correctly. However the real work would be in reproducing the behavior of your program in the graphical window. You will have to go through and integrate everything into the window procedure.

    theForger's Win32 API Tutorial

    Windows API Tutorial

    There are other libraries you can use for a graphical window as well, however since you're already using windows.h I thought it would be the quickest way. It might be a bit annoying in some aspects, as it's in C (The relisoft tutorial is made for C++ however).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Seriously don't use Win32 API. It's made for C and it's difficult and complex. You should be getting a GUI framework for C++, such as Qt. There are other frameworks out there, too, of course since there is no standard way of adding GUI to C++. Yet, anyway. Google is your friend.
    I would also recommend you getting a good C++11 book because your design leaves much to be desired.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How do I add a window to my program?
    Your program?

    If you learn the material and write your own code, you should have no problem adding the things you need.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    there is no standard way of adding GUI to C++. Yet, anyway.
    I could be wrong, but I have doubts that C++ will ever get standard GUI support. To me, it doesn't seem like it would be in the spirit of C++.

    I also recommend Qt. I use it for all my C++ GUI applications these days.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    I could be wrong, but I have doubts that C++ will ever get standard GUI support. To me, it doesn't seem like it would be in the spirit of C++.
    Well...
    Stroustrup: FAQ
    Never say never.

    Anyway, I really don't like Qt (reinventing the wheel with all its classes, doesn't follow standard UI guidelines [e.g. progress boxes jump instead of smoothly transitioning to a new value], and more), but I really don't know any better GUI framework with WYSIWYG UI design, so... yeah, that's what I use too, currently.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Well...
    Stroustrup: FAQ
    Never say never.
    I suppose if boost did something, it might have a chance to end up in the standard.

    Quote Originally Posted by Elysia View Post
    Anyway, I really don't like Qt (reinventing the wheel with all its classes, doesn't follow standard UI guidelines [e.g. progress boxes jump instead of smoothly transitioning to a new value], and more), but I really don't know any better GUI framework with WYSIWYG UI design, so... yeah, that's what I use too, currently.
    There's nothing stopping you from submitting your own patches to improve matters. Also, I'm curious to know what it is about Qt that you consider "reinventing the wheel." Other GUI toolkits also have their own way of doing things, so I'm just wondering why you say that about Qt, specifically.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    Quote Originally Posted by Alpo View Post
    Do you mean like a graphical window? You can do it with windows.h, if everything is linked correctly. However the real work would be in reproducing the behavior of your program in the graphical window. You will have to go through and integrate everything into the window procedure.


    theForger's Win32 API Tutorial


    Windows API Tutorial


    There are other libraries you can use for a graphical window as well, however since you're already using windows.h I thought it would be the quickest way. It might be a bit annoying in some aspects, as it's in C (The relisoft tutorial is made for C++ however).
    Quote Originally Posted by Elysia View Post
    Seriously don't use Win32 API. It's made for C and it's difficult and complex. You should be getting a GUI framework for C++, such as Qt. There are other frameworks out there, too, of course since there is no standard way of adding GUI to C++. Yet, anyway. Google is your friend.
    I would also recommend you getting a good C++11 book because your design leaves much to be desired.
    Quote Originally Posted by Matticus View Post
    Your program?


    If you learn the material and write your own code, you should have no problem adding the things you need.
    Quote Originally Posted by Elkvis View Post
    I could be wrong, but I have doubts that C++ will ever get standard GUI support. To me, it doesn't seem like it would be in the spirit of C++.


    I also recommend Qt. I use it for all my C++ GUI applications these days.
    I have posted this topic elsewhere, how in the world you found it is unbelievable lol. I am thinking about Qt and also coding it the API way. Thank you guys for the links.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by cppprogrammer View Post
    how in the world you found it is unbelievable
    Unbelievable? This forum is a fairly high-traffic site. It has dozens of active users that visit daily, so it should not be surprising that people responded to your question.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    There's nothing stopping you from submitting your own patches to improve matters. Also, I'm curious to know what it is about Qt that you consider "reinventing the wheel." Other GUI toolkits also have their own way of doing things, so I'm just wondering why you say that about Qt, specifically.
    I don't want to dig too much into Qt. Also, Qt is really the only framework I've had experience with so far (save for MFC in the past). It's just the only one I've found and know of so far that meets my requirements, but I'm always open for suggestions...
    When I say "reinventing the wheel," I mean all the stuff Qt has like strings, etc, that also the standard has. What's even more irritating is there's no conversion from standard containers to Qt ones. Then there's the whole signal and slot mechanism... if your slot if on another thread, you have to kind of defer the call so the GUI thread can call the slot. The problem? You can't pass standard containers to such slots. Qt will just fail quietly without any sort of warning or error. I found out the hard way by debugging the source code.
    So yeah, it's a bit annoying, and the whole framework is pretty old so they don't take advantage of newer stuff like smart pointers, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    When I say "reinventing the wheel," I mean all the stuff Qt has like strings, etc, that also the standard has.
    It also predates the first standard (1991), so it's likely that C++ implementations simply didn't have certain features when Qt first appeared.

    Quote Originally Posted by Elysia View Post
    What's even more irritating is there's no conversion from standard containers to Qt ones.
    Qt containers typically have the standard begin/end iterator mechanism, allowing their contents to be copied to/from standard container. This likely goes back to my first point here, where standard containers simply didn't exist in early implementations of C++.

    Quote Originally Posted by Elysia View Post
    Then there's the whole signal and slot mechanism... if your slot if on another thread, you have to kind of defer the call so the GUI thread can call the slot.
    Many GUI frameworks do that, including Windows Forms and WPF in the .Net framework. GUI programming seems to work best if a single thread has exclusive access to GUI objects.

    Quote Originally Posted by Elysia View Post
    The problem? You can't pass standard containers to such slots. Qt will just fail quietly without any sort of warning or error. I found out the hard way by debugging the source code.
    I've never run into this specific problem, but it likely stems from point #1 again.

    Quote Originally Posted by Elysia View Post
    So yeah, it's a bit annoying, and the whole framework is pretty old so they don't take advantage of newer stuff like smart pointers, etc.
    I don't know what version(s) you've used, but the framework is still under active development. They released version 5.3 a few months ago, and 5.4 is due in November. They have their own implementation of shared pointers (QSharedPointer), and many other things, and have had them since before such things became standard. The QSharedPointer class, for example, first appeared in 2009, with version 4.5.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    I've never run into this specific problem, but it likely stems from point #1 again.

    I don't know what version(s) you've used, but the framework is still under active development. They released version 5.3 a few months ago, and 5.4 is due in November. They have their own implementation of shared pointers (QSharedPointer), and many other things, and have had them since before such things became standard. The QSharedPointer class, for example, first appeared in 2009, with version 4.5.
    This is precisely the problem. Now that the standard includes these, it should be natural to use the standard containers instead of the Qt ones. But it doesn't work. There is no conversion from standard objects to Qt objects. I can understand that a lot of the Qt framework uses its own components due to its age (legacy code and all that stuff), but at the very least, there should be interop so that programmers can use standard components instead of Qt components when interacting with the framework. Sure, it predates the standard. So what? Time to modernize!

    I use 5.3, btw.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    there should be interop so that programmers can use standard components instead of Qt components when interacting with the framework.
    I can't disagree with you there, but I actually like the QString class a lot more than std::string. I'd like std::string more if I could derive classes from it. Making its member functions, and particularly its destructor, non-virtual was, in my opinion, a bad design choice.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to derive from a string class?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    Why do you want to derive from a string class?
    Obviously, it would be to add additional string-oriented functionality. Whitespace trimming, toupper, and tolower are just a few of the many things I'd like to have a string class do, without the aid of external functions.

    I've even gone so far as to implement my own wrapper, that duplicates the interface of std::string, and uses it for storage, but adds the additional features I wanted. Had I been able to safely derive from std::string, I could have saved a great deal of time and effort.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to close its own window
    By wizard21212 in forum C Programming
    Replies: 5
    Last Post: 04-20-2011, 11:57 PM
  2. Window Program
    By illidari in forum C++ Programming
    Replies: 6
    Last Post: 03-11-2011, 04:48 PM
  3. Create a Program with no window
    By brconner in forum C++ Programming
    Replies: 8
    Last Post: 05-27-2007, 01:26 PM
  4. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  5. making a window program
    By DDX in forum C++ Programming
    Replies: 30
    Last Post: 03-21-2005, 04:12 PM