匯東網


win32 应用程序模版

[編輯] [转简体]
|
作者:huidong | 分類:【編程】Win32
[ 8 瀏覽 0 評論 1 贊 1 踩 ]

概要
快速创建 win32 程序

正文

win32 入口

int WINAPI WinMain(HINSTANCE hThisInst,
    HINSTANCE hPrevInst,
    LPSTR lpszCmdLine,
    int nCmdShow)
{

---

#include <windows.h>

#define IDC_EDIT 100
#define IDC_BTN 101


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HINSTANCE hInstance = GetModuleHandle(0);
    static HWND hEdit;
    static HWND hBtn;

    switch (msg)
    {
    case WM_CREATE:

            // 创建一个输入框,一个按钮
            hEdit = CreateWindow(L"edit", L"Edit at here.",
                WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
                130, 50, 200, 20,
                hwnd, (HMENU)IDC_EDIT, hInstance, NULL);
            
            hBtn = CreateWindow(L"button", L"Click Me!",
                WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
                330, 50, 100, 20,
                hwnd, (HMENU)IDC_BTN, hInstance, NULL);

        break;

    case WM_COMMAND:

        switch (LOWORD(wParam))
        {

            // 按下按钮
        case IDC_BTN:

            if (true)
            {
                wchar_t str[128] = { 0 };

                // 得到输入框文本
                GetWindowText(hEdit, str, 512);
                MessageBox(hwnd, str, L"your input", MB_OK);
            }

            break;
        }

        break;
    
    case WM_CLOSE:
        DestroyWindow(hwnd);
        PostQuitMessage(NULL);
        exit(0);
        break;
    }

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

// 快速新建窗口
void initwindow(int w, int h, int mode, LRESULT(_stdcall* WindowProcess)(HWND, UINT, WPARAM, LPARAM), LPCTSTR strWndTitle)
{
    // 窗口类
    WNDCLASSEX WndClassEx;
    MSG Msg;

    // 隐藏cmd
    if (mode == 0)
        ShowWindow(GetConsoleWindow(), SW_HIDE);

    // 填写结构体
    WndClassEx.cbSize = sizeof(WNDCLASSEX);
    WndClassEx.style = CS_VREDRAW | CS_HREDRAW;
    WndClassEx.lpfnWndProc = WindowProcess;
    WndClassEx.cbClsExtra = 0;
    WndClassEx.cbWndExtra = 0;
    WndClassEx.hInstance = GetModuleHandle(0);
    WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClassEx.lpszMenuName = NULL;
    WndClassEx.lpszClassName = strWndTitle;
    WndClassEx.hIconSm = NULL;

    // 注册窗口类
    RegisterClassEx(&WndClassEx);

    // 创建窗口
    HWND hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, strWndTitle, strWndTitle, WS_OVERLAPPEDWINDOW, 200, 200, w, h, NULL, NULL, GetModuleHandle(0), NULL);

    // 显示窗口
    ShowWindow(hWnd, SW_SHOWNORMAL);

    // 更新窗口
    UpdateWindow(hWnd);

    // 循环获得消息
    while (GetMessage(&Msg, NULL, NULL, NULL))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
}


int main()
{
    initwindow(640, 480, 0, WndProc, L"mywnd");
    return 0;
}


[ 1] [ 1]


 評論區  0 條評論

+ 添加評論