|
关于Push Button,已经存在两篇比较经典的文章:
http://www.csdn.net/Develop/Read_Article.asp?Id=11690
http://www.csdn.net/Develop/Read_Article.asp?Id=8432
(第二篇或http://www.csdn.net/Develop/Read_Article.asp?Id=11689,内容一样)
但是这两篇都不很完美,
第一篇里有很多错误,不进行修改没办法编译,不适合初学者,特别是得出的按钮没有文字,而第二篇只能说是一个伪代码,只是一个思路,鉴于此,特别做出一个完整的示例及代码):
首先,仍然是从CButton继承一个新类:CMyButton
头文件:
#if !defined(AFX_MYBUTTON_H__35978C4D_94AF_40FD_A82C_6DB1847775D7__INCLUDED_) #define AFX_MYBUTTON_H__35978C4D_94AF_40FD_A82C_6DB1847775D7__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // MyButton.h : header file //
///////////////////////////////////////////////////////////////////////////// // CMyButton window
class CMyButton : public CButton { // Construction //DECLARE_DYNAMIC(CMyButton)
public: CMyButton(); virtual ~CMyButton(); // Attributes public: // Operations public:
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyButton) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);//重载此函数 //}}AFX_VIRTUAL
// Implementation public: //{{AFX_MSG(CMyButton) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG
DECLARE_MESSAGE_MAP() };
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYBUTTON_H__35978C4D_94AF_40FD_A82C_6DB1847775D7__INCLUDED_)
CPP文件:
// MyButton.cpp : implementation file //
#include "stdafx.h" #include "editor.h" #include "MyButton.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CMyButton
CMyButton::CMyButton() { }
CMyButton::~CMyButton() { }
BEGIN_MESSAGE_MAP(CMyButton, CButton) //{{AFX_MSG_MAP(CMyButton) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CMyButton message handlers
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_VCENTER|DT_SINGLELINE|DT_TABSTOP); //绘制文字 SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT); //透明
if (lpDrawItemStruct->itemAction &ODA_DRAWENTIRE) //当按下按钮时的处理 {// //重绘整个控制 CBrush brush(RGB(255,0,0)); //按下去的颜色设为红色 dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);// DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,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_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_color),255-GetGValue(m_color), 255-GetBValue(m_color));// CBrush brush(fc);// dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);//
} if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&(lpDrawItemStruct->itemAction & ODA_SELECT)) { //控制的选中状态结束,去掉边框 CBrush brush(RGB(0,255,0)); dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);//
} dc.Detach();// }
新类建好好,接下来就好办了,
新建一个对话框,新建一个按钮:IDC_TEST,
同时,用类向导为其添加一个成员变量:CMyButton m_test
(记得在testDlg.h里要加上:#include "mybutton.h")
然后在testDlg.cpp里的OnInitDialog()里加上:
m_test.SubclassDlgItem(IDC_TEST,this);
同时,为按钮添加函数OnTest()
void CTestDlg::OnTest() { MessageBox("OK"); }
运行一下,OK了!
由于公司FTP无法连接,
如果需要代码工程请跟我联系:
zhucde@163.com
MSN: zhucde@hotmail.com |