⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 advbutton.cpp

📁 实现了一个简单的个人事务助理的工具
💻 CPP
字号:
// AdvButton.cpp : implementation file
//

#include "stdafx.h"
#include "MyAssistant.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;
	m_BtnType = 0;
	m_IsPushed = 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_FLAT|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);

	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

	if ( m_BtnType == 0) {
		CRect rc0(m_ClientRect.left+1,
				  m_ClientRect.top+1,
				  m_ClientRect.right-1,
				  m_ClientRect.bottom-1);
		CRect rc1(m_ClientRect.left+2,
				  m_ClientRect.top+2,
				  m_ClientRect.right-2,
				  m_ClientRect.bottom-2);
		CRect rc2(m_ClientRect.left+8,
				  m_ClientRect.top+8,
				  m_ClientRect.right-8,
				  m_ClientRect.bottom-8);
		if(m_IsPushed)
		{
			pDC->Rectangle(&rc0);
			pDC->FillRect(&rc1,new CBrush(WHITE));
			if(m_State == 1) {
				pDC->FillRect(&rc2,new CBrush(SYSCOLOR));
			}
		}
		else
		{
			pDC->Rectangle(&m_ClientRect);
			pDC->FillRect(&rc1,new CBrush(SYSCOLOR));
			if(m_State == 1) {
				pDC->FillRect(&rc2,new CBrush(WHITE));
			}
		}

		LPTSTR pCaption = new char[MAXCAPTIONLEN];	//
		int iLen = GetWindowText(pCaption,MAXCAPTIONLEN);
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(0,0,0));
		pDC->DrawText(pCaption,iLen,&m_ClientRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	else if ( m_BtnType == 1 ) {
	}
	else if ( m_BtnType == 2 ) {
	}
}

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);
}

void CAdvButton::SetType(UINT piType)
{
	m_BtnType = piType;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -