📄 zgbutton.cpp
字号:
// ZgButton.cpp : implementation file
//
#include "stdafx.h"
#include "ZgButton.h"
#include "ZgTools.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZgButton
CZgButton::CZgButton()
{
m_bTracking = FALSE;
m_bOver = FALSE;
m_bDown = FALSE;
m_bFocus = FALSE;
m_bDefault = FALSE;
}
CZgButton::~CZgButton()
{
}
BEGIN_MESSAGE_MAP(CZgButton, CButton)
//{{AFX_MSG_MAP(CZgButton)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZgButton message handlers
void CZgButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
UINT uStyle = GetButtonStyle();
if (uStyle == BS_DEFPUSHBUTTON)
m_bDefault = TRUE;
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CZgButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nSaveDC =pDC->SaveDC();
UINT state = lpDrawItemStruct->itemState;
CZgMemDC dcMem(pDC, &rect);
//获取按钮的状态
m_bFocus = state & ODS_FOCUS;
m_bDown = state & ODS_SELECTED;
if (m_bDefault)
m_bFocus = TRUE;
//内边框
COLORREF crTop, crBottom;
CRect rcOver = rect;
rcOver.DeflateRect(1,1);
if (m_bDown)
{
crTop = RGB(209, 204, 193);
crBottom = RGB(218, 216, 207);
}
else if (m_bOver)
{
crTop = RGB(255, 240, 207);
crBottom = RGB(229, 151, 0);
}
else if (m_bFocus)
{
crTop = RGB(203, 231, 255);
crBottom = RGB(105, 130, 238);
}
else
{
crTop = RGB(254, 254, 254);
crBottom = RGB(226, 223, 214);
crBottom = RGB(214, 208, 197);
}
GradientFillRect(dcMem.m_hDC, rcOver, GRADIENT_FILL_RECT_V, crTop, crBottom);
//外边框
CZgPenDC pen(&dcMem, RGB(0, 60, 116));
if (state & ODS_DISABLED)
pen.Color(RGB(201, 199, 186));
DrawRoundBorder(&dcMem, rect);
//内部填充
if (!m_bDown)
{
CRect rcIn = rect;
if (m_bOver || m_bFocus)
rcIn.DeflateRect(3,3);
else
rcIn.DeflateRect(2,2);
crTop = RGB(252, 252, 251);
crBottom = RGB(236, 235, 230);
GradientFillRect(dcMem.m_hDC, rcIn, GRADIENT_FILL_RECT_V, crTop, crBottom);
}
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
//
if (m_bFocus)
{
CRect rcFoucs = rect;
dcMem.DrawFocusRect(rcFoucs);
}
//文本
CZgWindowText strTitle(this);
if (strlen(strTitle)>0)
{
CFont* hFont = GetFont();
CFont* hOldFont = dcMem.SelectObject(hFont);
CSize szExtent = dcMem.GetTextExtent(strTitle, lstrlen(strTitle));
if (strstr(strTitle, "&")!=0)
szExtent.cx -= dcMem.GetTextExtent("&").cx;
CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
if (state & ODS_SELECTED)
pt.Offset(1, 1);
int nMode = dcMem.SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
dcMem.DrawState(pt, szExtent, strTitle, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
dcMem.DrawState(pt, szExtent, strTitle, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
dcMem.SelectObject(hOldFont);
dcMem.SetBkMode(nMode);
}
}
void CZgButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (!m_bTracking)
{
TRACKMOUSEEVENT tms;
tms.cbSize = sizeof(TRACKMOUSEEVENT);
tms.dwFlags = TME_LEAVE|TME_HOVER;
tms.dwHoverTime = 1;
tms.hwndTrack = m_hWnd;
m_bTracking = ::_TrackMouseEvent(&tms);
}
CButton::OnMouseMove(nFlags, point);
}
LRESULT CZgButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bTracking = FALSE;
m_bOver = FALSE;
InvalidateRect(NULL);
return m_bTracking;
}
LRESULT CZgButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
m_bOver = TRUE;
InvalidateRect(NULL);
return m_bTracking;
}
void CZgButton::DrawRoundBorder(CDC *pDC, CRect rcBorder)
{
rcBorder.DeflateRect(0, 0, 1, 1);
CPoint pt[]={CPoint(rcBorder.left, rcBorder.top+2),
CPoint(rcBorder.left, rcBorder.bottom-2),
CPoint(rcBorder.left+1, rcBorder.bottom-1),
CPoint(rcBorder.left+2, rcBorder.bottom),
CPoint(rcBorder.right-2, rcBorder.bottom),
CPoint(rcBorder.right-1, rcBorder.bottom-1),
CPoint(rcBorder.right, rcBorder.bottom-2),
CPoint(rcBorder.right, rcBorder.top+2),
CPoint(rcBorder.right-1, rcBorder.top+1),
CPoint(rcBorder.right-2, rcBorder.top),
CPoint(rcBorder.left+2, rcBorder.top),
CPoint(rcBorder.left+1, rcBorder.top+1),
CPoint(rcBorder.left, rcBorder.top+2)};
BYTE bt[]={PT_MOVETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO,
PT_LINETO};
pDC->PolyDraw(pt, bt, 13);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -