Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set static text color Win32

Tags:

c

winapi

api

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;
}
like image 726
Jon Weinraub Avatar asked Oct 06 '09 13:10

Jon Weinraub


1 Answers

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

like image 132
meklarian Avatar answered Nov 14 '22 14:11

meklarian