5

I am making a dll that controls a dialogue box. I like to get a certain area to have red text. This code does compile, but the effect is not seen. Here is the area where the dialogProc is done:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:     
        CheckDlgButton(hDlg, IDC_CHECK, FALSE);
        EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
        return TRUE;

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_CHECK:
            if (IsDlgButtonChecked(hDlg, IDC_CHECK))
            {
                EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
                EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);
            }
            else
            {
                EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
                EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE);
            }
            break;
        case IDOK:
            {           
                EndDialog(hDlg, TRUE);
                return FALSE;
            }
        case IDCANCEL:
            {               
                EndDialog(hDlg, FALSE);
                return FALSE;
            }
        case WM_CTLCOLORSTATIC:
            // Set the colour of the text for our URL
            if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING)) 
            {
                // we're about to draw the static
                // set the text colour in (HDC)lParam
                SetBkMode((HDC)wParam,TRANSPARENT);
                SetTextColor((HDC)wParam, RGB(255,0,0));
                return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU));
            }
    return TRUE;
        }
    }
    return FALSE;
}
7
  • are you sure that your code block is executed? Oct 6, 2009 at 14:04
  • The case statement is, but I think that is more based on user interaction, such as clicking buttons. I am not too sure how to set color statically on Win32 API. I am more used to C++ where there was a color attribute on the object. Oct 6, 2009 at 14:09
  • Well, the blue is now working, I now have white background everywhere else! Oct 6, 2009 at 14:55
  • All of the backgrounds are okay except for the icon control. It is marked transparent too. Not too sure why that control now has a white background! Oct 6, 2009 at 15:08
  • Yeah, I realised that later, that's how I got it to work now, I moved it out of that block, I just put it in the wrong spot by accident! But, I can't get my icon to have a transparent background now. I moved the transparent portion outside of the if, so everything is transparent, since before the black text had a white background, but i couldn't get the icon now to have a transparent background. IUt was prior to this working. Oct 6, 2009 at 15:23

1 Answer 1

12

WM_CTLCOLORSTATIC is a separate message from WM_COMMAND. Your desired handling of the message appears to be correct except that the check for the message is inside your check for WM_COMMAND specific items. Try reorganizing your outer switch statement. Perhaps something like the following:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, 
                            WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:         
        // ...
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_CHECK:
            // ...
            break;
        case IDOK:
            // ...
            break;
        case IDCANCEL:
            // ...
            break;
        }
        break;
    case WM_CTLCOLORSTATIC:
        // Set the colour of the text for our URL
        if ((HWND)lParam == GetDlgItem(hDlg, IDC_WARNING)) 
        {
                // we're about to draw the static
                // set the text colour in (HDC)lParam
                SetBkMode((HDC)wParam,TRANSPARENT);
                SetTextColor((HDC)wParam, RGB(255,0,0));
                // NOTE: per documentation as pointed out by selbie, GetSolidBrush would leak a GDI handle.
                return (BOOL)GetSysColorBrush(COLOR_MENU);
        }
        break;
    }
    return FALSE;
}

Also note that it would be kinda weird to filter WM_COMMAND's wParam argument when wParam is supposed to provide the HDC for WM_CTLCOLORSTATIC.

WM_CTLCOLORSTATIC Notification at MSDN

2
  • 2
    Is there a way to do this without handling the CTLCOLORSTATIC message >?
    – viraj
    Nov 19, 2011 at 10:00
  • 7
    The above solution leaks at least one GDI object. There's no code to call DeleteObject for the call to CreateSolidBrush. I'm not sure if WM_CTLCOLORSTATIC gets passed on each paint operation or if it's only on the dialog initialization. But a leak is a leak. The code above should create the brush in WM_INITDIALOG and then call DeleteObject in WM_DESTROY. Alternatively, GetSysColorBrush may be a better solution for the above code instead of calling CreateSolidBrush.
    – selbie
    Nov 15, 2014 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.