📄 advbutton.cpp
字号:
// AdvButton.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "AdvButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXCAPTIONLEN 64
/////////////////////////////////////////////////////////////////////////////
// CAdvButton
CAdvButton::CAdvButton()
{
//initialize member variable
m_ClientRect.left = 0;
m_ClientRect.top = 0;
m_ClientRect.right = 0;
m_ClientRect.bottom= 0;
m_ClientRgn.DeleteObject();
m_ClientRgn.CreateEllipticRgnIndirect(&m_ClientRect);
m_State = 0;
m_Point.x = m_Point.y = 0;
m_IsTimerOn = FALSE;
}
CAdvButton::~CAdvButton()
{
}
BEGIN_MESSAGE_MAP(CAdvButton, CButton)
//{{AFX_MSG_MAP(CAdvButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdvButton message handlers
BOOL CAdvButton::Create(LPCTSTR lpszCaption,DWORD dwStyle,const RECT& rect,CWnd *pParentWnd,UINT nID)
{
return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
}
void CAdvButton::PreSubclassWindow()
{
//modify style
ModifyStyle(0, BS_OWNERDRAW|BS_PUSHBUTTON);
CButton::PreSubclassWindow();
}
int CAdvButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CAdvButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//get client rect
GetClientRect(&m_ClientRect);
m_ClientRgn.DeleteObject();
m_ClientRgn.CreateEllipticRgnIndirect(&m_ClientRect);
//set validate window rgn
SetWindowRgn(m_ClientRgn,FALSE);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CPen* pPen = NULL;
switch (m_State)
{
case 0:
pPen = new CPen(PS_SOLID,1,DefaultColor);
break;
case 1:
pPen = new CPen(PS_SOLID,1,FocusColor);
break;
case 2:
pPen = new CPen(PS_SOLID,1,SelectColor);
break;
case 3:
pPen = new CPen(PS_SOLID,1,DesiableColor);
break;
}
pDC->SetBkMode(TRANSPARENT);
pPen = pDC->SelectObject(pPen);
pDC->Ellipse(&m_ClientRect);
pPen = pDC->SelectObject(pPen);
if(pPen) delete pPen;
//
LPTSTR pCaption = new char[MAXCAPTIONLEN]; //
int iLen = GetWindowText(pCaption,MAXCAPTIONLEN);
pDC->SetTextColor(TextColor);
pDC->DrawText(pCaption,iLen,&m_ClientRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
void CAdvButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetWindowRect(&rect);
GetCursorPos(&m_Point);
if((rect.PtInRect(m_Point))&&(m_State != 2))
{
m_State = 2; //2:select state
Invalidate();
}
CButton::OnLButtonDown(nFlags, point);
}
void CAdvButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetWindowRect(&rect);
GetCursorPos(&m_Point);
if((rect.PtInRect(m_Point))&&(m_State != 1))
{
m_State = 1; //1:focus state
Invalidate();
}
CButton::OnLButtonUp(nFlags, point);
}
void CAdvButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(!m_IsTimerOn)
{
SetTimer(1000,100,NULL);
m_IsTimerOn = TRUE;
}
CButton::OnMouseMove(nFlags, point);
}
void CAdvButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetWindowRect(&rect);
GetCursorPos(&m_Point);
if(rect.PtInRect(m_Point))
{
if((m_State != 1)&&(m_State != 2)) //
{
m_State = 1;
Invalidate();
}
}
else
{
if(m_State != 0)
{
m_State = 0;
Invalidate();
}
KillTimer(nIDEvent);
m_IsTimerOn = FALSE;
}
CButton::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -