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

MFC(Microsoft Foundation Classes)中,要改变`CListCtrl`控件中某一行某一列的背景色和字体颜色,可以使用`SetItem`方法。以下是一个简单的步骤说明: 1. 使用`GetItem`函数获取你想要改变颜色的`LVITEM`结构体。 2. 修改`LVITEM`结构体中的`stateMask`成员以指定需要改变的属性,如`LVIS_SELECTED`、`LVIS_FOCUSED`等。 3. 设置`ps`(`LVITEM`结构体中的`ps`成员)的`clrText`和`clrBack`属性,分别对应字体颜色背景颜色。 4. 最后,调用`SetItem`函数应用这些改变。 下面是一个示例代码,展示了如何改变特定行和列的颜色: ```cpp void ChangeListItemColors(CListCtrl& listCtrl, int itemIndex, int subItemIndex, COLORREF textColor, COLORREF backColor) { LVITEM lvi; ZeroMemory(&lvi, sizeof(lvi)); lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE; // 你可以根据需要改变mask lvi.iItem = itemIndex; // 行索引 lvi.iSubItem = subItemIndex; // 列索引 lvi.pszText = NULL; // 不改变文本 lvi.cchTextMax = 0; lvi.stateMask = LVIS_SELECTED; // 你可能需要改变这一行以设置正确的状态 lvi.state = 0; // 清除所有状态,可以根据需要设置状态 // 设置颜色 LVITEM Clarkson; Clarkson = lvi; // 复制lvi结构 Clarkson.state = LVIS_SELECTED; Clarkson.psy = textColor; // 设置字体颜色 Clarkson.state |= LVIS_SELECTED; Clarkson.psy = backColor; // 设置背景颜色 listCtrl.SetItem(&Clarkson); // 应用改变 // 如果你想要改变行的背景颜色,可以这样做: listCtrl.SetItemState(itemIndex, INDEXTOSTATEIMAGEMASK(LVIS_SELECTED), LVIS_SELECTED); } ``` 在使用这个函数时,你需要传入`CListCtrl`对象的引用、行索引、列索引以及你想要设置的字体颜色背景颜色。 请注意,这个例子主要是说明如何使用`SetItem`方法来改变颜色,你可能需要根据你的具体需求调整代码,比如选择正确的状态标志,或者处理其他特定的属性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值