Static Text Color

I wrote the following code that will apply color to all static texts in one window, but I want to apply two different colors in one window , for example ID:1234 , where ID is a different color, and 1234 will be a different color in one window. How can i do this? here is what i did:

 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; 
+4
source share
1 answer

I'm not sure I understand your problem. Your code looks pretty good. It should be noted that you are responsible for cleaning up the resources that you allocate. Using the code above, you are HBRUSH object created by calling CreateSolidBrush . Since you do not need a special brush, it is better to use GetSysColorBrush .

As a taste, I would filter out the control identifier, not its window handle, using GetDlgCtrlID . Including 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; } 
+2
source

Source: https://habr.com/ru/post/1440233/


All Articles