设置静态文本颜色的Win32

设置静态文本颜色的Win32

本文介绍了设置静态文本颜色的Win32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做控制的对话框的DLL。我想获得一定区域内有红色文字。这code编译做,但效果是没有看到。这里就是dialogProc完成的区域:

  LRESULT CALLBACK DialogProc(HWND hDlg,UINT的消息,WPARAM wParam参数,LPARAM lParam的)
{
    开关(消息)
    {
    案例WM_INITDIALOG:
    CheckDlgButton(hDlg,IDC_CHECK,FALSE);
    EnableWindow(函数GetDlgItem(hDlg,IDOK),FALSE);
    返回TRUE;    案例WM_COMMAND:
    开关(LOWORD(wParam参数))
    {
    案例IDC_CHECK:
    如果(IsDlgButtonChecked(hDlg,IDC_CHECK))
    {
    EnableWindow(函数GetDlgItem(hDlg,IDOK),TRUE);
    EnableWindow(函数GetDlgItem(hDlg,IDCANCEL),FALSE);
    }
    其他
    {
    EnableWindow(函数GetDlgItem(hDlg,IDOK),FALSE);
    EnableWindow(函数GetDlgItem(hDlg,IDCANCEL),TRUE);
    }
    突破;
    案例IDOK:
    {
    的EndDialog(hDlg,TRUE);
    返回FALSE;
    }
    案例IDCANCEL:
    {
    的EndDialog(hDlg,FALSE);
    返回FALSE;
    }
    案例WM_CTLCOLORSTATIC:
    //设置文本的颜色为我们的网址
    如果((HWND)lParam的==函数GetDlgItem(hDlg,IDC_WARNING))
    {
    //我们要绘制静态
    //设置(HDC)lParam的文本颜色
    SetBkMode((HDC)的wParam,透明);
    SetTextColor((HDC)wParam中,RGB(255,0,0));
    返回(布尔)CreateSolidBrush(GetSysColor(COLOR_MENU));
    }
    返回TRUE;
    }
    }
    返回FALSE;
}


解决方案

WM_CTLCOLORSTATIC是WM_COMMAND单独的消息。您想要的信息处理看来是正确的,只是该消息的检查是你的支票WM_COMMAND具体项目中。尝试重组你的外在switch语句。也许类似以下内容:

  LRESULT CALLBACK DialogProc(HWND hDlg,UINT的消息,
                            WPARAM wParam参数,LPARAM lParam的)
{
    开关(消息)
    {
    案例WM_INITDIALOG:
        // ...
        打破;
    案例WM_COMMAND:
        开关(LOWORD(wParam参数))
        {
        案例IDC_CHECK:
            // ...
            打破;
        案例IDOK:
            // ...
            打破;
        案例IDCANCEL:
            // ...
            打破;
        }
        打破;
    案例WM_CTLCOLORSTATIC:
        //设置文本的颜色为我们的网址
        如果((HWND)lParam的==函数GetDlgItem(hDlg,IDC_WARNING))
        {
                //我们要绘制静态
                //设置(HDC)lParam的文本颜色
                SetBkMode((HDC)的wParam,透明);
                SetTextColor((HDC)wParam中,RGB(255,0,0));
                //注意:每个文件由selbie指出,GetSolidBrush会泄漏一个GDI句柄。
                回报(BOOL)GetSysColorBrush(COLOR_MENU);
        }
        打破;
    }
    返回FALSE;
}

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

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;
}
解决方案

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

这篇关于设置静态文本颜色的Win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:45