C++界面编程之MFC自绘制按钮

1.设置Button的颜色,正常为白色,鼠标在上面时为绿色

#pragma once
#include "afxwin.h"
class CMyButton :
    public CButton
{
public:
    CMyButton();
    ~CMyButton();
    virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
    DECLARE_MESSAGE_MAP()
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnMouseLeave();

private:
    bool m_flag;

};


/

#include "stdafx.h"
#include "CMyButton.h"


CMyButton::CMyButton()
{
    m_flag = false;
}


CMyButton::~CMyButton()
{
}


void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

    // TODO:  添加您的代码以绘制指定项
    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(255, 255, 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);
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
    ON_WM_MOUSEMOVE()
    ON_WM_MOUSELEAVE()
END_MESSAGE_MAP()


void CMyButton::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 CMyButton::OnMouseLeave()
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_flag = false;

    CButton::OnMouseLeave();
    Invalidate();
    UpdateWindow();
}

//

2.设置TabCtrl的背景色:

添加WM_ERASEBKGND消息

BOOL CMyTabCtrl::OnEraseBkgnd(CDC* pDC)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值

    //return CTabCtrl::OnEraseBkgnd(pDC);

    // TODO: Add your message handler code here and/or call default

    //获取控件矩形

    CRect   rect;

    GetClientRect(&rect);

    //创建画刷

    CBrush   brush;

    brush.CreateSolidBrush(RGB(255, 255, 255));

    //填充控件背景

    pDC->FillRect(&rect, &brush);

    //return CTabCtrl::OnEraseBkgnd(pDC);

    return true;
    return CTabCtrl::OnEraseBkgnd(pDC);

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值