Warning: file_get_contents(https://whois.pconline.com.cn/jsLabel.jsp?ip=127.0.0.1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable in D:\wwwroot\huidong\wwwroot\function.inc.php on line 884
快速创建窗口,省去创建窗口类,注册窗口的麻烦 - huidong

huidong

首页 | 会员登录 | 关于争取 2022 寒假做出汇东网 Ver3.0.0 !
搜索文章


#include <Windows.h>
#include <tchar.h>


LRESULT __stdcall WindowProcedure(HWND window, unsigned int msg, WPARAM wp, LPARAM lp)
{
    // 简单的绘图示例中的变量
    HDC dc;
    LPCTSTR str = L"hello world";

    switch (msg)
    {
    case WM_PAINT:

        // 简单的绘图示例
        dc = GetDC(window);
        TextOut(dc, 50, 50, str, lstrlen(str));
        ReleaseDC(window, dc);

    default:
        return DefWindowProc(window, msg, wp, lp);
    }
}

// 创建窗口
HWND CreatWnd(LPCTSTR strTitle = _T("title"), LPCTSTR strClass = _T("WndClass"))
{
    // 窗口类
    WNDCLASSEX wc;

    wc.cbSize = sizeof(WNDCLASSEX);
    /* Win 3.x */
    wc.style = CS_DBLCLKS;
    wc.lpfnWndProc = WindowProcedure;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = GetModuleHandle(0);
    wc.hIcon = LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = 0;
    wc.lpszClassName = strClass;
    /* Win 4.0 */
    wc.hIconSm = LoadIcon(0, IDI_APPLICATION);

    if (RegisterClassEx(&wc))
    {
        RECT rect;
        rect.left = 300;
        rect.top = 300;
        rect.right = 640;
        rect.bottom = 480;

        HWND window = CreateWindowEx(0, strClass, strTitle,
            WS_OVERLAPPEDWINDOW, rect.left, rect.top,
            rect.right, rect.bottom, 0, 0, GetModuleHandle(0), 0);

        if (window)
        {
            ShowWindow(window, SW_SHOWDEFAULT);
            return window;
        }
        else
        {
            return NULL;
        }
    }
    else
    {
        return NULL;
    }
}

// 派发消息
void OnMsg()
{
    MSG msg;
    GetMessage(&msg, 0, 0, 0);
    DispatchMessage(&msg);
}

int main()
{
    HWND wnd = CreatWnd(L"My Wnd");

    while (true)
    {
        OnMsg();
    }

    return 0;
}




返回首页


Copyright (C) 2018-2024 huidong