A Simple Win32 GUI Introduction


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! :)

About the Author

Uni Student - Engineering

#include <windows.h> /* Windows header */

char szClassName[ ] = "WindowsApp";   /* Class ID */

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 (
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           CW_USEDEFAULT,       /* Default x... */
           CW_USEDEFAULT,       /* ...and default y position of window */
           640,                 /* The programs width... */
           480,                 /* ...and height in pixels */

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    while (GetMessage (&messages, NULL, 0, 0))

    /* Return: wParam from a quit message usually = 0 */
    return messages.wParam;

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 */       
            return DefWindowProc (hwnd, message, wParam, lParam);
    return 0;
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

nFunsterStil? The folks at Dev C++ use it too! Wonder what that means?

1o0oBhP 4 Posting Pro in Training

DevC++ Template gives you it!!! dunno what it does but i will find out...

Tight_Coder_Ex 17 Posting Whiz in Training

I am curious as to which compiler you used to create the executable. In know with VC++, not typecasting WindowProcedure will cause and error.

Secondly, why MS$ has done this I'll never know, but not adding 1 to the desired background color will yeild the wrong color. I must use HBRUSH (COLOR_BACKGROUND + 1) with VC++.

Maybe the headers you use have the background colors adjusted already.

1o0oBhP 4 Posting Pro in Training

DevC++ using mingw32 compiler. I think its the other way round as most MS users I have seen have got all sorts of auto-generated things to adjust the code! The brush issue should work as COLOR_BACKGROUND is independant of the compiler and is defined in the Win32 API reference, unless VC++ modifies it somewhere else. WindowProcedure is going to need a cast most of the time (it is c++ after all!) but this compiles ok for me. This is a modified DevC++ template sample. DevC++ can also handle not defining WindowProcedure and having the defininition / implementation all in one go provided it is before main (where it is first used). Some compilers I have seen REQUIRE you to define a function first, wierd no?

Dae 0 Newbie Poster

nFunsterStil is just a local variable name they used down at 'ShowWindow (hwnd, nFunsterStil);', meaning upon calling the fuction you could use that as an arguement spot... but the name itself doesnt mean anything apprently.

Good way for them to tell who is using their code though, just search google for that variable name they've come up with and put as a temple on their program.

bumsfeld 413 Nearly a Posting Virtuoso

I think FunsterStil is Danish for WindowStyle.

Be a part of the DaniWeb community

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