MFC 中静态编辑框(StaticEdit)、编辑框(Edit)和按钮(Button)的背景颜色、字颜色和字体

本文介绍了如何在MFC应用中修改静态编辑框(StaticEdit)、编辑框(Edit)和按钮(Button)的背景色、文字颜色及字体。通过在对话框类中添加成员变量和响应WM_CTLCOLOR消息,实现StaticEdit控件的黄色背景与蓝色文字,Edit控件的黄色背景与红色华文楷体文字,以及Button控件的绿色背景与红色文字。此外,Button的自定义背景颜色变化通过重写DrawItem方法实现。

今天主要总结一下有关MFC 中静态编辑框(StaticEdit)、编辑框(Edit)和按钮(Button)的背景颜色、字颜色和字体。

我的程序运行结果如下:MFC 修改各种控件的背景颜色、字颜色和字体-love my lifePHP

 

MFC 修改各种控件的背景颜色、字颜色和字体-love my lifePHP

由上图我们知道修改的地方有:1、把StaticEdit的背景颜色变成黄色,字体颜色变成蓝色;2、Edit的背景颜色变成黄色,字体变成红色,字体为华文楷体

3、Button的背景颜色为绿色,字体为红色。

1、对StaticEdit控件修改

在0106ChangeColorDlg.h中添加一个变量CBrush m_brush,用来保存控件的背景颜色;

对0106ChangeColorDlg添加一个响应WM_CTLCOLOR消息,在OnCtlColor函数中添加如下代码:

else if(pWnd->GetDlgCtrlID()==IDC_STA)//如果是静态编辑框

    {

        pDC->SetTextColor(RGB(,,));//修改字体的颜色

        pDC->SetBkMode(TRANSPARENT);//把字体的背景变成透明的

        return m_brush;//返回背景色

    }

2、对Edit控件修改

在OnCtlColor函数中添加如下代码:

if(pWnd->GetDlgCtrlID()==IDC_EDIT1)//如果是编辑框

    {

        pDC->SetTextColor(RGB(,,));//设置编辑框字体的颜色

        pDC->SetBkColor(RGB(,,));//设置字体背景颜色

        CFont font;

        font.CreatePointFont(,"华文楷体");

         pDC->SelectObject(&font);//设置字体

        return m_brush;

    }

3、对Button控件修改

对Button按钮修改需要通过重写DrawItem方法,所以写一个类CSXBtn,继承于CButton类。CSXBtn类实现了鼠标在button和不在button按钮时变换背景色功能。具体代码如下:

void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

{

    static int i=;

    UINT uStyle = BS_DEFPUSHBUTTON;

    // This code only works with buttons.

    ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

    // If drawing selected, add the pushed style to DrawFrameControl.

    if (lpDrawItemStruct->itemState & ODS_SELECTED)

        uStyle |= DFCS_PUSHED;

    // Draw the button frame.

    ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,

        DFC_BUTTON, uStyle);

    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

    // Get the button's text.

    CString strText;

    GetWindowText(strText);

    // Draw the button text using the text color red.

    CBrush B;

    CRect focusRect;

    focusRect.CopyRect(&lpDrawItemStruct->rcItem);

    DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);

    pDC->Draw3dRect(focusRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));

    if(m_flag)//判断鼠标是否在button按钮上

    {

        B.CreateSolidBrush(RGB(,,));

    }

    else

    {

        B.CreateSolidBrush(RGB(,,));

    }

    ::FillRect(lpDrawItemStruct->hDC,&focusRect, (HBRUSH)B.m_hObject);

    ::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

    COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(,,));

    ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),

        &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);

    ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);

}

void CSXBtn::OnMouseMove(UINT nFlags, CPoint point)

{

    // TODO: 在此添加消息处理程序代码和/或调用默认值

    m_flag=true;

    TRACKMOUSEEVENT tme;

    tme.cbSize=sizeof(tme);

    tme.dwFlags=TME_LEAVE;

    tme.hwndTrack=this->m_hWnd;

    ::_TrackMouseEvent(&tme);

    CButton::OnMouseMove(nFlags, point);

    Invalidate();

}

void CSXBtn::OnMouseLeave()

{

    // TODO: 在此添加消息处理程序代码和/或调用默认值

    m_flag=false;

    CButton::OnMouseLeave();

    Invalidate();

    UpdateWindow();

}

最后,关于WM_MOUSELEAVE的用法请参考:http://www.cnblogs.com/Simon-Sun1988/articles/4209104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值