简体   繁体   中英

Set background color of a specific control

I have searched the net looking for a way to set background color a dialog control.

I have managed to do this with this code:

case WM_CTLCOLORSTATIC:
{
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(255,255,255));
    SetBkColor(hdcStatic, RGB(0,0,0));

    if (hbrBkgnd == NULL)
    {
        hbrBkgnd = CreateSolidBrush(RGB(0,0,0));
    }
    return (INT_PTR)hbrBkgnd;
}

However, what I am actually looking for is to color only a specific static control, not all the static controls I have in my dialog. Is there anyway to do this? Perhaps set the hdc to something using GetDlgItem(hdlg,"IDC_MYCONTROL")?

-- UPDATE

After the suggestions I ended up with this:

case WM_CTLCOLORSTATIC:
{
    HDC hdcStatic = (HDC) lParam;
    HWND hWnd =  (HWND)lParam;
    HWND dlg =GetDlgItem(hDlg, IDC_STATIC2);
    if (hWnd == dlg)
    {
        SetTextColor(hdcStatic, RGB(255,255,255));
        SetBkColor(hdcStatic, RGB(0,0,0));
    }
    if (hbrBkgnd == NULL)
    {
        hbrBkgnd = CreateSolidBrush(RGB(0,0,0));
    }
    return (INT_PTR)hbrBkgnd;
}

And it seems that even if SetBkColor is run nothing changes on the dialog, leading on the wierd problem described below.

The HWND is passed to the dialog proc so you could;

 HWND hWnd = (HWND) lParam;
 if (hWnd == GetDlgItem(hdlg, "IDC_MYCONTROL")) {
     ...

Check lParam matches the handle to the child that you want to change the colour of.

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