MFC实现透明button

       一个继承于CButton的按钮控件类,实现Button背景色与文字的共存与改变,可以自行设计背景色。
 
头文件:CMyButton.h 如下

#pragma once
#include "afxwin.h"

class CMyButton : public CButton
{
 //DECLARE_DYNAMIC(CMyButton)
public:
 CMyButton();
 virtual ~CMyButton();
 //设置Button Down的背景颜色
    void SetDownColor(COLORREF color);
    //设置Button Up的背景颜色
 void SetUpColor(COLORREF color);
 BOOL Attach(const UINT nID, CWnd* pParent);

protected:

    //必需重载的函数
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

public:
 //三种颜色分别为文字,Button Down的背景颜色,Button Up的背景颜色
 COLORREF m_TextColor, m_DownColor, m_UpColor;
};


源文件:CMyButton.cpp

#include "StdAfx.h"
#include "CMyButton.h"

CMyButton::CMyButton(void)
{
 m_DownColor = m_UpColor = RGB(0,0,0);
}

CMyButton::~CMyButton(void)
{
}


//CMyButton是CButton派生类,具有CButton的全部成员函数,

//但在创建时需要使用BS_OWNERDRAW风格。

//如果按钮不是动态生成,使用Attach函数使CMyButton代替原来按钮的窗口过程。

BOOL CMyButton::Attach(const UINT nID, CWnd* pParent)
{
 //GetDlgItem(nID)->ModifyStyle(0,BS_OWNERDRAW,0);

 if (!SubclassDlgItem(nID, pParent))
     return FALSE;

 return TRUE;
}

void CMyButton::SetDownColor(COLORREF color)
{
 m_DownColor = color;
}

void CMyButton::SetUpColor(COLORREF color)
{
 m_UpColor = color;
}


void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 // TODO: Add your code to draw the specified item
 CDC dc;
 dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
 VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);


 // 得当Button上文字,这里的步骤是:1,先得到在资源里编辑的按钮的文字,

 //然后将此文字重新绘制到按钮上,
 //同时将此文字的背景色设为透明,这样,按钮上仅会显示文字

 const int bufSize = 512;
 TCHAR buffer[bufSize];
 GetWindowText(buffer, bufSize);

 int size=strlen(buffer);   //得到长度
 DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);   //绘制文字
 SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);   //透明


 if (lpDrawItemStruct->itemState &ODS_SELECTED)  //当按下按钮时的处理
 {//
  //重绘整个控制
  CBrush brush(m_DownColor);  
  dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//

  //因为这里进行了重绘,所以文字也要重绘
  DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);   
  SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);      
 }
 else                       //当按钮不操作或者弹起时
 {
  CBrush brush(m_UpColor);     
  dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//

  //同上,进行重绘文字
  DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);    
  SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
 }

 if ((lpDrawItemStruct->itemState &ODS_SELECTED)&&(lpDrawItemStruct->itemAction &(ODA_SELECT| ODA_DRAWENTIRE)))
   //选中了本控件,高亮边框
  COLORREF fc=RGB(255-GetRValue(m_UpColor),255-GetGValue(m_UpColor), 255-             GetBValue(m_UpColor));//
  CBrush brush(fc);//
  dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);//

 }
 if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&(lpDrawItemStruct->itemAction & ODA_SELECT))
 {
  //控制的选中状态结束,去掉边框
  CBrush brush(m_UpColor);
  dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);//

 }
 dc.Detach();//
}

 

调用CMyButton类的方式:

在对话框类头文件中#include "CMyButton.h",再在对话框类中找到函数OnInitDialog()如果找不到可以在对话框事件属性中重载出来,

其中m_cbBtn变量的声明为:

 CMyButton m_cbBtn;//这句可以放在类的其他地方,只要合法就行

 //将按钮修改为BS_OWNERDRAW风格,其他风格无效

 GetDlgItem(IDC_BUTTON1)->ModifyStyle(0,BS_OWNERDRAW,0);

 //绑定控件IDC_BUTTON1与类CMyButton,响应重载函数DrawItem()
 m_cbBtn.Attach(IDC_BUTTON1,this);

 //设置Button Down的背景色
 m_cbBtn.SetDownColor(RGB(255,0,0));

 //设置Button Up的背景色
 m_cbBtn.SetUpColor(RGB(0,0,255));

 

PS:如果连接代码时在m_cbBtn.Attach(IDC_BUTTON1,this);这句产生中断,可能的原因是IDC_BUTTON1控件已经绑定了一次消息,这里再次绑定当然不成功啦,

改正方法为:将映射函数DoDataExchange()中

//DDX_Control(pDX, IDC_BUTTON1, m_cbBtn);//这句注释掉就可以了。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值