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

📄 flatbutton.cpp

📁 Visual C++高级编程及其项目应用开发(含源代码)
💻 CPP
字号:
// FlatButton.cpp : implementation file
//

#include "stdafx.h"
#include "DlgTry.h"
#include "FlatButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFlatButton

CFlatButton::CFlatButton()
{
	m_blnRaised = false;
}

CFlatButton::~CFlatButton()
{
}


BEGIN_MESSAGE_MAP(CFlatButton, CButton)
	//{{AFX_MSG_MAP(CFlatButton)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFlatButton message handlers

void CFlatButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_blnRaised = false;

	CButton::OnLButtonUp(nFlags, point);
}

void CFlatButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CButton::OnLButtonDown(nFlags, point);
}

void CFlatButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//下面这几个变量,用来获取图标的信息
	HICON	hIcon;
	ICONINFO iconinfo ;
	BITMAP bm;
	HBITMAP hBitMap;

	TEXTMETRIC textMetric;//字体信息
	
	CRect	rectWindow;
	GetWindowRect(rectWindow);	//获取按钮屏幕位置
	CPoint	ptCursor;
	GetCursorPos(&ptCursor);	//取得光标位置的屏幕坐标

	if(!rectWindow.PtInRect(ptCursor))	//光标是否在按钮客户区
	{
		CButton::OnMouseMove(nFlags, point);
		return;
	}

	if(false == m_blnRaised)	//如果没有画突起框,就开始绘制
	{
		SetTimer(1, 10, NULL);
		CDC	*pDc = GetDC();
		pDc->FillSolidRect(m_rectClient, ::GetSysColor(COLOR_BTNFACE)); //画一个亮的背景
		
		hIcon =GetIcon();
		if(hIcon)
		{
			CRect rectIcon;
			rectIcon.CopyRect(m_rectClient);
			rectIcon.DeflateRect(2, 2, 2, 2);

			GetIconInfo(hIcon, &iconinfo);
			hBitMap = iconinfo.hbmMask;
			::GetObject(hBitMap, sizeof( bm ), &bm );

			DrawIconEx(pDc->GetSafeHdc(),rectIcon.left,rectIcon.top,
				GetIcon(),bm.bmWidth, bm.bmHeight, NULL,
				(HBRUSH)NULL,DI_NORMAL);
		}
		//画3D框
		pDc->Draw3dRect(m_rectClient,::GetSysColor(COLOR_BTNHIGHLIGHT),
			::GetSysColor(COLOR_BTNSHADOW));

		if(m_blnFocus)	//如果当前有焦点,显示虚边框
		{
			pDc->DrawFocusRect(m_rectFocus);
		}
		m_blnRaised = true;

		//取得字体信息
		pDc->GetTextMetrics(&textMetric);

		this->GetWindowText(m_strCaption);
		pDc->SetBkMode(TRANSPARENT);
		pDc->TextOut((m_rectClient.Width() - m_strCaption.GetLength())/2 - 2*bm.bmWidth/3, 
			(m_rectClient.Height()-textMetric.tmHeight)/2, m_strCaption);
			
		ReleaseDC(pDc);
	}

	CButton::OnMouseMove(nFlags, point);
}

void CFlatButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	//下面这几个变量,用来获取图标的信息
	HICON	hIcon;
	ICONINFO iconinfo ;
	BITMAP bm;
	HBITMAP hBitMap;

	TEXTMETRIC textMetric;

	m_rectClient.CopyRect(&lpDrawItemStruct->rcItem);	//拷贝绘制范围给成员变量
	m_rectFocus.CopyRect(&m_rectClient);
	m_rectFocus.DeflateRect(4, 4, 4, 4);	//焦点虚线框小于绘制范围

	//刷新背景,突起状态是明亮背景,平滑和凹陷状态是灰色背景
	CDC	*pDc = CDC::FromHandle(lpDrawItemStruct->hDC);

	pDc->FillSolidRect(&m_rectClient, ::GetSysColor(COLOR_BTNFACE));

	if(lpDrawItemStruct->itemState & ODS_SELECTED) //画凹陷状态
	{
		pDc->Draw3dRect(&m_rectClient, ::GetSysColor(COLOR_BTNSHADOW),
				::GetSysColor(COLOR_BTNHIGHLIGHT));
	}
	else
	{
		pDc->Draw3dRect(&m_rectClient, ::GetSysColor(COLOR_BTNFACE),
				::GetSysColor(COLOR_BTNFACE));
	}

	hIcon = GetIcon();
	if(hIcon)
	{
		//int icoWidth,icoHeight;
		//GetIconSi
		CRect rectIcon;
		rectIcon.CopyRect(m_rectClient);
		rectIcon.DeflateRect(2, 2, 2, 2);

		GetIconInfo(hIcon, &iconinfo);
		hBitMap = iconinfo.hbmMask;
	
		::GetObject(hBitMap, sizeof( bm ), &bm );

		if(lpDrawItemStruct->itemState & ODS_SELECTED) //画凹陷状态
		{
			rectIcon.OffsetRect(1, 1);
		}

		DrawIconEx(pDc->GetSafeHdc(),rectIcon.left,rectIcon.top,
			GetIcon(),bm.bmWidth, bm.bmHeight, NULL,
			(HBRUSH)NULL,DI_NORMAL);
	}

	if(lpDrawItemStruct->itemState & ODS_FOCUS) //画凹陷状态
	{
		pDc->DrawFocusRect(m_rectFocus);
		m_blnFocus = true;
	}
	else
	{
		m_blnFocus = false;
	}

	//取得字体信息
	pDc->GetTextMetrics(&textMetric);

	this->GetWindowText(m_strCaption);
	pDc->SetBkMode(TRANSPARENT);
	pDc->TextOut((m_rectClient.Width() - m_strCaption.GetLength())/2 - 2*bm.bmWidth/3, 
		(m_rectClient.Height()-textMetric.tmHeight)/2, m_strCaption);
	
	ReleaseDC(pDc);
}

void CFlatButton::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CRect	rectWindow;
	GetWindowRect(rectWindow);
	CPoint	ptCursor;
	GetCursorPos(&ptCursor);

	if(!rectWindow.PtInRect(ptCursor))	//光标移出客户区,关闭定时器
	{
		CButton::OnTimer(nIDEvent);
		KillTimer(1);	//关闭定时器
		InvalidateRect(NULL);	//间接调用DrawItem()函数,恢复平滑状态
		m_blnRaised = false;
		return;
	}

	CButton::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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