这是一个按钮自绘的框架,其他控件也类似
//.h头文件
#pragma once
#include "afxwin.h"
#include "MemDC.h"//封装内存绘图类
class CYuButton :public CWnd
{
private:
BOOL m_bIsDown;
BOOL m_bIsMove;
BOOL _bMouseTrack;
CString m_sCaption;
CFont *m_pFont;
CString m_strFocusPic;//焦点图片
CString m_strForePic; //前景图片
CMemoryDC m_Icon_MDC;
CMemoryDC m_focus_MDC;
CMemoryDC m_fore_MDC;
public:
DECLARE_DYNCREATE(CYuButton)
CYuButton(void);
virtual ~CYuButton(void);
BOOL Create(LPCTSTR sCpation,DWORD dwStyle,CONST CRect & rt,CWnd * pParendWnd,UINT uId);
afx_msg void OnNcPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);
DECLARE_MESSAGE_MAP()
//鼠标按下
void DrawDown(CDC * pDC);
void DrawNormal(CDC * pDC);
//鼠标移动
void DrawMove(CDC * pDC);
//字体
void SetFont(CFont * pFont);
CFont * GetFont();
//获取当前程序路径
CString GetApplicationPath();
public:
//修改按钮状态
void SetNormalStatus();
};//.cpp文件
#include "StdAfx.h"
#include "YuButton.h"
CYuButton::CYuButton(void)
{
WNDCLASS wd={CS_VREDRAW|CS_HREDRAW,::DefWindowProc};
wd.lpszClassName = _T("YUButton");
}
CYuButton::~CYuButton(void)
{
}
BOOL CYuButton::Create(LPCTSTR sCaption,DWORD dwStyle,CONST CRect & rt,CWnd * pParendWnd,UINT uId)
{
LPCTSTR lpszClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)GetStockObject(LTGRAY_BRUSH), NULL);
m_sCaption = sCaption;
m_pFont = pParendWnd->GetFont();
return CWnd::Create(lpszClassName,sCaption,dwStyle|WS_CHILD,rt,pParendWnd,uId);
}
IMPLEMENT_DYNCREATE(CYuButton, CWnd)
BEGIN_MESSAGE_MAP(CYuButton, CWnd)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
void CYuButton::OnNcPaint()
{
}
BOOL CYuButton::OnEraseBkgnd(CDC* pDC)
{
return true;//CWnd::OnEraseBkgnd(pDC);
}
void CYuButton::OnPaint()
{
CPaintDC dc(this); // device context for painting
if(m_bIsDown)
DrawDown(&dc);
else
DrawNormal(&dc);
//绘按钮上面的文字
CRect rt;
GetClientRect(&rt);
rt.top = 70;
dc.SetTextColor(RGB(255,255,255));
CFont font;
VERIFY(font.CreatePointFont(115,_T("微软雅黑"), &dc));
dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(m_sCaption,rt,DT_CENTER|DT_SINGLELINE);
}
void CYuButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bIsDown = TRUE;
this->SetFocus();
this->Invalidate(FALSE);
CWnd * pWnd = this->GetParent();
if(pWnd)
pWnd->SendMessage(WM_COMMAND,GetDlgCtrlID(),(LPARAM)this->GetSafeHwnd());
CWnd::OnLButtonDown(nFlags, point);
}
void CYuButton::OnLButtonUp(UINT nFlags, CPoint point)
{
CWnd::OnLButtonUp(nFlags, point);
}
void CYuButton::DrawNormal(CDC * pDC)
{
CMemoryDC m_CompoundDC(m_focus_MDC.Width(),m_focus_MDC.Height(),pDC);//空白内存位图
m_CompoundDC.BitBlt(0,0,m_focus_MDC.Width(),m_focus_MDC.Height(),&m_focus_MDC,0,0,SRCCOPY);
m_fore_MDC.BitTrans(0,0,m_fore_MDC.Width(),m_fore_MDC.Height(),
&m_CompoundDC,0,0,RGB(135,200,241));
pDC->BitBlt(0,0,m_focus_MDC.Width(),m_focus_MDC.Height(),&m_CompoundDC,0,0,SRCCOPY);
}
void CYuButton::DrawDown(CDC * pDC)
{
//画小图标
pDC->BitBlt(0,0,m_Icon_MDC.Width(),m_Icon_MDC.Height(),&m_Icon_MDC,0,0,SRCCOPY);
}
void CYuButton::SetFont(CFont * pFont)
{
m_pFont = pFont;
}
CFont * CYuButton::GetFont()
{
return m_pFont;
}
void CYuButton::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
m_bIsMove = FALSE;
Invalidate(TRUE);
}
CString CYuButton::GetApplicationPath()
{
WCHAR buff[255]={0};
::GetModuleFileName(0,buff,255);
CString strAppFullName;
strAppFullName.Format(_T("%s"),buff);
CString strAppPath = _T("");
strAppPath = strAppFullName.Left(strAppFullName.ReverseFind(‘\\‘)+1);
return strAppPath;
}
void CYuButton::SetNormalStatus()
{
m_bIsDown = FALSE;
Invalidate(FALSE);
}原文地址:http://9233403.blog.51cto.com/9223403/1981225