Basic WinMain function

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hInstancePrev,
    LPSTR szCmdLine, int iCmdShow) {
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance; // set instance handle
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass( &wndclass)) {
        MessageBox(NULL, TEXT("This program requires Windows NT!"),
            szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,
                        TEXT("Hello Windows"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        400,
                        200,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    CenterWindow(hwnd);

    while (GetMessage( &msg, NULL, 0, 0) > 0) {
        TranslateMessage( &msg);
        DispatchMessage( &msg);
    }
    return msg.wParam;
}
		




Creating a window

hwnd = CreateWindow(
	szAppName,
	TEXT("Window Caption"),
	WS_OVERLAPPEDWINDOW,
	CW_USEDEFAULT, // initial X position
	CW_USEDEFAULT, // initial Y position
	400,        // Width
	200,        // Height
	NULL,       // parent window handle
	NULL,       // window menu handle
	hInstance,  // program instance handle
	NULL) ;     // creation parameters


ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
CenterWindow(hwnd);
		




Window Styles

  WS_BORDER
  WS_CAPTION
  WS_CHILDWINDOW
  WS_CLIPCHILDREN
  WS_CLIPSIBLINGS
  WS_DISABLED
  WS_DLGFRAME
  WS_GROUP
  WS_HSCROLL
  WS_ICONIC
  WS_MAXIMIZE
  WS_MAXIMIZEBOX
  WS_MINIMIZE
  WS_MINIMIZEBOX
  WS_OVERLAPPED
  WS_OVERLAPPEDWINDOW
  WS_POPUP
  WS_POPUPWINDOW
  WS_SIZEBOX
  WS_SYSMENU
  WS_TABSTOP
  WS_THICKFRAME
  WS_TILED
  WS_TILEDWINDOW
  WS_VISIBLE
  WS_VSCROLL
  




Common Window Messages

Message Description
WM_CREATESent when a window is being created. Good for initializing controls/resources.
WM_DESTROYSent when the window is being destroyed. Use PostQuitMessage(0) here.
WM_CLOSESent when the user tries to close the window (e.g. clicks the X).
WM_QUITPosted by PostQuitMessage() to terminate the message loop.
WM_COMMANDSent when a control or menu sends a notification. LOWORD(wParam) gives the ID.
WM_PAINTSent when the window needs to be redrawn. Handle all drawing here.
WM_SIZESent when the window is resized. lParam gives new width/height.
WM_MOVESent when the window is moved. lParam gives new x/y position.
WM_TIMERSent after SetTimer() interval elapses. Used for periodic updates.
WM_KEYDOWNSent when a key is pressed. wParam contains the virtual key code.
WM_KEYUPSent when a key is released.
WM_CHARSent when a character is typed. Takes into account keyboard state.
WM_MOUSEMOVESent when the mouse moves within the client area.
WM_LBUTTONDOWNSent when the left mouse button is pressed.
WM_LBUTTONUPSent when the left mouse button is released.
WM_RBUTTONDOWNSent when the right mouse button is pressed.
WM_MOUSEWHEELSent when the mouse wheel is scrolled.
WM_SETFOCUSSent when the window gains keyboard focus.
WM_KILLFOCUSSent when the window loses keyboard focus.
WM_ENABLESent when the window is enabled or disabled.
WM_ERASEBKGNDSent before WM_PAINT to erase background. Can be used to reduce flicker.
WM_SHOWWINDOWSent when the window is shown or hidden.
WM_ACTIVATESent when the window is activated or deactivated.
WM_HSCROLLSent when a horizontal scrollbar is used.
WM_VSCROLLSent when a vertical scrollbar is used.
WM_NOTIFYSent by controls to notify the parent of events (e.g. list views).
WM_INITDIALOGSent before a dialog box is displayed. Used for initialization.
WM_SETCURSORSent to set the cursor when it moves over the window.
WM_CONTEXTMENUSent when a user right-clicks or uses the context menu key.
WM_GETMINMAXINFOAllows customizing min/max window size during resizing.
WM_SYSKEYDOWNSent for system key presses (e.g., Alt key combinations).




Basic Window Procedure

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch (iMsg)
    {
    case WM_CREATE:
        break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
		
        // All painting occurs here, between BeginPaint and EndPaint.
		
        FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
        EndPaint(hwnd, &ps);
    }
        break;
    case WM_DESTROY :
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}