KayWang
KayWang

Reputation: 71

Can I make a Win32 child window show under the parent window?

I want to add a child window to a parent window,make the content of child window is below the parent window(Z order).just like compose two layer,not replacement.Because I want some UI element in parent window can be seen within the child window's area,but not cover all of the child window.The child window should be a window,so I can't just using parent window's UI system to implement the child window's UI.

Is there any api to achieve this?

I found something like "UpdateLayeredWindow",But I failed to create a layer child window,CreateWindowEx return NULL and GetLastError return 0.This code was copy from Windows's example.and the demo project (DirectCompositionLayeredChildWindow) works well.

HRESULT hr = S_OK;

// Register the window class.
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
wc.lpszClassName = L"DirectComposition Window Class";

RegisterClassEx(&wc);

// Creates the m_hMainWindow window.
HWND m_hMainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW| WS_EX_COMPOSITED,                          // Extended window style
    wc.lpszClassName,                                // Name of window class
    L"DirectComposition Layered Child Window Sample", // Title-bar string
    WS_OVERLAPPED | WS_SYSMENU,                      // Top-level window
    CW_USEDEFAULT,                                   // Horizontal position
    CW_USEDEFAULT,                                   // Vertical position
    1000,                                            // Width
    700,                                             // Height
    NULL,                                            // Parent
    NULL,                                            // Class menu
    GetModuleHandle(NULL),                           // Handle to application instance
    NULL                                             // Window-creation data
);

if (!m_hMainWindow)
{
    hr = HRESULT_FROM_WIN32(GetLastError());
}

if (SUCCEEDED(hr))
{
    ShowWindow(m_hMainWindow, SW_SHOWDEFAULT);
}


WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WindowProc;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"DirectCompositionChildWindow-Child";

RegisterClassEx(&wcex);

// !!! m_hControlChildWindow is always NULL
HWND m_hControlChildWindow = CreateWindowEx(WS_EX_LAYERED,                           // Extended window style
    wcex.lpszClassName,                      // Name of window class
    NULL,                                    // Title-bar string
    WS_CHILD,              // Child window
    30,30, 100, 100,                              // Window will be resized via MoveWindow
    m_hMainWindow,                           // Parent
    NULL,                                    // Class menu
    GetModuleHandle(NULL),                   // Handle to application instance
    NULL);                                   // Window-creation data

if (!m_hControlChildWindow)
{
    hr = HRESULT_FROM_WIN32(GetLastError());
}
ShowWindow(m_hControlChildWindow, nCmdShow);
UpdateWindow(m_hControlChildWindow);

Upvotes: 1

Views: 1671

Answers (1)

IInspectable
IInspectable

Reputation: 51345

This cannot be done, as explained here:

No part of a child window ever appears outside the borders of its parent window.

Also:

A child window is grouped with its parent in z-order.

In other words: You cannot create a child window that's either outside the parent window's client area, or underneath the parent window in Z-order.

Upvotes: 2

Related Questions