读书人

MFC Button 的重绘 重绘的旋钮消失

发布时间: 2013-11-20 12:46:02 作者: rapoo

MFC 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;

};

CMButton.cpp:
#include "StdAfx.h"

#include "MyButton.h"

CMyButton::CMyButton(void)

{

m_DownColor = m_UpColor = RGB(0,0,0);//初始化设为黑色

}

CMyButton::~CMyButton(void)

{

}



BOOL CMyButton::Attach(const UINT nID,CWnd* pParent)

{

if (!SubclassDlgItem(nID, pParent))

return FALSE;

return TRUE;

}

void CMyButton::SetDownColor(COLORREF color)

{ //CMyButton类的函数

m_DownColor = color;

}

void CMyButton::SetUpColor(COLORREF color)

{

m_UpColor = color;

}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

{

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=sizeof(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);//利用画刷brush,填充矩形框

//因为这里进行了重绘,所以文字也要重绘

DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);

SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

}

else//当按钮不操作或者弹起时

{

CBrush brush(RGB(255,255,0));

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);//用画刷brush,填充矩形边框

}

if (!(lpDrawItemStruct->itemState &ODS_SELECTED) &&(lpDrawItemStruct->itemAction & ODA_SELECT))

{

CBrush brush(m_UpColor); //控制的选中状态结束,去掉边框

dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);//}

dc.Detach();

}


}


初始化:

CMyButton m_Btn;//定义一个CMybutton的变量,可以在其他地方进行定义,只需要包含 “CMyButton.h” 即可



//将按钮修改为BS_OWNERDRAW风格,允许button的采用自绘模式


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




//绑定控件IDC_BUTTON1与类CMyButton,响应重载函数DrawItem()

m_Btn.Attach(IDC_BUTTON1,this);
//设置Button Down的背景色,SetDownColor()和SetUpnColor()是CMyButton类中的析构函数

m_Btn.SetDownColor(RGB(255,255,255));




//设置Button Up的背景色

m_Btn.SetUpColor(RGB(255,255,255));
AfxMessageBox(_T("初始化成功"));



说明:1.网上的strlen(CMButton.cpp int size=sizeof(buffer);//得到长度) 因为unicode的 问题,我改为了sizeof不知道有没有问题?
2.在不加AfxMessageBox(_T("初始化成功"));我的IDC_BUTTON1消失,加了后会有颜色,但文字是乱码,点击确定后一样消失

控件 mfc Button? 类
[解决办法]
消失是 onsize resize 之类的函数没有没有实现吧
乱码的话 看看用多字符集行不行
[解决办法]
//设置Button Up的背景色

m_Btn.SetUpColor(RGB(255,255,255));
AfxMessageBox(_T("初始化成功"));
在哪个函数里?

读书人网 >C++

热点推荐