Hungry Mind , Blog about everything in IT - C#, Java, C++, .NET, Windows, WinAPI, ...

WinAPI application skeleton source

Недавно потребовалось быстренько написать окошку на WinApi, писать было лень - решил поискать заготовочку в сети. Не нашел, написал свою.
#include "stdafx.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static TCHAR lpszClassName[] = _T("MyWindowClass");

int WINAPI _tWinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPTSTR lpszArgs, int nWinMode);

int __cdecl _tmain(int argc, TCHAR **argv) {
    FreeConsole();
    return(_tWinMain(NULL, NULL, NULL, SW_SHOWNORMAL));
}

int WINAPI _tWinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPTSTR lpszArgs, int nWinMode) {
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wcl;
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst;
    wcl.lpszClassName = lpszClassName;
    wcl.lpfnWndProc = WndProc;
    wcl.style = CS_DBLCLKS /* | CS_HREDRAW | CS_VREDRAW*/;
    wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcl.hIconSm = NULL;
    wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcl.lpszMenuName = NULL;
    wcl.cbClsExtra = 0;
    wcl.cbWndExtra = 0;
    wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    if (!RegisterClassEx(&wcl)) {
        DWORD w32Err = GetLastError();
        DebugBreak();
        return 0;
    }
    hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, lpszClassName, _T("My Window Name"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hThisInst, NULL);
    if (!hwnd) {
        DWORD w32Err = GetLastError();
        DebugBreak();
        return 0;
    }
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);

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

    return (static_cast<int>(msg.wParam));
}

void Handle_WmPaint(HWND hWnd) {
    PAINTSTRUCT ps;
    HDC hDC = BeginPaint(hWnd, &ps);
    // Paint your invalid rect here
    EndPaint(hWnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_PAINT:
            Handle_WmPaint(hWnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

2 коммент.:

Анонимный комментирует...

Amazing issues here. I'm very happy to see your post. Thank you a lot and I'm looking ahead to contact you.

Will you kindly drop me a mail?
Feel free to visit my webpage ; GFI Norte

Анонимный комментирует...

Superb, what a blog it is! This website provides useful data to
us, keep it up.

Here is my web page; iphone 5 launch date

Отправить комментарий

Copyright 2007-2011 Chabster