ᴘᴀɴᴀʏɪᴏᴛɪs
ᴘᴀɴᴀʏɪᴏᴛɪs

Reputation: 7509

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.

Upvotes: 0

Views: 3693

Answers (2)

Alex K.
Alex K.

Reputation: 175748

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

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

Upvotes: 1

Mike Kwan
Mike Kwan

Reputation: 24447

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

Upvotes: 0

Related Questions