Click here to Skip to main content
15,885,032 members
Articles / Desktop Programming / MFC

Changing the style of an edit control

Rate me:
Please Sign up or sign in to vote.
3.90/5 (7 votes)
29 Mar 2008CPOL 58.3K   1.5K   39   13
This article will tell you an easy way to change the style of an edit control.

Introduction

This is an easy way to change the style of an edit control. We can add a message handler for WM_CTLCOLOR in the parent window (a dialog) and then use a switch sentence to determine a change with the edit control style. When there are many edit boxed, you find this unadvisable. So, I decided to write my own edit class named CExEdit, which is derived from CEdit.

Using the code

The following member functions are provided by the new class CExEdit:

C++
void setTextColor( COLORREF clrText );      // set the text color of edit control
void setFontSize( int nSize );   // set size of font 
void setFontStyle( BOOL bItalic, BOOL bBold, BOOL bUnderLine );
void setBkColor( COLORREF clrBk );   //set the background color of edit control
void setFont( CString strFontName, BYTE language );  // set font name, and character set
void drawEditFrame();            //draw edit border  
void setFontHelper();

Its implementation is very easy. Like in the following code:

C++
void CExEdit::setTextColor( COLORREF clrText )
{
     m_clrText = clrText;
}
void CExEdit::setFontSize( int nSize )
{
     m_nFontSize = nSize;
}
void CExEdit::setFontStyle( BOOL bItalic, BOOL bBold, BOOL bUnderLine )
{
     m_bItalic = bItalic;
     m_bBold = bBold;
     m_bUnderLine = bUnderLine;
}
void CExEdit::setBkColor( COLORREF clrBk )
{
     m_clrBk = clrBk;
     if( m_bkBrush.m_hObject )
         m_bkBrush.DeleteObject();
 
     m_bkBrush.CreateSolidBrush( m_clrBk );
     //m_bkBrush.CreateSolidBrush( RGB( 0, 255, 255 ) );
}
void CExEdit::setFont( CString strFontName, BYTE language )
{
     m_strFontName = strFontName;
     m_language = language;
}
void CExEdit::drawEditFrame()
{
     CRect rcItem;
     CDC* pDC=this->GetDC();
     this->GetClientRect(&rcItem);
     if( !this->IsWindowEnabled() )
     {
          rcItem.InflateRect( 1, 1 );
          pDC->Draw3dRect( rcItem, RGB( 96, 96, 96),RGB( 96, 96, 96));
          rcItem.InflateRect(1,1);
          pDC->Draw3dRect( rcItem, RGB( 128, 128, 128),RGB( 128, 128, 128 ) );
          ReleaseDC( pDC );
          return;
     }
     if( m_bOver&& m_bFocus )
     {
          rcItem.InflateRect(1,1);
          pDC->Draw3dRect(rcItem,RGB( 255, 102, 51 ),RGB( 255, 102, 51 ) );
          rcItem.InflateRect(1,1);
          pDC->Draw3dRect(rcItem,RGB( 255, 163, 132 ),RGB( 255, 163, 132 ) );
     }
     if( m_bOver && !m_bFocus )
     {
          rcItem.InflateRect( 1, 1 );
          pDC->Draw3dRect( rcItem,RGB( 0, 176, 88 ),RGB( 0, 176, 88 ) );
          rcItem.InflateRect(1,1);
              pDC->Draw3dRect( rcItem,RGB( 122, 189, 32 ),RGB( 122, 189, 32 ) );
     }
     if( !m_bOver )
     {
          rcItem.InflateRect( 1, 1 );
          pDC->Draw3dRect(rcItem,RGB(102,102,153),RGB(102,102,153));
          rcItem.InflateRect( 1, 1);
          pDC->Draw3dRect(rcItem,RGB(174,174,202),RGB(174,174,202));
     }
     ReleaseDC(pDC);
}
void CExEdit::setFontHelper()
{   
    if( m_pFont->m_hObject )
        m_pFont->DeleteObject();
    LOGFONT  lgFont;
    lgFont.lfCharSet = m_language;
    lgFont.lfClipPrecision = 0;
    lgFont.lfEscapement = 0;
    strcpy( lgFont.lfFaceName, m_strFontName );
    lgFont.lfHeight = m_nFontSize;
    lgFont.lfItalic = m_bItalic;
    lgFont.lfOrientation = 0;
    lgFont.lfOutPrecision = 0;
    lgFont.lfPitchAndFamily = 2;
    lgFont.lfQuality = 0;
    lgFont.lfStrikeOut = 0;
    lgFont.lfUnderline = m_bUnderLine;
    lgFont.lfWidth = 0;
    if( m_bBold )
       lgFont.lfWeight = FW_BOLD;
    else
       lgFont.lfWeight = FW_NORMAL;
    m_pFont->CreatePointFontIndirect( &lgFont );
    SetFont( m_pFont );
}

