静态文本颜色 [英] Static Text Color

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

问题描述

我编写了以下代码,它将颜色应用于一个窗口中的所有静态文本,但是我想在一个窗口中应用两种不同的颜色,比如ID:1234,其中ID是另一种颜色,1234 将在一个窗口中显示不同的颜色.我怎样才能做到这一点?这是我所做的:

I have written Following code which will apply color to all static text in one window, but I want to apply two different colors in one window like ID:1234 where ID is another color and 1234 will be different color in one window. How can I do this? here is what i have done:

case WM_CTLCOLORSTATIC:

    SetBkColor( hdc, COLORREF( :: GetSysColor( COLOR_3DFACE) ) );    
    //sets bckcolor of static text same as window color

    if ( ( HWND ) lParam == GetDlgItem( hWnd, IDC_PID) ) 
    {
    SetTextColor( ( HDC ) wParam, RGB( 250, 50, 200));
    return ( BOOL ) CreateSolidBrush ( GetSysColor( COLOR_3DFACE) );
    }

    break;

推荐答案

我不确定我是否理解您的问题.你的代码看起来很不错.值得注意的一点是,您负责清理您分配的资源.使用上面的代码,您将泄漏通过调用 CreateSolidBrush 创建的 HBRUSH 对象.由于您不需要自定义画笔,您应该使用 GetSysColorBrush.

I'm not sure I understand your problem. Your code looks pretty much ok. One point worth noting is that you are responsible for cleaning up resources that you allocate. With the code above you are leaking the HBRUSH object created through a call to CreateSolidBrush. Since you don't need a custom brush you should rather be using GetSysColorBrush.

根据个人喜好,我会使用 GetDlgCtrlID.合并更改后,您的代码应如下所示:

As a matter of taste I would filter on the control ID rather than its window handle using GetDlgCtrlID. Incorporating the changes your code should look like this:

case WM_CTLCOLORSTATIC:
    switch ( GetDlgCtrlID( (HWND)lParam ) )
    {
    case IDC_PID:
        //sets bckcolor of static text same as window color
        SetBkColor( (HDC)wParam, COLORREF( GetSysColor( COLOR_3DFACE ) ) );    
        SetTextColor( (HDC)wParam, RGB( 250, 50, 200) );
        return (INT_PTR)GetSysColorBrush( COLOR_3DFACE );

    default:
        // Message wasn't handled -> pass it on to the default handler
        return 0;
    }

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