65.9K
CodeProject 正在变化。 阅读更多。
Home

更改编辑控件的样式

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.90/5 (7投票s)

2008年3月29日

CPOL
viewsIcon

59946

downloadIcon

1533

本文将介绍一种简单的方法来更改编辑控件的样式。

引言

这是一种更改编辑控件样式的简单方法。我们可以在父窗口(一个对话框)中添加一个 WM_CTLCOLOR 消息处理程序,然后使用一个 switch 语句来确定编辑控件样式的更改。当有许多编辑框时,你可能会发现这样做不可取。因此,我决定编写自己的编辑类,名为 CExEdit,它继承自 CEdit

使用代码

新的类 CExEdit 提供了以下成员函数

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();

它的实现非常简单,如下面的代码所示

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 );
}

添加消息处理程序

CExEdit 类中添加以下消息处理程序

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);

首先,我们必须添加以下代码来跟踪鼠标事件,以实现编辑控件的动态样式更改。

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);
}

以下代码非常重要,我们可以通过它来更改背景颜色、文本颜色等。

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;
}
© . All rights reserved.