簡體   English   中英

用C ++創建一個窗口

[英]creating a window in c++

我對c ++和其他語言有一定的經驗,但是在使用c ++之前我只是做過游戲,所以我需要制作一個OBS插件。.我想知道是否有人可以提供幫助。

我正在嘗試使用-創建一個窗口

    int nHeight = 500;
int nWidth = 500;

#define metaData(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\
    CreateWindowExA(0L, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)

if (GetAsyncKeyState(VK_F5))
    {
        //MsgeBox::myMessage::createMessage(NULL, (LPCWSTR)L"Hello", (LPCWSTR)L"I See You.", MB_ICONWARNING | MB_CANCELTRYCONTINUE);
        #define CreateWindow metaData;
    }

它不會創建窗口,也不會給出錯誤..當我調用消息框時,它僅在我嘗試關閉窗口時才出現..為什么?

如何創建單獨的窗口?

我下面的教程是-https: //msdn.microsoft.com/en-us/library/bb384843.aspx

您必須創建一個消息過程,然后響應關鍵消息。 例如

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_KEYDOWN:
        if (wp == VK_F5)
        CreateWindow(L"ChildClass", L"Child window", 
                    WS_VISIBLE | WS_POPUPWINDOW | WS_CAPTION, 
                    0, 0, 300, 200, hwnd, 0, 0, 0);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, msg, wp, lp);
}

請注意,您必須為“ ChildClass”注冊另一個類名稱,然后為此子類創建一個不同的消息過程。

然后,添加一個名為ChildProc的單獨函數,該函數類似於WndProc 例如:

#define UNICODE
#include <Windows.h>

HINSTANCE g_hinstance = 0;

LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, msg, wp, lp);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_KEYDOWN:
    {
        if (wp == VK_F5)
        {
            MessageBox(0, L"VK_F5 detected", 0, 0);
            if (!CreateWindow(L"ChildClass", L"ChildTitle",
                WS_VISIBLE | WS_POPUPWINDOW | WS_CAPTION, 100, 100, 300, 200,
                hwnd, 0, g_hinstance, 0))
            {
                DWORD err = GetLastError();
                wchar_t buf[100];
                wsprintf(buf, L"%d\n", err);
                MessageBox(0, buf, 0, 0);
            }
        }
        break;
    }

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, msg, wp, lp);
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
    WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wcex.lpszClassName = L"MainClass";
    RegisterClassEx(&wcex);

    wcex.lpszClassName = L"ChildClass";
    wcex.lpfnWndProc = ChildProc;
    RegisterClassEx(&wcex);

    CreateWindow(L"MainClass", L"MainTitle", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        0, 0, 600, 400, 0, 0, hInstance, 0);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM