zoukankan      html  css  js  c++  java
  • MFC 修改各种控件的背景颜色、字颜色和字体

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

    我的程序运行结果如下:

    由上图我们知道修改的地方有: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(0,0,255));//修改字体的颜色
            pDC->SetBkMode(TRANSPARENT);//把字体的背景变成透明的
            return m_brush;//返回背景色
        }

    2、对Edit控件修改

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

    if(pWnd->GetDlgCtrlID()==IDC_EDIT1)//如果是编辑框
        {
            pDC->SetTextColor(RGB(255,0,0));//设置编辑框字体的颜色
    
            pDC->SetBkColor(RGB(255,255,0));//设置字体背景颜色
            CFont font;
            font.CreatePointFont(100,"华文楷体");
             pDC->SelectObject(&font);//设置字体        
            return m_brush;
    
        }

    3、对Button控件修改

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

    void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
        static int i=0;
        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(0,255,0));
        }
        else
        {
            B.CreateSolidBrush(RGB(0,0,255));
        }
        ::FillRect(lpDrawItemStruct->hDC,&focusRect, (HBRUSH)B.m_hObject);
        ::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
        COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
        ::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

  • 相关阅读:
    cmd设置代理
    移动端坐标定位tap
    T02-Django基本应用结构
    支持向量机算法的Sklearn完整复现
    T01-何为Django,入门"Hello World"
    决策树算法的Sklearn完整复现
    逻辑回归算法的Sklearn完整复现
    线性回归算法Sklearn完整复现
    K-近邻算法的Sklearn完整复现
    数据分析中的'疑难杂症'小结(三)
  • 原文地址:https://www.cnblogs.com/Simon-Sun1988/p/4207411.html
Copyright © 2011-2022 走看看