Static Text Color

岁酱吖の 提交于 2021-02-08 19:58:10

问题


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;

回答1:


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.

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


来源:https://stackoverflow.com/questions/12929554/static-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!