Bruce Shankle - Inside Out

Thinking for 39 years and counting...

Thursday, November 22, 2007

Minimum Windows Application

Today we are spoiled by tools like Visual Studio which come complete with wizards and project templates to help us kick-start projects. However, sometimes it's good to go back to the basics just so we don't forget how everything works under the hood.

Tonight I decided I just wanted a minimal windows application with a single window. I was surprised by how long this took me to recall. I suppose I need to crack open Petzold's book.

If you're interested in how I got the syntax coloring into my blog, check out the Visual Studio Add-Ins Every Developer Should Download Now.

Here is my minimum windows app:


//--------------------------------------------------------------------------------

//Minimal windows application

//by Bruce Shankle

#include <windows.h>

 

//--------------------------------------------------------------------------------

//Window procedure callback

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )

{

    switch (message)

    {

       case WM_DESTROY:

            PostQuitMessage(0);

            break;

 

        default:

            return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}

 

//--------------------------------------------------------------------------------

//Entry point

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

    //--------------------------------------------------------------------------------

    //Register a window class

    WNDCLASSEX wcex={0};

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.lpfnWndProc    = WndProc;   

    wcex.hInstance      = hInstance;

    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW);   

    wcex.lpszClassName  = L"MinWinApp";   

    if( !RegisterClassEx(&wcex))

        return -1;

 

    //--------------------------------------------------------------------------------

    // Create window (640 x 480 client area)

    RECT rc = { 0, 0, 640, 480 };

    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );

    HWND hWnd = CreateWindow( L"MinWinApp", L"Minimum Windows App", WS_OVERLAPPEDWINDOW,

        CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);

    if( !hWnd )

        return -2;

 

    //--------------------------------------------------------------------------------

    //Show the window

    ShowWindow( hWnd, nCmdShow );

 

    //--------------------------------------------------------------------------------

    //Process messages

    MSG msg = {0};

    while( GetMessage( &msg, NULL, 0, 0 ) )

    {

        TranslateMessage( &msg );

        DispatchMessage( &msg );

    }

 

    return 0;

}

 

 

1 Comments:

  • At 2:49 AM , OpenID blog said...

    If you want to get slightly more minimal, remove the unneccesary initialisation of msg.

     

Post a Comment

Subscribe to Post Comments [Atom]

<< Home