Question:
C++ newbie question please HELP!!!?
conan!
2009-12-20 21:03:43 UTC
ok, so im fairly new to writing programs... so far i can do simple things like math in an MS-DOS window (you know, those black windows with grey letters) but i want to move on to real windows applications. well, basically i just want to know how to create an actual Windows 'window', a GUI. how do you do this? can you post the code to make a gui window here? thanks! and i'll be sure to pick you as best answer =D
Five answers:
jacobandalex
2009-12-20 21:27:34 UTC
Going from console programming (what you did there) to windows programming is hard. The code jumps from a few dozen lines to sometimes over hundreds. To get a working program, start a non-empty, win32 project (not console) in Visual Studio, if you have it, which I suggest because it is free and powerful. This will provide a good starting project that will display a window. Here the shortest program that will display a plain white screen using the windows API, standard with windows:



#include

#include

#include



//application title

#define APPTITLE "Hello World"



//function prototypes (forward declarations)

BOOL InitInstance(HINSTANCE,int);

ATOM MyRegisterClass(HINSTANCE);

LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);





//the window event callback function

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_DESTROY:

PostQuitMessage(0);

break;

}

return DefWindowProc(hWnd, message, wParam, lParam);

}



//helper function to set up the window properties

ATOM MyRegisterClass(HINSTANCE hInstance)

{

//create the window class structure

WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);



//fill the struct with info

wc.style = CS_HREDRAW | CS_VREDRAW;

wc.lpfnWndProc = (WNDPROC)WinProc;

wc.cbClsExtra = 0;

wc.cbWndExtra = 0;

wc.hInstance = hInstance;

wc.hIcon = NULL;

wc.hCursor = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wc.lpszMenuName = NULL;

wc.lpszClassName = APPTITLE;

wc.hIconSm = NULL;



//set up the window with the class info

return RegisterClassEx(&wc);

}



//helper function to create the window and refresh it

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HWND hWnd;



//create a new window

hWnd = CreateWindow(

APPTITLE, //window class

APPTITLE, //title bar

WS_OVERLAPPEDWINDOW, //window style

CW_USEDEFAULT, //x position of window

CW_USEDEFAULT, //y position of window

500, //width of the window

400, //height of the window

NULL, //parent window

NULL, //menu

hInstance, //application instance

NULL); //window parameters



//was there an error creating the window?

if (!hWnd)

return FALSE;



//display the window

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);



return TRUE;

}





//entry point for a Windows program

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)

{

// declare variables

MSG msg;



// register the class

MyRegisterClass(hInstance);



// initialize application

if (!InitInstance (hInstance, nCmdShow))

return FALSE;



//set random number seed

srand(time(NULL));



// main message loop

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}



return msg.wParam;

}



Told you it was long. This was copied from Beginning Game Programming. Make sure that you are using a win32 project, not a console project. Also, use a multibyte charecter set, not Unibyte. It is a project setting. If you have any problems or questions, email me at kids.r@hotmail.com
2009-12-21 05:26:25 UTC
It's easier to create a GUI for a C# program than it is for a C++ program, but none the less, get a free copy of Visual C++ Express Edition, which is used for both.



You then can drag and drop the GUI elements (buttons etc (they are all objects)) onto a new window, then double click each element and enter your code for it.
valley89
2009-12-21 05:25:06 UTC
You should download Visual C++ 2008 (Express Edition - FREE)... go to http://www.microsoft.com/express/download/ and scroll to the bottom of the page and download Visual C++...



Once you download it, you'll be able to create windows forms, buttons, menus, and much more... everything you need for GUIs. This will be especially useful for you since you're a beginner and with Visual C++ you can simply drag-and-drop controls onto the page without doing much coding. (Then later on, after seeing the generated code, you'll be able to write the code yourself if you wanted to. But for now, I strongly recommend downloading Visual C++ (on link above) and you'll be able to create GUIs really quickly.
F0
2009-12-21 06:15:34 UTC
You should first learn File I/O, memory management and system calls (API) of the platform you want to develop for; learning how to interact with the kernel at lower level is more important than creating GUIs imo.



Then when you feel like you are ready to create GUIs, find a tutorial or book on some widget library that runs on your platform. GTK+ and QT are cross-platform, so you might want to learn those.

http://en.wikipedia.org/wiki/GTK%2B

http://en.wikipedia.org/wiki/Qt_%28toolkit%29

If you are using Microsoft Windows then GUIs are a part of the API

http://en.wikipedia.org/wiki/Windows_API
nighthwk3
2009-12-21 05:12:43 UTC
You probably want to start with Visual C++.

Try http://msdn.microsoft.com/en-us/visualc/ee340952.aspx



Though you might want to think about working in Linux (or at least use gcc and other open source stuff in Windows). It's cheaper (free) and there's a lot of open source stuff out there to learn from.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...