Code:
#include <windows.h>
#define IDBUTTON 102 //This is a standart defenition for a GUI control ID
//Used to link the button to handle the event of a click
#define IDQUITBUTTON 103 //quit button
//declaring windows peocedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
//HWND = GUI elements. represents the actual window/window element of a program
//UINT = unsigned int
//WPARAM/LPARAM = parameter messages that window uses to add data to a message
//class name will be a global variable
char szClassName [ ] = "windowWithButton"; //class name for the windows operating system
HINSTANCE g_hInst; //Program instance. defines what program a GUI belongs to. used throughout the program
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
//WinMain is recognised by the system, like main()
{
HWND hwnd; //window handle
MSG messages; //messages from windows (all inputs) stored here
WNDCLASSEX wincl; //data structure for the window class
/*Window Structure*/
g_hInst = hThisInstance; //Set the program instance to this instance
wincl.hInstance = hThisInstance; //Set the window class instance to this instance
wincl.lpszClassName = szClassName; //Set the according class name
wincl.lpfnWndProc = WindowProcedure; //function called by windows
wincl.style = CS_DBLCLKS; //Catch double-clicks
wincl.cbSize = sizeof (WNDCLASSEX); //the size of a parameter of WNDCLASSEX will be its own size
/*We will use the default icon and mouse cursor*/
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; //no menu
wincl.cbClsExtra = 0; //no extra bytes after the window class
wincl.cbWndExtra = 0; //Structure or the window instance
//Using windows default color as the background for the window
wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
//Register the window class. on fail quit
if(!RegisterClassEx (&wincl))
return 0;
//now that the class is registered, we can create the program
hwnd = CreateWindowEx (
0, //extended possibilities for variation - TEST
szClassName, //ClassName
"GUI v.0.2", //Title Text
WS_OVERLAPPEDWINDOW, //default window
CW_USEDEFAULT, //windows will decide the position
CW_USEDEFAULT, //where the window ends up on the screen
240, //width
140, //heigth
HWND_DESKTOP, //child-window to desktop
NULL, //no menu
hThisInstance, //program instance handler
NULL //no window creation data
);
//Make the window visivle
ShowWindow (hwnd, SW_SHOW);
UpdateWindow (hwnd);
//Message loop time!
while (GetMessage (&messages, NULL, 0, 0))
{
//Translate virtual key messages into character messages
TranslateMessage(&messages);
//send it to WindowProcedure
DispatchMessage(&messages);
}
//program return value is 0. PostQuitMessage
return messages.wParam;
}
//The Window Procedure is the way of sending/recieving windows messages declared as WM_
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hwndButton; //make a HWND for the button
HWND hwndQButton; //HWND for the Quit Button
switch(message) //handle all windws messages/inputs
{
case WM_COMMAND: //if a command has happened
{
if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)) //if it was a click
{
int iMID;
iMID = LOWORD(wParam);
switch(iMID)
{
case IDBUTTON: //if it was the button
{
MessageBox(hwnd, (LPCTSTR)"Hello World!", (LPCTSTR) "My Program!", MB_OK|MB_ICONEXCLAMATION);
break; //send a message
}
case IDQUITBUTTON: //if it was the quit button
{
PostQuitMessage (0);
break;
}
default:
break;
}
}
break;
}
case WM_DESTROY: //if it was a close off sign
{
PostQuitMessage (0); //send WM_QUIT to message queue, shut down
break;
}
case WM_CREATE: //here we make the buttons
{
hwndButton = CreateWindowEx(0, //more / extended
TEXT("BUTTON"), //create what?
TEXT("Push Me"), //Caption
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, //control styles seperated by |
10, //LEFT POSITION (from left)
10, //TOP POSITION (from top)
200, //WIDTH OF CONTROL
30, //HEIGHT OF CONTROL
hwnd, //parent windows handle
(HMENU)IDBUTTON, //control id for WM_COMMAND
g_hInst, //application instance
NULL);
hwndQButton = CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Quit"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
10,
50, //10 space between window top and button1, then 30 of button1, then 10 between the 2 buttons
200,
30,
hwnd,
(HMENU)IDQUITBUTTON,
g_hInst,
NULL);
break;
}
default: //messages we will not process
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}