将多个 Windows 添加到 WinAPI 应用程序
Adding Multiple Windows to WinAPI Application
我正在使用 WinAPI 构建一个应用程序,该应用程序基本上由两个主要部分组成:左侧面板始终显示,右侧区域根据显示的屏幕而变化。目前 left-hand,静态区域正被绘制到主要区域 window;但是,因为我不相信我可以简单地将正确的区域绘制到主要 window 上,因为它需要更改。因此,我认为我需要在那里创建 windows 并通过控制其可见性来控制显示的内容。我在 MSDN 中阅读了很多关于多文档界面的内容,但我不确定这是否是我正在寻找的内容。具体来说,我不希望 child windows 表现得像真实的 windows,这意味着我不希望它们有标题或图标,或者能够最小化、关闭,调整大小或移动。基本上,我希望它们只是包含 controls/D2D 几何体的框架。执行此操作的最佳方法是什么?
正如雷米所说,你只需要re-register一个新的window然后在主window中创建它作为一个子类。设置样式时不需要设置标题和最大化最小化按钮,也可以自行决定是否需要处理子类的WNDPROC函数
这里是一个示例(为了显示child window,我将它的背景设置为黑色。):
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK cldWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
static TCHAR cldwndName[] = TEXT("test app");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
100,
100,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
WNDCLASS cldclass;
cldclass.style = CS_HREDRAW | CS_VREDRAW;
cldclass.lpfnWndProc = cldWndProc;
cldclass.cbClsExtra = 0;
cldclass.cbWndExtra = 0;
cldclass.hInstance = hInstance;
cldclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
cldclass.hCursor = LoadCursor(NULL, IDC_ARROW);
cldclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
cldclass.lpszMenuName = NULL;
cldclass.lpszClassName = cldwndName;
if (!RegisterClass(&cldclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
HWND hwnd1 = CreateWindow(cldwndName,
NULL,
WS_CHILD|WS_VISIBLE,
400,
200,
200,
300,
hwnd,
NULL,
hInstance,
NULL);
ShowWindow(hwnd1, iCmdShow);
UpdateWindow(hwnd1);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK cldWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对我有用:
我正在使用 WinAPI 构建一个应用程序,该应用程序基本上由两个主要部分组成:左侧面板始终显示,右侧区域根据显示的屏幕而变化。目前 left-hand,静态区域正被绘制到主要区域 window;但是,因为我不相信我可以简单地将正确的区域绘制到主要 window 上,因为它需要更改。因此,我认为我需要在那里创建 windows 并通过控制其可见性来控制显示的内容。我在 MSDN 中阅读了很多关于多文档界面的内容,但我不确定这是否是我正在寻找的内容。具体来说,我不希望 child windows 表现得像真实的 windows,这意味着我不希望它们有标题或图标,或者能够最小化、关闭,调整大小或移动。基本上,我希望它们只是包含 controls/D2D 几何体的框架。执行此操作的最佳方法是什么?
正如雷米所说,你只需要re-register一个新的window然后在主window中创建它作为一个子类。设置样式时不需要设置标题和最大化最小化按钮,也可以自行决定是否需要处理子类的WNDPROC函数
这里是一个示例(为了显示child window,我将它的背景设置为黑色。):
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK cldWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
static TCHAR cldwndName[] = TEXT("test app");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
100,
100,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
WNDCLASS cldclass;
cldclass.style = CS_HREDRAW | CS_VREDRAW;
cldclass.lpfnWndProc = cldWndProc;
cldclass.cbClsExtra = 0;
cldclass.cbWndExtra = 0;
cldclass.hInstance = hInstance;
cldclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
cldclass.hCursor = LoadCursor(NULL, IDC_ARROW);
cldclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
cldclass.lpszMenuName = NULL;
cldclass.lpszClassName = cldwndName;
if (!RegisterClass(&cldclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
HWND hwnd1 = CreateWindow(cldwndName,
NULL,
WS_CHILD|WS_VISIBLE,
400,
200,
200,
300,
hwnd,
NULL,
hInstance,
NULL);
ShowWindow(hwnd1, iCmdShow);
UpdateWindow(hwnd1);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK cldWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对我有用: