VC++ 2010中实现自定义窗口按钮控件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
CCustomButton::CCustomButton()
{
}
CCustomButton::~CCustomButton()
{
}
BEGIN_MESSAGE_MAP(CCustomButton, CButton)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
}
void CCustomButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO:在此添加消息处理程序代码和/或调用默认值
m_IsPressed = TRUE;
CButton::OnLButtonDown(nFlags, point);
}
BOOL CCustomButton::OnEraseBkgnd(CDC* pDC)
}
else
{
CPen pen(PS_SOLID,2,RGB(5,255,0));
dc.SelectObject(&pen);
HRGN rgn = CreateEllipticRgn(0,0,rect.Width(),rect.Height());
SetWindowRgn(rgn,TRUE);
dc.Ellipse(0,0,rect.Width(),rect.Height());
public:
CCustomButton();
virtual~CCustomButton();
BOOL m_IsPressed;
protected:
DECLARE_MESSAGE_MAP()
public:
virtualvoidDrawItem(LPDRAWITEMSTRUCT/*lpDrawItemStruct*/);
// TODO:添加您的代码以绘制指定项
}
void CCustomButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO:在此添加消息处理程序代码和/或调用默认值
m_IsPressed = FALSE;
CButton::OnLButtonUp(nFlags, point);
END_MESSAGE_MAP()
// CCustomButton消息处理程序
void CCustomButton::DrawItem(LPDRAWITEMSቤተ መጻሕፍቲ ባይዱRUCT lpDrawItemStruct)
{
CRect rect;
GetClientRect(rect);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.SetBkMode(TRANSPARENT);
CBrush m_Brush;
m_Brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&m_Brush);
if(m_IsPressed)
{
CPen pen(PS_SOLID,2,RGB(255,2,0));
{
// TODO:在此添加消息处理程序代码和/或调用默认值
return TRUE;
//return CButton::OnEraseBkgnd(pDC);
}
4、最后改写WM_ERASEBKGND禁止窗口重画
然后关联控件变量
到此为止,我们已经我们完成了自定义按钮控江
afx_msgvoidOnLButtonUp(UINT nFlags, CPoint point);
afx_msgvoidOnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
3.。重写方法WM_BUTTONDOWN WM_LBUTTONUP
VC++ 2010中实现自定义窗口按钮控件
1.新建一个MFC类使得它继承与CButton类;
2.
3
2.在生成的类中添加变量
#pragmaonce
// CCustomButton
classCCustomButton :publicCButton
{
DECLARE_DYNAMIC(CCustomButton)
dc.SetTextColor(RGB(120,0,190));
}
CString str;
GetWindowText(str);
dc.DrawText(str,CRect(0,0,rect.Width(),rect.Height()),DT_CENTER|DT_VCENTER|DT_SINGLELINE);
dc.SelectObject(&pen);
HRGN rgn = CreateEllipticRgn(0,0,rect.Width(),rect.Height());
SetWindowRgn(rgn,TRUE);
dc.Ellipse(0,0,rect.Width(),rect.Height());
dc.SetTextColor(RGB(0,0,255));
// CustomButton.cpp :实现文件
//
#include "stdafx.h"
#include "just test.h"
#include "CustomButton.h"
// CCustomButton
IMPLEMENT_DYNAMIC(CCustomButton, CButton)
{
}
CCustomButton::~CCustomButton()
{
}
BEGIN_MESSAGE_MAP(CCustomButton, CButton)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
}
void CCustomButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO:在此添加消息处理程序代码和/或调用默认值
m_IsPressed = TRUE;
CButton::OnLButtonDown(nFlags, point);
}
BOOL CCustomButton::OnEraseBkgnd(CDC* pDC)
}
else
{
CPen pen(PS_SOLID,2,RGB(5,255,0));
dc.SelectObject(&pen);
HRGN rgn = CreateEllipticRgn(0,0,rect.Width(),rect.Height());
SetWindowRgn(rgn,TRUE);
dc.Ellipse(0,0,rect.Width(),rect.Height());
public:
CCustomButton();
virtual~CCustomButton();
BOOL m_IsPressed;
protected:
DECLARE_MESSAGE_MAP()
public:
virtualvoidDrawItem(LPDRAWITEMSTRUCT/*lpDrawItemStruct*/);
// TODO:添加您的代码以绘制指定项
}
void CCustomButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO:在此添加消息处理程序代码和/或调用默认值
m_IsPressed = FALSE;
CButton::OnLButtonUp(nFlags, point);
END_MESSAGE_MAP()
// CCustomButton消息处理程序
void CCustomButton::DrawItem(LPDRAWITEMSቤተ መጻሕፍቲ ባይዱRUCT lpDrawItemStruct)
{
CRect rect;
GetClientRect(rect);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.SetBkMode(TRANSPARENT);
CBrush m_Brush;
m_Brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&m_Brush);
if(m_IsPressed)
{
CPen pen(PS_SOLID,2,RGB(255,2,0));
{
// TODO:在此添加消息处理程序代码和/或调用默认值
return TRUE;
//return CButton::OnEraseBkgnd(pDC);
}
4、最后改写WM_ERASEBKGND禁止窗口重画
然后关联控件变量
到此为止,我们已经我们完成了自定义按钮控江
afx_msgvoidOnLButtonUp(UINT nFlags, CPoint point);
afx_msgvoidOnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
3.。重写方法WM_BUTTONDOWN WM_LBUTTONUP
VC++ 2010中实现自定义窗口按钮控件
1.新建一个MFC类使得它继承与CButton类;
2.
3
2.在生成的类中添加变量
#pragmaonce
// CCustomButton
classCCustomButton :publicCButton
{
DECLARE_DYNAMIC(CCustomButton)
dc.SetTextColor(RGB(120,0,190));
}
CString str;
GetWindowText(str);
dc.DrawText(str,CRect(0,0,rect.Width(),rect.Height()),DT_CENTER|DT_VCENTER|DT_SINGLELINE);
dc.SelectObject(&pen);
HRGN rgn = CreateEllipticRgn(0,0,rect.Width(),rect.Height());
SetWindowRgn(rgn,TRUE);
dc.Ellipse(0,0,rect.Width(),rect.Height());
dc.SetTextColor(RGB(0,0,255));
// CustomButton.cpp :实现文件
//
#include "stdafx.h"
#include "just test.h"
#include "CustomButton.h"
// CCustomButton
IMPLEMENT_DYNAMIC(CCustomButton, CButton)