简体   繁体   中英

Can't change the background color of windows which are inside another window

I want to change the background color of a STATIC window, both - on load and change it on runtime. So far I have been able to change the color the following way:

    case WM_CTLCOLORSTATIC:
    {
        HDC hdcStatic = (HDC)wParam;
        SetTextColor(hdcStatic, RGB(200, 200, 20));
        SetBkColor(hdcStatic, RGB(10, 10, 10));
        SetBkMode(hdcStatic, TRANSPARENT);
        return (INT_PTR)CreateSolidBrush(RGB(30, 30, 30));
    }

Everything works fine and the background color gets changed, except for any STATIC windows, which are inside another static window:

HWND mainContainer = CreateWindowEx
(
    0,
    _TEXT("STATIC"),
    "",
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_SOLID,
    10, 10, 500, 500,
    hwnd,
    NULL,
    (HINSTANCE)GetWindowLong(hwnd, GWLP_HINSTANCE),
    NULL
);

HWND subItem = CreateWindowEx
(
    0,
    _TEXT("STATIC"),
    "SubItem",
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_SOLID,
    10, 10, 100, 100,
    mainContainer,
    NULL,
    (HINSTANCE)GetWindowLong(mainContainer, GWLP_HINSTANCE),
    NULL
);

In this case the mainContainer color gets changed, but not the background color for subItem. Any ideas why this is happening? Thank you!

The message WM_CTLCOLORSTATIC will be sent only to the parent window, but not parent's parent window.

According to About Static Controls :

The window procedure for the predefined static control window class performs default processing for all messages that the static control procedure does not process.

The WM_CTLCOLORSTATIC is not in the list that it process. So The predefined window procedure will passes the message to DefWindowProc for default processing.

(We really don't often put a static window inside another static window. This is not a common operation. So you should reset the parent window of subItem to hwnd .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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