2013-03-25 138 views
0

我有兩個窗口,其中一個較小,在其他更大的頂部。首先創建的窗口將始終處於頂層。 BringWindowToTop(hWnd)SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);均無效!我也一直在嘗試HWND_TOPMOSTHWND_BOTTOMWinAPI ZOrder困境

看起來使其中一個窗口成爲其他窗口的唯一方法是正確的創建順序。但這不是我所需要的,我需要在飛行中更改訂單。任何人都知道是什麼原因造成的那些只是老的普通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; 
} 
+0

要發佈的任何代碼? – 2013-03-25 08:08:31

回答

2

按照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是你的窗口應放置窗口的句柄。你試過這個嗎?

+0

是的,通過A和B確實有效!但我認爲我越來越瘋狂了,因爲靠近用戶的窗口實際上是根據我的手柄行爲的底部窗口。 – exebook 2013-03-25 08:37:04

+1

檢查[this](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v = vs.85).aspx#zorder)out,可能有助於解決您的困惑。 – 2013-03-25 08:38:11

1

好的,也許有人會發現它真的有用。因爲真正的問題是SetWindowPosBringWindowToTo都能正常工作,但是之後在我的真實應用程序中,我再次打電話給SetWindowPos來移動組件,並且它再次擰緊了ZOrder

所以我加SWP_NOZORDER第二個電話SetWindowPos