An introduction to the Win32 GUI. The code creates a simple window that you can play with. Many parameters can be changed if you like! The unusual thing about windows apps is that rather than directly handling inputs (ie the position of the mouse / what control has focus ect...) you process window messages. Whenever something happens, it sends a message to the window making a message queue. DispatchMessage() calls WindowProcedure (which if you look is assigned to be the default message handler for the class name ("WindowsApp") of which the window belongs. The good thing is that as all messages are of the same structure it is easy to get information about what happened (lParam/wParam) and take the correct action (switch statement). Enjoy! :)
A Simple Win32 GUI Introduction
#include <windows.h> /* Windows header */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp"; /* Class ID */
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* See end of code */
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Default windows background colour */
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* Default x... */
CW_USEDEFAULT, /* ...and default y position of window */
640, /* The programs width... */
480, /* ...and height in pixels */
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
/* Return: wParam from a quit message usually = 0 */
return messages.wParam;
}
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ /* Called when window processes a message */
switch (message)
{
case WM_DESTROY: /* Destoy message: called if you press the "x" in the top right corner */
PostQuitMessage (0); /* Send a message to quit */
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
1o0oBhP 4 Posting Pro in Training
Tight_Coder_Ex 17 Posting Whiz in Training
1o0oBhP 4 Posting Pro in Training
Dae 0 Newbie Poster
bumsfeld 413 Nearly a Posting Virtuoso
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.