在 C++ 中创建 window
creating a window in c++
我对 c++ 和其他语言有一些经验,但我在使用 c++ 之前只制作过游戏,我需要制作一个 OBS 插件。我想知道是否有人可以提供帮助..
我正在尝试用 -
创建一个 window
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;
}
它不会创建 window,也不会给出错误。当我调用消息框时,它只会在我尝试关闭 window 时出现。为什么那个?
如何创建单独的 window?
我接下来的教程是 - 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" 注册第二个 class 名称,然后为此 child class 创建一个不同的消息过程。
然后您添加一个名为 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;
}
我对 c++ 和其他语言有一些经验,但我在使用 c++ 之前只制作过游戏,我需要制作一个 OBS 插件。我想知道是否有人可以提供帮助..
我正在尝试用 -
创建一个 window 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;
}
它不会创建 window,也不会给出错误。当我调用消息框时,它只会在我尝试关闭 window 时出现。为什么那个?
如何创建单独的 window?
我接下来的教程是 - 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" 注册第二个 class 名称,然后为此 child class 创建一个不同的消息过程。
然后您添加一个名为 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;
}