Visual Studio 6Visual C++ 7.0Windows 2000Visual C++ 6.0Windows XPMFCIntermediateDevVisual StudioWindowsC++
A focus-sensitive EditBox Class






2.42/5 (9 votes)
May 20, 2003

60760

2394
Change the back color of an edit box when it gets the focus, and back to another color when focus is lost
Introduction
I wanted a focus-sensitive Editbox
to make my GUI program more attractive. I found one in codetools, which was distributed by Warren J. Hebert. However I downloaded it and found it worked perfectly only when the Editbox
is single line style. But my application wants the EditBox
to be a multi-line .So I wrote one! This is CEditEx
with the features below:
- Change the back color of an edit box when it gets the focus, and back when it loses the focus.
- Use the flat scroll bar instead of the default behavior.
The Implemention of this class:
The following snippets are used to track to mouseCEditEx::OnMouseMove(UINT nFlags, CPoint point) { CEdit::OnMouseMove(nFlags, point); if (m_bIsFocused) return; if (GetCapture() != this) { // come in for the first time m_bMouseOver = TRUE; SetCapture(); Invalidate(TRUE); } else { CRect rect; GetClientRect(&rect); if (!rect.PtInRect(point)) { //Mouse move out of Edit control m_bMouseOver = FALSE; Invalidate(TRUE); ReleaseCapture(); } } } void CEditEx::OnSetFocus(CWnd* pOldWnd) { CEdit::OnSetFocus(pOldWnd); m_bMouseOver=TRUE; Invalidate(TRUE); } void CEditEx::OnKillFocus(CWnd* pNewWnd) { CEdit::OnKillFocus(pNewWnd); m_bMouseOver=FALSE; Invalidate(TRUE); }
Change the scrollbar appearance // In the Message map ON_CONTROL_REFLECT(EN_VSCROLL, OnVscroll) void CEditEx::OnVscroll() { InitializeFlatSB(GetSafeHwnd()); // Use the flat scrollbar feature provided by Microsoft. // See MSDN for this API }
//Change the background color of EditBox according to different contex. // In the Message map ON_WM_CTLCOLOR_REFLECT() CEditEx::CEditEx() { m_bMouseOver = FALSE; brHot.CreateSolidBrush(RGB(255, 255, 255)); br.CreateSolidBrush(RGB(221, 221, 221)); } HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor) { if (m_bMouseOver) { pDC->SetBkColor(RGB(255, 255, 255)); return brHot; } else { pDC->SetBkColor(RGB(221, 221, 221)); return br; } }
To use the CEditEx
class, you should do as follows:
- Add an
EditBox
into your application. Change the style to be multi-line, No border, Want Return, Vertical Scrollbar. - In your application add a member variable derived from
CEditEx
. See, for example testDlg.cpp:
CEditEx m_ctlEdit1;