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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM