Thursday, May 10, 2007

change the color of an edit control

1. we can derive our own control from CEdit
2. we can change the color of an edit box using WM_CTLCOLOREDIT message


WM_CTLCOLOREDIT message code :
--------------------------------------------------------
I added the edit control to the Dialog box.

Next in the dlg.h header file, ( CDialog derived class)


I added the following code.


CEdit m_ColorEdit;

in the cpp file of the CDialog derived class add the following :

DDX_Control(pDX,IDC_EDIT1,m_ColorEdit);

(we added the member variable for the Edit control ...)


add this fn to the CDialog derived class.
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

and addthe message

ON_WM_CTLCOLOR() in the message map.

within OnOk() method,

I called the

HDC hdc = ::GetDC(m_ColorEdit.m_hWnd);

m_ColorEdit.SendMessage(WM_CTLCOLOREDIT, (WPARAM)hdc,
(LPARAM)m_ColorEdit.m_hWnd);


::ReleaseDC(m_ColorEdit.m_hWnd,editHdc);


within OnCtlColor() method add the following code :


HBRUSH CDialogAppDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

if( nCtlColor == CTLCOLOR_EDIT)
{

if(pWnd->GetDlgCtrlID() == IDC_EDIT1)
{
OutputDebugString("IDC_EDIT1 onCtlColor() fn");

hbr = CreateSolidBrush(RGB(255,140,0));
pDC->SetBkColor(RGB(255,140,0));
pDC->SetTextColor(RGB(255,255,255));
pDC->SetBkMode(OPAQUE);
}


}

we can do the same thing in Win32 as follows :

case WM_CTLCOLOREDIT :
HDC hdc = (HDC) wparam;
HWND hwnd = (HWND) lparam;
SetTextColor(hdc , RGB(255,255,255)); //Set the white as Text Color
SetBkColor(hdc,RGB(0,0,0)); //Set the black as background color
//return the background brush;

return bgBrush;
break;







Example for Someother things :

case WM_CTLCOLORDLG:
return (LONG)g_hbrBackground;
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(255, 255, 255));
SetBkMode(hdcStatic, TRANSPARENT);
return (LONG)g_hbrBackground;
}
break;



How can we dynamically create the brush with the specified color?

const COLORREF crefDarkGray = (RGB(128,128,128));


LOGBRUSH m_DGBrushStruct;
HBRUSH m_DGBrush;

m_DGBrushStruct.lbStyle = BS_SOLID;
m_DGBrushStruct.lbColor = crefDarkGray;
m_DGBrushStruct.lbHatch = 0;
m_DGBrush = ::CreateBrushIndirect (&m_DGBrushStruct);

within

case WM_CTLCOLOREDIT :
HDC hdc = (HDC) wparam;
HWND hwnd = (HWND) lparam;
SetTextColor(hdc , RGB(255,255,255)); //Set the white as Text Color
SetBkColor(hdc,RGB(0,0,0)); //Set the black as background color

//return the background brush;

return m_DGBrush;
break;



WM_CTLCOLOR Notification
The WM_CTLCOLOR message is used in 16-bit versions of Windows to change the color scheme of list boxes, the list boxes of combo boxes, button controls, edit controls, static controls, message boxes, and dialog boxes.

Note For information related to this message and 32-bit versions of Windows, see Remarks.



WM_CTLCOLOR
WPARAM wParam // Handle to a display context (DC).
LPARAM lParam; // Handle to a child window (control).

Return Value
If an application processes this message, it returns a handle to a brush. The system uses the brush to paint the background of the control.

Remarks
The WM_CTLCOLOR message has been replaced by more specific notifications in 32-bit versions of Windows. These replacements include the following:


WM_CTLCOLORBTN
WM_CTLCOLOREDIT
WM_CTLCOLORDLG
WM_CTLCOLORLISTBOX
WM_CTLCOLORSCROLLBAR
WM_CTLCOLORSTATIC

Labels:

0 Comments:

Post a Comment

<< Home