Jump to content

WINAPI Programming/Lesson One

From Wikiversity

Lesson One

[edit | edit source]

One of the most important things to learn about using the WINAPI to write windows applications is that to do so you have to fill a Windows class structure. The following code will show you how.

Window Procedure

[edit | edit source]
#include<windows.h> 

TCHAR* Simple = TEXT("A Simple Window...");

LRESULT CALLBACK WinProc(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;
}


WinMain Function

[edit | edit source]
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev,  LPSTR lpCmd, int nShow)
{
    WNDCLASS wc;
    HWND hwnd;
    MSG Msg;

    //The WNDCLASS struct wc is filled here by assigning all members of the struct
    wc.lpfnWndProc = WinProc; 
    wc.hInstance = hInst;
    wc.style = CS_BYTEALIGNCLIENT; 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = Simple; 
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.cbWndExtra = 0; wc.cbClsExtra = 0; 

    //end of struct assignments
 
    //Register our newly defined window class
    RegisterClass(&wc);

    //Create a window from wc class 
    hwnd = CreateWindow(Simple, Simple, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    CW_USEDEFAULT, CW_USEDEFAULT, 300, 210, HWND_DESKTOP, NULL, hInst, NULL);

    //Updating the window
    UpdateWindow(hwnd); 
 
    //The message loop or the place where messages are routed
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
         DispatchMessage(&Msg);
    }

return (int)Msg.wParam;<br>
}


Questions

[edit | edit source]

Leave all your questions here.

Bodene 04:29, 17 January 2007 (UTC)

Could you explain what a windows class structure is, what its for etc? Anonymous 07.38, 3 July 2007

WinApi Home