Setting the color of static Win32 text

I am creating a dll that controls a dialog box. I like to get a certain area to have red text. This code compiles, but no effect is observed. Here is the area where the dialog is executed:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: CheckDlgButton(hDlg, IDC_CHECK, FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_CHECK: if (IsDlgButtonChecked(hDlg, IDC_CHECK)) { EnableWindow(GetDlgItem(hDlg, IDOK), TRUE); EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE); } else { EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE); } break; case IDOK: { EndDialog(hDlg, TRUE); return FALSE; } case IDCANCEL: { EndDialog(hDlg, FALSE); return FALSE; } case WM_CTLCOLORSTATIC: // Set the colour of the text for our URL if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING)) { // we're about to draw the static // set the text colour in (HDC)lParam SetBkMode((HDC)wParam,TRANSPARENT); SetTextColor((HDC)wParam, RGB(255,0,0)); return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU)); } return TRUE; } } return FALSE; } 
+4
source share
1 answer

WM_CTLCOLORSTATIC is a separate message from WM_COMMAND. The desired message processing seems to be correct, except that the message check is inside your check for certain WM_COMMAND elements. Try reorganizing your external switch statement. Perhaps something like the following:

 LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: // ... break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_CHECK: // ... break; case IDOK: // ... break; case IDCANCEL: // ... break; } break; case WM_CTLCOLORSTATIC: // Set the colour of the text for our URL if ((HWND)lParam == GetDlgItem(hDlg, IDC_WARNING)) { // we're about to draw the static // set the text colour in (HDC)lParam SetBkMode((HDC)wParam,TRANSPARENT); SetTextColor((HDC)wParam, RGB(255,0,0)); // NOTE: per documentation as pointed out by selbie, GetSolidBrush would leak a GDI handle. return (BOOL)GetSysColorBrush(COLOR_MENU); } break; } return FALSE; } 

Also note that it would be strange to filter the WM_COMMAND wParam argument when wParam should provide an HDC for WM_CTLCOLORSTATIC.

WM_CTLCOLORSTATIC notification on MSDN

+9
source

All Articles