Hey guys. Im writing a console based application which i want to convert to a windowed application. however, i still want the user to be able to type in input and for my program to be able to output in a console based style, i'd just like the option to have buttons which do other functions too. is this easy to do? Im using dev c++, can i switch to a visual developer?

Recommended Answers

All 14 Replies

First off I would switch from Dev-C++ because it is a dead IDE. Use Code::Blocks or Visual C++ that is more current (I use Code::Blocks and had switched from Dev-C++ -- the transition wasn't that hard).

As for the whole having a window and cmd prompt up at the same time you just have to make a Console application type project and copy all the windowing code over (or just go Project >> options >> build targets and select Type: Console application for Code::Blocks).

If you do not know any WinAPI and would like to make windows I would recommend using Visual C++ (I think they have a GUI editor) or find an IDE that can make windows.

Ok i have visual c++ now, and my window. i know no WinAPI so the code looks like nonesense to me. is there anyway i can simulate a console application within my window?

The code which the Ide generates at first is the code required for building a simple window which will act as a canvas for your other controls like buttons and scroll lists.You should learn how to create an Edit control first.It is a window which can take user input like those in which you type cd-keys:D.Almost every control in a windows application is a window itself so you should read about the CreateWindow function in case you'll go for the Win32Api , and a bit about window procedures.You'll catch up with the rest as you go through all that.

using vc++ 2010 there are a couple options
1. Create a win32, Win32 Project. That will generate a shell of a program in which you will have to code all the window stuff using pure win32 api graphics functions. Here is a tutorial to get you started with that. Be forwarned that you will have to write a lot of code just to do the simplest things.

2. Create a CLR Windows Forms Application. This is mostly visual drag-and-drop to design the visual forms the way you want them. Then to make a control such as a button do something all you have to do is double click the button and add the code you want in the function that the IDE will provide for you. This is called managed c++ and is a little different than standard c++. Here is a list of several tutorials.

It's not managed C++ anymore, it's C++/CLI--there is a difference.

managed C++ had the horrible syntax, C++/CLI is actually kind of decent.

commented: good point :) +36

Thanks for the clarification -- just goes to show how little I really know about it :)

ok maybe im not making myself clear. is it possible to nest my console application within a window easily? can i just copy my code and shove it in there somewhere amongst all that create window API stuff that i dont understand? I use lots of console functions, cin, cout ect.

My main function is already winmain, here is my declaration

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
            char * cmdParam, int cmdShow)

{
SetConsoleTitle( "Program Version 1" );
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),FOREGROUND_GREEN);

I dont mind learning the API but id rather learn it using my current project, rather than spending loads of time writing complex code that im not going to use.

anyone know how i can do this? ive been looking for solutions everywhere. can i start the window as a child process and have the console send its output to the windowed application?

You can make it as one process, just create a console application then put the window code into it and run it. You will see a the console window and a window pop up. All cin/cout usage will go to the console window and if you have your program/inputs set up properly you are going to be able to use that input to manipulate your window.

Yes, I agree with Sfuo -- you can add MS-Windows gui to a console application. Here's how to do it from the console program. What I did was create a win32 windows application then copied the *.cpp, *.h, *.rc and other resource files into the console project folder, then add all those files to the console project. I even left tWinMain() in tact, just as the VC++ 2010 Express IDE generated it. I changed nothing in the GUI program. Then I put the GUI startup into its own thread so that the GUI would run at the same time as the console program.

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{

    HMODULE hInstance = GetModuleHandle( (char *)lpParameter);
    _tWinMain(hInstance,NULL,(char *)lpParameter,SW_SHOW);
    return 0;
}

int main(int argc, char* argv[])
{
    DWORD dwCounter = 0;
    DWORD dwThreadID = 0;
    HANDLE hThread =  CreateThread(NULL,0,ThreadProc,argv[0],0,&dwThreadID);
    while(true)
    {
        // your actual console program may not have to call WaitForSingleObject()
        // I just put it here so that I could easily see the console displaying
        // something while the gui is running.
        DWORD dwReturn = WaitForSingleObject(hThread,2000); // 2 second wait
        if( dwReturn == WAIT_OBJECT_0 )
            break;
        cout << "counter = " << dwCounter << '\n';
        ++dwCounter;
    }
    cout << "All done folks\n";
    cin.get();
    return 0;
}

Edited: Forgot to log in before writing out this post.. so now it looks messed up as hell so I dont wanna rewrite it but I hope it helps!


