📄 mybutton.cpp
字号:
// MyButton.cpp : 实现文件
//
#include "stdafx.h"
#include "MyButton.h"
// CMyButton
IMPLEMENT_DYNAMIC(CMyButton, CButton)
CMyButton::CMyButton()
{
}
CMyButton::CMyButton(HWND hWnd)
{
m_hParent=hWnd;
m_bSelect=0;
m_bFocus=0;
m_bPaint=0;
m_hTheme=0;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_NCHITTEST()
ON_WM_MOUSEMOVE()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_CREATE()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// CMyButton 消息处理程序
int CMyButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
m_hTheme=OpenThemeData(m_hWnd,L"BUTTON");
VERIFY(m_hTheme!=0);
return 0;
}
void CMyButton::OnDestroy()
{
CButton::OnDestroy();
// TODO: 在此添加消息处理程序代码
CloseThemeData(m_hTheme);
m_hTheme=0;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
UINT itemAction=lpDrawItemStruct->itemAction;
UINT itemState=lpDrawItemStruct->itemState;
RECT rcItem=lpDrawItemStruct->rcItem;
UINT CtlID=lpDrawItemStruct->CtlID;
HDC hDC=lpDrawItemStruct->hDC;
TCHAR title[80],tip[80];
RECT cntItem;
HRESULT hr;
int state;
BOOL br;
USES_CONVERSION;
GetWindowText(title,80);
sprintf(tip," %s ",title);
switch(itemAction)
{
case ODA_DRAWENTIRE:
DrawSelectState(itemState,state);
DrawFocusState(itemState,state);
break;
case ODA_SELECT:
DrawSelectState(itemState,state);
DrawFocusState(itemState,state);
break;
case ODA_FOCUS:
DrawFocusState(itemState,state);
break;
default:
break;
}
hr=DrawThemeBackground(m_hTheme,hDC,BP_PUSHBUTTON,state,&rcItem,0);
hr=GetThemeBackgroundContentRect(m_hTheme,hDC,BP_PUSHBUTTON,state,&rcItem,&cntItem);
hr=DrawThemeText(m_hTheme,hDC,BP_PUSHBUTTON,state,T2W(tip),lstrlen(title),
DT_CENTER|DT_VCENTER|DT_SINGLELINE,0,&cntItem);
if(itemState&ODS_FOCUS) br=DrawFocusRect(hDC,&cntItem);
}
void CMyButton::DrawSelectState(UINT itemState,int& state)
{
if(itemState&ODS_SELECTED)
{
state=PBS_PRESSED;
m_bSelect=1;
}
else //未选中
{
state=PBS_NORMAL;
m_bSelect=0;
}
}
void CMyButton::DrawFocusState(UINT itemState,int& state)
{
if(itemState&ODS_FOCUS)
{
m_bFocus=1;
if(itemState&ODS_SELECTED)
{
state=PBS_PRESSED;
m_bSelect=1;
}
else //无焦点
{
state=PBS_HOT;
m_bSelect=0;
}
}
else m_bFocus=0;
}
UINT CMyButton::OnNcHitTest(CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return(HTCLIENT);
// return CButton::OnNcHitTest(point);
}
LRESULT CMyButton::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
if(WM_MOUSELEAVE==message)
{
m_bPaint=0;
if(1==m_bFocus) DrawButtonEdge(PBS_DEFAULTED);
else DrawButtonEdge(PBS_NORMAL);
}
return CButton::WindowProc(message, wParam, lParam);
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_hTheme&&!m_bPaint)
{
m_bPaint=1;
InitTrackEvent(m_hWnd,TME_LEAVE);
if(1==m_bSelect) DrawButtonEdge(PBS_PRESSED);
else DrawButtonEdge(PBS_HOT);
}
CButton::OnMouseMove(nFlags, point);
}
BOOL CMyButton::InitTrackEvent(HWND hWnd,DWORD flags)
{
TRACKMOUSEEVENT tme;
BOOL br;
ZeroMemory(&tme,sizeof(TRACKMOUSEEVENT));
tme.cbSize=sizeof(TRACKMOUSEEVENT);
tme.dwFlags=flags;
tme.hwndTrack=hWnd;
tme.dwHoverTime=HOVER_DEFAULT;
br=TrackMouseEvent(&tme);
return(br);
}
BOOL CMyButton::DrawButtonEdge(int state)
{
TCHAR title[80],tip[80];
RECT rcItem,cntItem;
HRESULT hr;
BOOL br;
HDC hDC;
USES_CONVERSION;
GetWindowText(title,80);
sprintf(tip," %s ",title);
hDC=::GetDC(m_hWnd);
GetClientRect(&rcItem);
hr=DrawThemeBackground(m_hTheme,hDC,BP_PUSHBUTTON,state,&rcItem,0);
hr=GetThemeBackgroundContentRect(m_hTheme,hDC,BP_PUSHBUTTON,state,&rcItem,&cntItem);
hr=DrawThemeText(m_hTheme,hDC,BP_PUSHBUTTON,state,T2W(tip),lstrlen(title),
DT_CENTER|DT_VCENTER|DT_SINGLELINE,0,&cntItem);
if(PBS_DEFAULTED==(state&PBS_DEFAULTED)||m_bFocus) br=DrawFocusRect(hDC,&cntItem);
br=::ReleaseDC(m_hWnd,hDC);
hDC=0;
return(br);
}
HBRUSH CMyButton::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: 在此更改 DC 的任何属性
// TODO: 如果不应调用父级的处理程序,则返回非空画笔
if(m_hTheme&&!m_bPaint)
{
m_bPaint=1;
InitTrackEvent(m_hWnd,TME_LEAVE);
if(1==m_bFocus) DrawButtonEdge(PBS_DEFAULTED);
else DrawButtonEdge(PBS_NORMAL);
}
pDC->SetBkMode(TRANSPARENT);
return((HBRUSH)GetStockObject(WHITE_BRUSH));
// return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -