【问题标题】:WinAPI ZOrder woesWinAPI ZOrder 问题
【发布时间】:2013-03-25 08:06:20
【问题描述】:

我有两扇窗户,一扇较小,另一扇较大。首先创建的窗口将始终位于顶部。 BringWindowToTop(hWnd)SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 都不做任何事情!我也一直在尝试HWND_TOPMOSTHWND_BOTTOM

似乎使一个窗口在另一个窗口之上的唯一方法是正确的创建顺序。但这不是我需要的,我需要即时更改订单。有谁知道这可能是什么原因?这些只是旧的普通CreateWindow() 实例都具有相同的父级。互联网上的任何搜索都不会产生任何结果。需要专家帮助!

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

HWND make_child(HWND Parent, int x, int y, int color) {
    HINSTANCE hInstance = 0;
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = CreateSolidBrush(color);//(HBRUSH)(COLOR_WINDOWFRAME);
    wc.lpszClassName = (LPCSTR)L"mychildwin";
    RegisterClass(&wc);
    return CreateWindow("edit",(LPCSTR)"child", WS_BORDER|WS_CHILD|WS_VISIBLE, x,y,100,100,Parent,0,hInstance,NULL);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = CreateSolidBrush(0xff8080);
    wc.lpszClassName = (LPCSTR)L"minwindowsapp";
    if (!RegisterClass(&wc))  return 1;
    HWND W = CreateWindow(wc.lpszClassName,(LPCSTR)L"Minimal Windows Application", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0,0,640,480,0,0,hInstance,NULL);
    HWND A = make_child(W, 10, 10, 0x88ff00);
    HWND B = make_child(W, 70, 70, 0x8888ff);
    BringWindowToTop(B);
    SetWindowPos(B, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

    //REAL PROBLEM WAS HERE: A second call to SetWindowPos
     SetWindowPos(handle, 0, 0, 0, new_width, new_height,
      SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
    // adding SWP_NOZORDER fixed it..

    while(GetMessage(&msg, NULL, 0, 0) > 0) DispatchMessage(&msg);
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message) {
        case WM_CLOSE: PostQuitMessage(0); break;
        default: return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}  

【问题讨论】:

    标签: winapi z-order


    【解决方案1】:

    根据MSDN-documentation,还可以将窗口句柄传递给您希望在其后插入窗口的窗口。

    BOOL WINAPI SetWindowPos(
      _In_      HWND hWnd,
      _In_opt_  HWND hWndInsertAfter,
      _In_      int X,
      _In_      int Y,
      _In_      int cx,
      _In_      int cy,
      _In_      UINT uFlags
    );
    

    其中hWndInsertAfter 是您的窗口应位于之后 的窗口句柄。你试过这个吗?

    【讨论】:

    • 是的,通过 A 和 B 确实有效!但我觉得我快疯了,因为根据我的句柄的行为,离用户更近的窗口实际上是底部窗口。
    • 检查this,可能有助于解决您的困惑。
    【解决方案2】:

    好的,也许有人会发现它真的很有用。因为真正的问题是SetWindowPosBringWindowToTo 都运行良好,但之后在我的真实应用程序中我再次调用SetWindowPos 来移动组件一点,它又搞砸了 ZOrder

    所以我在第二次调用SetWindowPos 时添加了SWP_NOZORDER

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多