Tuesday, February 23, 2010

Smallest C++ windowed application

Interesting to see this is the smallest windowed Windows application:

#include <windows.h> 

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow)
{
    MSG msg;

    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, 
    WndProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), 
    NULL, L"DX9_TUTORIAL1_CLASS", NULL}; 

    RegisterClassEx(&wc);

    HWND hMainWnd = CreateWindow(L"DX9_TUTORIAL1_CLASS", 
    L"DirectX 9 Bare Bones Tutorial 1", 
    WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, 
    NULL, NULL, hInstance, NULL);
    ShowWindow(hMainWnd, nShow);
    UpdateWindow(hMainWnd);

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

    return(0);
} 

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
        PostQuitMessage(0);
        return(0);
    }

    return(DefWindowProc(hwnd, msg, wParam, lParam));
}

0 comments:

Post a Comment