Adding a message handler

Add the following message handler in the CExEdit class:

C++
afx_msg void OnKillfocus();
afx_msg void OnSetfocus();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseHover( WPARAM wParam, LPARAM lParam );
afx_msg LRESULT OnMouseLeave( WPARAM wParam, LPARAM lParam );
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);

First, we must add the following code to track mouse events to the dynamic style changes of the edit control.

C++
void CExEdit::OnMouseMove(UINT nFlags, CPoint point) 
{
     // TODO: Add your message handler code here and/or call default
     TRACKMOUSEEVENT mouseEvent;
 
     mouseEvent.cbSize = sizeof( TRACKMOUSEEVENT );
     mouseEvent.dwFlags = TME_HOVER | TME_LEAVE;
     mouseEvent.dwHoverTime = 0;
     mouseEvent.hwndTrack = m_hWnd;
     _TrackMouseEvent( &mouseEvent );
     CEdit::OnMouseMove(nFlags, point);
}

The following code is very important, in which we can change the background color, the text color, and others.

C++
HBRUSH CExEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
     // TODO: Change any attributes of the DC here
     drawEditFrame();
     
     pDC->SetTextColor( m_clrText );
     pDC->SetBkColor( m_clrBk );
     //pDC->SetBkMode( TRANSPARENT );
     // TODO: Return a non-NULL brush if the parent's handler should not be called
     return m_bkBrush;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
shetkarabhijeet19-Nov-12 2:25
shetkarabhijeet19-Nov-12 2:25 
BugDefault Fontname is not taken Pin
Mircosax27-Oct-11 20:40
Mircosax27-Oct-11 20:40 
Bugforgot to release the pointer (CFont) and default bckColor is not taken Pin
Mircosax26-Oct-11 23:51
Mircosax26-Oct-11 23:51 
GeneralError with multiline edit Pin
Ernst Sauer4-Apr-08 4:54
Ernst Sauer4-Apr-08 4:54 
GeneralRe: Error with multiline edit Pin
guanghui wu4-Apr-08 19:09
guanghui wu4-Apr-08 19:09 
GeneralRe: Error with multiline edit Pin
Ernst Sauer5-Apr-08 9:11
Ernst Sauer5-Apr-08 9:11 
Generalrelease version crash of your sample code Pin
chris_liush31-Mar-08 22:35
chris_liush31-Mar-08 22:35 
GeneralRe: release version crash of your sample code [modified] Pin
guanghui wu1-Apr-08 7:20
guanghui wu1-Apr-08 7:20 
GeneralBRUSH! Pin
bcde31931-Mar-08 20:29
bcde31931-Mar-08 20:29 
GeneralRe: BRUSH! Pin
Allen.fan25-Feb-09 20:11
Allen.fan25-Feb-09 20:11 
GeneralRe: BRUSH! Pin
guanghui wu1-Mar-09 22:39
guanghui wu1-Mar-09 22:39 
Thank you!
QuestionSwitch Wrap on or off? Pin
peterboulton29-Mar-08 7:21
professionalpeterboulton29-Mar-08 7:21 
Generalgood work Pin
bughoho29-Mar-08 6:46
bughoho29-Mar-08 6:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.