如何使一个子窗口最顶层(c ++ win32 SDK)

时间:2014-09-04 10:33:31

标签: c++ winapi z-order childwindow topmost

我正在尝试编写一个非常简单的gui布局设计器,就像最流行的IDE一样: enter image description here

如何在小部件周围实现这8个点? 我的想法是创建一个透明的静态控件(称为ghost),周围有8个点,将其重新分配到小部件的相同大小,覆盖小部件。问题是,如何在其他控制面前制造幽灵?

我写了一个小测试,一个按钮和一个静态控件,我希望静态始终位于z-order的顶部

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int main() {
    static char szAppName[] = "winhello";
    HWND        hwnd;
    MSG         msg;
    WNDCLASSEX  wndclass;

    wndclass.cbSize         = sizeof(wndclass);
    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hInstance      = GetModuleHandle(0);
    wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszClassName  = szAppName;
    wndclass.lpszMenuName   = NULL;

    RegisterClassEx(&wndclass);

    hwnd = CreateWindow(szAppName, "Hello, world!",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, GetModuleHandle(0), NULL);

    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    while ( GetMessage(&msg, NULL, 0, 0) ) {
    TranslateMessage(&msg);    /*  for certain keyboard messages  */
    DispatchMessage(&msg);     /*  send message to WndProc        */
    } 

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
    HWND hStatic;
    HWND hButton;

    switch ( iMsg ) {
    case WM_CREATE:
        {

            hButton = CreateWindow("button", "btn", WS_CHILD|WS_VISIBLE, 
                                 10, 10, 100, 100, hwnd, 0, 0, 0);
            hStatic = CreateWindow("static", "edt", WS_CHILD|WS_VISIBLE,
                                   40, 40, 50, 200, hwnd, 0, 0, 0);
            // WS_EX_TOPMOST doesn't work
            // HWND_TOP doesn't work
            // SetWindowPos(hStatic, HWND_TOP, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

运行程序,单击按钮,按钮转到前面

enter image description here

我尝试了 SetWindowPos(hStatic,HWND_TOP) WS_EX_TOPMOST ,但没有用。

0 个答案:

没有答案