c - 设置静态文本颜色 Win32

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

最佳答案

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

关于c - 设置静态文本颜色 Win32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1525669/

相关文章:

java - SugarCRM 自定义字段

python - Youtube V3 API 新订阅视频脚本

winapi - 运行一个简单的 shell 命令

c++ - 如何使用 GCC 配置 Mono Include Directory 路径

c - 这两种解决方案有什么区别?

c - c中全局变量和堆变量有什么异同?

api - Uber API 显示汽车位置

C++ 类 : Object that creates Thread + pointer to the function = Access violation

windows - BS_OWNERDRAW 按钮的悬停视觉状态不同

c - 匿名结构