简体   繁体   中英

Static Text Color

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM