跳到主要內容

win32 sample code

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <tchar.h>


LRESULT CALLBACK MainWindowProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    // register windows class
    WNDCLASSEX wndclassex = {NULL};
    wndclassex.cbSize = sizeof(WNDCLASSEX);
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;
    wndclassex.lpfnWndProc = MainWindowProc;
    //wndclassex.cbClsExtra = 0;
    //wndclassex.cbWndExtra = 0;
    wndclassex.hInstance = hInstance;
    wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclassex.hCursor = LoadCursor(NULL,IDC_ARROW);
    wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    //wndclassex.lpszMenuName = NULL;
    wndclassex.lpszClassName = (LPCTSTR)(_T("TestMainWindow"));
    wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&wndclassex);


    // create windows
    HWND hwnd = CreateWindowEx(NULL,
                               (LPCTSTR)(_T("TestMainWindow")),
                               (LPCTSTR)(_T("My Test Window")),
                               WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               NULL,
                               NULL,
                               hInstance,
                               NULL);

    //ShowWindow(hwnd, SW_SHOWNORMAL);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);


    // message loop
    MSG msg = {NULL};
    int iResult = 0;
    while ((iResult = GetMessage(&msg, NULL, 0, 0)) != 0) {
        if (iResult == -1) {
            break;
        } else {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (uMsg) {
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
           
            EndPaint(hwnd, &ps);
            break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
           
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
           
        default :
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

留言