Static Text is located on the form, the text color needs to be changed periodically, the background remains the original, it has reached:

case WM_CTLCOLORSTATIC: { if( ((HWND)lParam)==GetDlgItem(hDlg, IDC_STATUS_DDE) ) { SetBkMode((HDC) wParam, TRANSPARENT); SetTextColor((HDC) wParam, RGB(0,0,255)); } return (INT_PTR)GetStockObject(NULL_PEN); } 

As a result, the text IDC STATUS DDE changes color to blue, but on the rest of the Static Text, the background color has changed to another! Who is strong help to understand!

Changed the code as recommended by @mega

 case WM_CTLCOLORSTATIC: if( ((HWND)lParam)==GetDlgItem(hDlg, IDC_STATUS_DDE) ) { SetBkMode((HDC) wParam, TRANSPARENT); SetTextColor((HDC) wParam, RGB(0,0,255)); return (INT_PTR)GetStockObject(NULL_BRUSH); } else return DefWindowProc(hDlg, message, wParam, lParam); 

Earned almost as required, but with the next output in Static Text

  SetDlgItemTextA (g_hwndMainForm, IDC_STATUS_DDE, szStatusDDE); 

there is a superposition of one line to another, which I have not yet considered!?

  • one
    > but at the next output in Static Text, one line is superimposed on another, which I haven’t taken into account yet !? You need to return any brush other than NULL_BRUSH . Those. if you return NULL_BRUSH , the component will not fill the background at all. And since there is no background, it means that it will never be updated, i.e. all that is drawn on it, then it will remain. Hence the result - all overlays are visible. - mega
  • If you want the background color for IDC_STATUS_DDE remain unchanged, you do not need to return the handle at all, even if DefWindowProc does the same. - mega
  • If you use DefWindowProc, this case does not change the color of the text! - rejie
  • one
    > If you use DefWindowProc, in this case the text color does not change! You can first call DefWindowProc , and then change the text color, and then return what DefWindowProc returned. Or just return some system brush handle, for example: ::GetSysColorBrush( COLOR_BTNFACE ) - mega
  • Thank you, both of the options you offer work! 1. Which option will be the best (I think the second)? 2. If I set a value from the COLOR_BTNFACE system palette (the second option), will the colors be displayed correctly if I change the color schemes in Windows? - rejie

2 answers 2

First, the result of the WM_CTLCOLORSTATIC should be a handle to the brush, not the pen. Second, you return it no matter what descriptor comes in lParam . Therefore, all components are painted the same. Return the handle to the condition, and for all others, pass WM_CTLCOLORSTATIC processing to the standard window procedure.

    Here is the option that turned out, with the background color of the window itself:

     case WM_CTLCOLORSTATIC: if (((HWND)lParam) == GetDlgItem(hWnd, 3)) { SetTextColor((HDC)wParam, RGB(0, 0, 255)); } return (LRESULT)GetSysColorBrush(COLOR_WINDOW); break;