设置静态文本颜色Win32

时间:2009-10-06 13:41:13

标签: c api winapi

我正在创建一个控制对话框的dll。我喜欢让某个区域有红色文字。这段代码确实编译,但没有看到效果。这是dialogProc完成的区域:

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;
}

1 个答案:

答案 0 :(得分:10)

WM_CTLCOLORSTATIC是来自WM_COMMAND的单独消息。您对邮件的理想处理似乎是正确的,除了检查邮件是在WM_COMMAND特定项目的检查范围内。尝试重新组织外部switch语句。也许类似于以下内容:

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;
}

另请注意,当wParam应该为WM_CTLCOLORSTATIC提供HDC时,过滤WM_COMMAND的wParam参数会有点奇怪。

WM_CTLCOLORSTATIC Notification at MSDN