繁体   English   中英

如何使用SetWindowPos function?

[英]How to use the SetWindowPos function?

我想创建一个 Windows 应用程序,该应用程序由一个主要父项 window 和多个子项 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() function。我不明白它是如何工作的。 我以为这样称呼它:

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 window 移动到所有应用程序 windows 的顶部(如文档针对HWND_TOP选项所说: Places the window at the top of the Z order )。

但是,windows 仍然按照创建顺序绘制:

图像

SetWindowPos()不应该先将Child 2移到Child 3上,然后将Child 1移到Child 2上,从而使 windows 的放置顺序与创建顺序相反,而Child 1在上面?

使孩子 windows 都具有 window 样式 WS_CLIPSIBLINGS 以及 WS_CHILD 等。

来自微软的文档:

如果未指定 WS_CLIPSIBLINGS 并且子 windows 重叠,则在子 window 的客户区内绘制时,可能会在相邻子 window 的客户区内绘制。

基本上,如果孩子 windows 不相互剪辑,那么它们的绘制顺序(这是任意的)决定了视觉 z 顺序。

例如,下面是您的代码,其中删除了消息框内容,使用 WS_VISIBLE 而不是 ShowWindow,为可见性添加边框,并使用 WS_CLIPSIBLINGS。

BOOL CreateChildren(HWND hParentWnd) {
    HWND hWnd_child1 = CreateWindowEx(WS_EX_CLIENTEDGE, L"STATIC", L"Child 1", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
        0, 0, 100, 80, hParentWnd, nullptr, g_hInstance, nullptr);

    HWND hWnd_child2 = CreateWindowEx(WS_EX_CLIENTEDGE, L"STATIC", L"Child 2", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
        10, 10, 160, 120, hParentWnd, nullptr, g_hInstance, nullptr);

    HWND hWnd_child3 = CreateWindowEx(WS_EX_CLIENTEDGE, L"STATIC", L"Child 3", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
        20, 20, 160, 120, hParentWnd, nullptr, g_hInstance, nullptr);

    SetWindowPos(hWnd_child2, HWND_TOP, 10, 10, 100, 80, NULL);
    SetWindowPos(hWnd_child1, HWND_TOP, 0, 0, 100, 80, NULL);

    UpdateWindow(hParentWnd);
    return TRUE;
}

哪个产量重叠的孩子 child1 在上面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM