Topic : Win32 Intro tutorial
Author : BJZ
Page : 1

Win32 Intro tutorial

By: BJZ


We start by making a basic WinMain function. This will be the "body" of out program, without this we have no program.


#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    return 0;
}



We now have a Win32 program that executes and does nothing. Next step is to create a window procedure. A window procedure handles messages that windows throws out to it, its like out programs brain. Say we want to close our window, we click the "x" and windows sends a WM_QUIT message. Now if we have no window procedure our program will be like what the hell is going on here. So lets look at a basic procedure that closes our window.

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}



Ok now we have a procedure to handle messages, what next? We have to register the class for our window. I know your thinking "What the hell does that mean?" Well basically it our window properties, it tells the window which procedure to use, what icons to use, which instance to use and what we want out window to look like, and the class name for our window. So right inside out WinMain() function add the following.

    WNDCLASSEX wc;  
    ZeroMemory(&wc, sizeof(wc));
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = NULL;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = NULL;
    wc.cbWndExtra = NULL;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "BasicClass";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    RegisterClassEx(&wc);



The ZeroMemory function I added is not required but rather a habbit I have aquired. It basically cleans out "wc" incasse anything slipped in there. We are now ready to create our actual window that people see. We do this by using the CreateWindow function or CreateWindowEx if you want some extended functions. So right up there by WNDCLASSEX wc; add this.


    HWND hWnd;
    MSG Msg;



This will be the handle to our window and Msg will be used later. Now right under our window class settings make our window.


    hWnd = CreateWindow("BasicClass","BJZ Tutorial Window",
                          WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
                          640,480,NULL,NULL,hInstance,NULL);



Now our window code is done, but as you see if we run out program no window shows up. Why not? you ask, this is because we must tell windows to show our window. we do that with ShowWindow() and UpdateWindow() So add this to your code.

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


One last thing is needed for our window to work correctly..... A message loop, this is the heart out our program. Everything passes through it. It Gets and Sends messages to windows. So right under our window code add this.

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }



And replace return 0; with this.

    return Msg.wParam;


This concludes my Win32 introduction tutorial.

Page : 1