drawtool.cpp

来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 84 行

CPP
84
字号
// DrawTool.cpp : implementation file
//

#include "stdafx.h"
#include "DrawToolBar.h"
#include "DrawTool.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDrawTool

CDrawTool::CDrawTool()
{
	m_HotColor     = RGB(193, 210, 238);
	m_TextColor    = RGB(0,0,255);
	m_LineColor    = RGB(49, 106, 197);
	m_HotTextColor = RGB(255,0,0);
	m_IsDraw       = TRUE;
}

CDrawTool::~CDrawTool()
{
}

void CDrawTool::OnOwnerDraw(NMHDR *pNotifyStruct, LRESULT *pResult)
{
	NMTBCUSTOMDRAW *pCustomDraw = (NMTBCUSTOMDRAW *)pNotifyStruct;
	CDC	dc;
	dc.Attach(pCustomDraw->nmcd.hdc);
	if (pCustomDraw->nmcd.uItemState &CDIS_HOT )
		pCustomDraw->clrText =m_HotTextColor;
	else
		pCustomDraw->clrText =m_TextColor;
	switch (pCustomDraw->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		*pResult = CDRF_NOTIFYITEMDRAW;
		break;
	case CDDS_ITEMPREPAINT:
		DrawButton(&dc, pCustomDraw->nmcd.rc, pCustomDraw->nmcd.uItemState);
		*pResult = TBCDRF_NOEDGES; //不绘按钮边框
		break;
	}
	dc.Detach();
}

void CDrawTool::DrawButton(CDC *pDC, const RECT &rect, UINT uState)
{
	CPoint pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	CRect rect1;
	GetClientRect(rect1);

	if (rect1.PtInRect(pt))
	{
		if (uState & CDIS_HOT)
		{
			CPen	pen(PS_SOLID, 1, m_LineColor);
			CPen	*pOldPen = pDC->SelectObject(&pen);
			CBrush	brush(m_HotColor);
			CBrush	*pOldBrush = pDC->SelectObject(&brush);
			pDC->Rectangle(&rect);
			pDC->SelectObject(pOldBrush);
			pDC->SelectObject(pOldPen);
		}
	}
}

BEGIN_MESSAGE_MAP(CDrawTool, CToolBarCtrl)
	//{{AFX_MSG_MAP(CDrawTool)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnOwnerDraw)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawTool message handlers

⌨️ 快捷键说明

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