我正在制作一个控制对话框的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/

10-13 08:35