mhmm Open visual studio, press ctrl shift N or else just press file -> New -> Project
Create a "WINDOWS FORM APPLICATION"

A windows will show up.. U will now see "Form1.h [Design]"

If u double click on it, you can see the code for the form..
If u want to add a button, point the mouse at the toolbar on the left side (vs2008) right side (vs2010) {default positions}

When the toolbar expands, drag and drop a button to the form1.h design.. and place it in the window..

Now u have a button, double click the button to make form the code.. and now u can add code to the button..

Add a textbox the same way and double click it.. the code is now formed and u can make it so that when the person presses the button, output everything into the textbox.. or u can use a messagebox to show commands..

#pragma endregion is usually where the codes are added.. such as buttons, textboxes, comboboxes.. etc..

Example:

#pragma endregion

      private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) 
      }
      private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
      int c3; //declare c3 as an integer.
      Int32::TryParse(textBox3->Text,c3); //take textbox3 text and check if its an integer/
      int c5; //declare c5 as an integer.

      Int32::TryParse(textBox5->Text,c5); //check if textbox5 text is an int and pass it to c5. example: textbox5 text is "16" then c5 = 16.. if textbox 5 text is "fafnajsns" then it fails to pass it as c5 since c5 is of type int only.

      int XPNeeded = ((c5)-(c3)); //declare that XPNeeded is of type int. and is equal to c5 - c3.. provided that c5 and c3 are integers.. (reason for the tryParse above).

      this->textBox7->Text = XPNeeded.ToString(); //If previous statement worked and XPNeeded is an int as expected, string it and place the string/value in textBox7.
      }

      int crntlvl;
      Int32::TryParse(textBox4->Text,crntlvl);

      if (crntlvl > 99){
      MessageBox::Show("Current Level cannot be Greater than 99!"); //Instead of using a textbox, I've used a messagebox.. Kinda like a popup with an alert on it telling u the message in quotations.

      int crntlvl = (1);
      textBox4->Text = crntlvl.ToString();
      }
      else if(crntlvl < 1){
      MessageBox::Show("Current Level cannot be Less than 99!");
      crntlvl = (1);
      textBox4->Text = crntlvl.ToString();
      }
      //Rest of code is basically the same as before except for the messagebox.

      //U can also do something like this:
      int targlvl;
      int targxp;
      int crntlvl;
      Int32::TryParse(textBox6->Text,targlvl);
      Int32::TryParse(textBox5->Text,targxp);
      Int32::TryParse(textBox4->Text,crntlvl);
       
      if(targlvl == 0){
      textBox10->Text = "Error! Target Level cannot be Less than 1!";
      textBox10->Clear(); //Notice the TextBox clear.. It removes all texts from the textbox if targlvl is equal to 0.. so if the user enters "0" into "textbox6", clear textbox10.
      }

      //The following line of code is click the form1.h [design], press a textbox and press properties.. in the properties box under the solution explorer, press the lightning bolt sign.. then find "KeyPress". This means when the person presses a key, it triggers an event such as the following:

      private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
      // Accept only digits and the Backspace character
      if(!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
      e->Handled = true;
      }
      //That means if the user tries to enter a letter into textbox1, it will not show up as the program will ignore it.. it accepts only digits. if the person presses anything other than a number, it wont show. If the backspace key is pressed though, itll backspace 1 character (a digit obviously).

      Of course you can do this with a masked textbox.. but u will see the difference yourself between my way vs a masked textbox.. a masked textbox will have a spaceholder no matter what.. meaning if there is nothing in the textbox, itll be filled with underscores or # signs or anything by default..

Thanks dragon, ive done what you said, all the files are added to my project, but im getting a compiler error. sdkddkver.h - no such file or directory.

my console project is a dev c++ file as that's where i wrote my console program so i dont know if the error is related to that. do you think it would be possible to post some compilable code so i can see it working?

I'd replace that old compiler with a newer one, such as Code::Blocks or VC++ 2010 Express. Dev-C++ has not been supported or updated for many years now, and the distribution contains an old obsolete version of MinGW compiler.

Aside from that, the error you got was probably because that header file was not in the same directory as the rest of the source code files.

Attached is the project I created using VC++ 2010 Express. I deleted all the compiler-generated files to make the *.zip file smaller

commented: very informative and helpful +2

cheers dragon thats exactly what im looking for, the only reason ive ever had to sit down and learn the winAPI.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.