【发布时间】:2021-11-22 11:06:23
【问题描述】:
我想创建一个包含一个主父窗口和几个子窗口的 Windows 应用程序。这是我到目前为止的代码摘录:
...
// -----> CHILD WINDOWS <-----
HWND hWnd_child1 = CreateWindowW(L"STATIC", L"Child 1", WS_CHILD,
0, 0, 100, 80, hParentWnd, nullptr, hInstance, nullptr);
if (!hWnd_child1) {
MessageBox(NULL, L"CreateWindowW Child 1", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
HWND hWnd_child2 = CreateWindowW(L"STATIC", L"Child 2", WS_CHILD,
10, 10, 160, 120, hParentWnd, nullptr, hInstance, nullptr);
if (!hWnd_child2) {
MessageBox(NULL, L"CreateWindowW Child 2", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
HWND hWnd_child3 = CreateWindowW(L"STATIC", L"Child 3", WS_CHILD,
20, 20, 160, 120, hParentWnd, nullptr, hInstance, nullptr);
if (!hWnd_child3) {
MessageBox(NULL, L"CreateWindowW Child 3", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
ShowWindow(hWnd_child3, nCmdShow);
SetWindowPos(hWnd_child2, HWND_TOP, 10, 10, 100, 80, NULL);
ShowWindow(hWnd_child2, nCmdShow);
SetWindowPos(hWnd_child1, HWND_TOP, 0, 0, 100, 80, NULL);
ShowWindow(hWnd_child1, nCmdShow);
ShowWindow(hParentWnd, nCmdShow);
UpdateWindow(hParentWnd);
// -------------------
...
问题在于SetWindowPos() 函数。我无法理解它是如何工作的。我以为这样称呼它:
ShowWindow(hWnd_child3, nCmdShow);
SetWindowPos(hWnd_child2, HWND_TOP, 10, 10, 100, 80, NULL);
ShowWindow(hWnd_child2, nCmdShow);
SetWindowPos(hWnd_child1, HWND_TOP, 0, 0, 100, 80, NULL);
ShowWindow(hWnd_child1, nCmdShow);
会将Child 1 窗口移动到所有应用程序窗口的顶部(正如doc 对HWND_TOP 选项所说:Places the window at the top of the Z order)。
但是,窗口仍然按创建顺序绘制:
难道SetWindowPos() 不应该先移动Child 2 到Child 3,然后再移动Child 1 到Child 2,从而使窗口以与创建时相反的顺序排列,Child 1 在顶部?
【问题讨论】:
-
如果一切都是
HWND_TOP,那么实际上谁有优先权? ?????? -
这能回答你的问题吗? Moving a child control to top
-
还有stackoverflow.com/questions/17722398/child-window-z-order/…(本质上是z顺序和剪辑兄弟之间的区别)
-
您可能还需要让这些子窗口自己重绘。
-
@PeteKirkham,是的,这是一个类似的问题,但正如 David Heffernan 所建议的那样,最奇怪的叫
SetWindowPos(b1, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);对我不起作用,而是在b2上调用SetWindowPos工作!。是不是很奇怪!?