Wednesday, March 25, 2009

Set Foreground Color to an Individual Edit Box or Static Control

If you have several edit boxes but want to set color to one of them only,
capture the WM_CTLCOLOREDIT message and get the handle to that edit box.


case WM_CTLCOLOREDIT:
//Get Handle to that control first

if((HWND)lParam==GetDlgItem(hwnd,IDC_TEXTBOX)){
HDC hDC = (HDC)wParam;
COLORREF color = RGB(192,0,0); // red color
SetTextColor(hDC, color);
//Optionally, set background color
//SetBkColor(hDC, RGB(255,255,0)); /* yellow */
}
return GetStockObject(WHITE_BRUSH);
break;


For a static control use WM_CTLCOLORSTATIC message:
Example:

HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(255, 0, 0)); //red color


Set the background mode to transparent to use the parent window's background color and return NULL_BRUSH:

SetBkMode(hdcStatic,TRANSPARENT);
return GetStockObject(NULL_BRUSH);


No comments:

Post a Comment