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

📄 macsliderctrl.cpp

📁 无线图象监控系统(用VC++编程)用数据库ACCESS存储图象
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MacSliderCtrl.cpp : implementation file
//
//	CMacSliderCtrl class, version 1.0
//
//	Copyright (c) 1999 Paul M. Meidinger (pmmeidinger@yahoo.com)
//
// Feel free to modifiy and/or distribute this file, but
// do not remove this header.
//
// I would appreciate a notification of any bugs discovered or 
// improvements that could be made.
//
// This file is provided "as is" with no expressed or implied warranty.
//
//	History:
//		PMM	12/21/1999		Initial implementation.		

#include "stdafx.h"
#include "MacSliderCtrl.h"

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

// Funtion prototypes.
COLORREF LightenColor(const COLORREF crColor, BYTE byIncreaseVal);
COLORREF DarkenColor(const COLORREF crColor, BYTE byReduceVal);

//-------------------------------------------------------------------
//
COLORREF CMacSliderCtrl::LightenColor(const COLORREF crColor, BYTE byIncreaseVal)
//
// Return Value:	None.
//
// Parameters	:	crColor - References a COLORREF structure.
//						byReduceVal - The amount to reduce the RGB values by.
//
// Remarks		:	Lightens a color by increasing the RGB values by the given number.
//
{
	BYTE byRed = GetRValue(crColor);
	BYTE byGreen = GetGValue(crColor);
	BYTE byBlue = GetBValue(crColor);

	if ((byRed + byIncreaseVal) <= 255)
		byRed = BYTE(byRed + byIncreaseVal);
	if ((byGreen + byIncreaseVal)	<= 255)
		byGreen = BYTE(byGreen + byIncreaseVal);
	if ((byBlue + byIncreaseVal) <= 255)
		byBlue = BYTE(byBlue + byIncreaseVal);

	return RGB(byRed, byGreen, byBlue);
}	// LightenColorref

//-------------------------------------------------------------------
//
COLORREF CMacSliderCtrl::DarkenColor(const COLORREF crColor, BYTE byReduceVal)
//
// Return Value:	None.
//
// Parameters	:	crColor - References a COLORREF structure.
//						byReduceVal - The amount to reduce the RGB values by.
//
// Remarks		:	Darkens a color by reducing the RGB values by the given number.
//
{
	BYTE byRed = GetRValue(crColor);
	BYTE byGreen = GetGValue(crColor);
	BYTE byBlue = GetBValue(crColor);

	if (byRed >= byReduceVal)
		byRed = BYTE(byRed - byReduceVal);
	if (byGreen >= byReduceVal)
		byGreen = BYTE(byGreen - byReduceVal);
	if (byBlue >= byReduceVal)
		byBlue = BYTE(byBlue - byReduceVal);

	return RGB(byRed, byGreen, byBlue);
}	// DarkenColorref

/////////////////////////////////////////////////////////////////////////////
// CMacSliderCtrl

//-------------------------------------------------------------------
//
CMacSliderCtrl::CMacSliderCtrl()
//
// Return Value:	None.
//
// Parameters	:	None.
//
// Remarks		:	Standard constructor.
//
{
	m_crThumb = ::GetSysColor(COLOR_3DFACE);
	m_crChannel = ::GetSysColor(COLOR_WINDOW);
	m_crSelection = ::GetSysColor(COLOR_HIGHLIGHT);
	GetColors();
	CreatePens();
}	// CMacSliderCtrl

//-------------------------------------------------------------------
//
CMacSliderCtrl::~CMacSliderCtrl()
//
// Return Value:	None.
//
// Parameters	:	None.
//
// Remarks		:	Destructor.
//
{
	DeletePens();
}	// ~CMacSliderCtrl


BEGIN_MESSAGE_MAP(CMacSliderCtrl, CSliderCtrl)
	//{{AFX_MSG_MAP(CMacSliderCtrl)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMacSliderCtrl message handlers

//-------------------------------------------------------------------
//
void CMacSliderCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) 
//
// Return Value:	None.
//
// Parameters	:	pNMHDR - Used to get a pointer to a NM_CUSTOMDRAW structure.
//						pResult - Will receive a value which depends on the current
//							drawing stage.
//
// Remarks		:	Sent by the slider control to notify the parent window 
//						about drawing operations. This notification is sent in 
//						the form of a WM_NOTIFY message.
//
{
	LPNMCUSTOMDRAW lpcd = (LPNMCUSTOMDRAW)pNMHDR;

	// CDDS_PREPAINT is at the beginning of the paint cycle. You 
	// implement custom draw by returning the proper value. In this 
	// case, we're requesting item-specific notifications.
	if (lpcd->dwDrawStage == CDDS_PREPAINT)
	{
		// Request prepaint notifications for each item.
		*pResult = CDRF_NOTIFYITEMDRAW;         
		return;
	}

	// Because we returned CDRF_NOTIFYITEMDRAW in response to
	// CDDS_PREPAINT, CDDS_ITEMPREPAINT is sent when the control is
	// about to paint an item.
	if (lpcd->dwDrawStage == CDDS_ITEMPREPAINT)
	{	
		CDC *pDC = CDC::FromHandle(lpcd->hdc);
		CRect rect(lpcd->rc);
		int nSavedDC = pDC->SaveDC();

		if (lpcd->dwItemSpec == TBCD_TICS)
		{
			*pResult = CDRF_DODEFAULT;
			return;
		}	// if drawing tics
		else if (lpcd->dwItemSpec ==  TBCD_THUMB)
		{
			// If the slider has been clicked on (selected) darken
			// the thumb. Save the thumb color, in case it is changed.
			COLORREF crSavedThumb = m_crThumb;

			if (lpcd->uItemState & CDIS_SELECTED)
			{
				m_crThumb = DarkenColor(m_crThumb, 51);
				GetColors();
				CreatePens();
			}

			if (rect.Height() > rect.Width())
				DrawHorizontalThumb(pDC, rect);
			else
				DrawVerticalThumb(pDC, rect);

			// Restore the thumb color.
			if (lpcd->uItemState & CDIS_SELECTED)
			{
				m_crThumb = crSavedThumb;
				GetColors();
				CreatePens();
			}
		}	// if drawing thumb
		else if (lpcd->dwItemSpec == TBCD_CHANNEL)
		{
			// If the slider is disabled, change the thumb
			// to the current face color.
			COLORREF crSavedChannel = m_crChannel;
			if (!IsWindowEnabled())
			{
				m_crChannel = ::GetSysColor(COLOR_3DFACE);
			}

			CRect rcThumb;
			GetThumbRect(rcThumb);
			DrawChannel(pDC, rect, rcThumb.Height() > rcThumb.Width());

			m_crChannel = crSavedChannel;
		}	// if drawing channel

		pDC->RestoreDC(nSavedDC);

		*pResult = CDRF_SKIPDEFAULT;
		return;
	}

	*pResult = 0;
}	// OnCustomDraw

//-------------------------------------------------------------------
//
void CMacSliderCtrl::DrawHorizontalThumb(CDC *pDC, const CRect &rect)
//
// Return Value:	None.
//
// Parameters	:	pDC - Specifies a device context.
//						rect - The rectangle for the thumb.
//
// Remarks		:	Draws a thumb for a horizontal slider control.
//
{
	CRect rc(rect);
	int nWidth = rect.Width();
	int nMid = nWidth >> 1;

	CPen penFrame(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOWFRAME));
	CPen *pOldPen = pDC->SelectObject(&penFrame);	
	CBrush br(m_crThumb);
	CBrush *pOldBrush = pDC->SelectObject(&br);

	DWORD dwStyle = GetStyle();

	// No points on thumb.
	if (dwStyle & TBS_BOTH)
	{
		pDC->RoundRect(rc, CPoint(2, 2));
		rc.DeflateRect(1, 1, 1, 1);
		pDC->Draw3dRect(rc, m_crThumbLight, m_crThumbDark);

		pDC->SetPixel(rc.left, rc.top, m_crThumbLighter);
		pDC->SetPixel(rc.right - 1, rc.top, m_crThumb);
		pDC->SetPixel(rc.right - 1, rc.bottom - 1, m_crThumbDarker);
		pDC->SetPixel(rc.left, rc.bottom - 1, m_crThumb);
	}
	// Point on top.
	else if (dwStyle & TBS_TOP)
	{
		pDC->MoveTo(rc.left, rc.top + nMid - 1);
		pDC->LineTo(rc.left, rc.bottom - 2);
		pDC->LineTo(rc.left + 1, rc.bottom - 1);
		pDC->LineTo(rc.right - 2, rc.bottom - 1);
		pDC->LineTo(rc.right - 1, rc.bottom - 2);
		pDC->LineTo(rc.right - 1, rc.top + nMid - 1);
		pDC->LineTo(rc.right - nMid, rc.top);
		pDC->LineTo(rc.left + nMid - 1, rc.top);
		pDC->LineTo(rc.left, rc.top + nMid - 1);

		pDC->FloodFill(rc.left + nMid, rc.top + nMid, ::GetSysColor(COLOR_WINDOWFRAME));

		if (nWidth > 5)
		{
			pDC->SelectObject(&m_penThumbLight);
			pDC->MoveTo(rc.left + 1, rc.top + nMid);
			pDC->LineTo(rc.left + 1, rc.bottom - 2);
			pDC->MoveTo(rc.right - nMid, rc.top + 1);
			pDC->LineTo(rc.right - nMid - 2, rc.top + 1);

			pDC->SelectObject(&m_penThumbLighter);
			pDC->MoveTo(rc.left + 1, rc.top + nMid - 1);
			pDC->LineTo(rc.left + nMid, rc.top);


			pDC->SelectObject(&m_penThumbDark);
			pDC->MoveTo(rc.left + 2, rc.bottom - 2);
			pDC->LineTo(rc.right - 2, rc.bottom - 2);
			pDC->LineTo(rc.right - 2, rc.bottom - 2);
			pDC->LineTo(rc.right - 2, rc.top + nMid - 1);
			pDC->LineTo(rc.right - nMid, rc.top + 1);
		}

		rc.DeflateRect(1, 2, 1, 0);
	}	// if point at top of thumb
	// Point is on bottom.
	else
	{
		pDC->MoveTo(rc.left, rc.top + 1);
		pDC->LineTo(rc.left, rc.bottom - nMid);
		pDC->LineTo(rc.left + nMid - 1, rc.bottom - 1);
		pDC->LineTo(rc.right - nMid, rc.bottom - 1);
		pDC->LineTo(rc.right - 1, rc.bottom - nMid);
		pDC->LineTo(rc.right - 1, rc.top + 1);
		pDC->LineTo(rc.right - 2, rc.top);
		pDC->LineTo(rc.left, rc.top);

		pDC->FloodFill(rc.left + nMid, rc.top + nMid, ::GetSysColor(COLOR_WINDOWFRAME));

		if (nWidth > 5)
		{
			pDC->SetPixel(rc.left + 1, rc.top + 1, m_crThumbLighter);

			pDC->SelectObject(&m_penThumbLight);
			pDC->MoveTo(rc.left + 1, rc.top + 2);
			pDC->LineTo(rc.left + 1, rc.bottom - nMid + 1);
			pDC->MoveTo(rc.left + 2, rc.top + 1);
			pDC->LineTo(rc.right - 2, rc.top + 1);

			pDC->SelectObject(&m_penThumbDark);
			pDC->MoveTo(rc.left + 2, rc.bottom - nMid + 1);
			pDC->LineTo(rc.left + nMid - 1, rc.bottom - 2);
			pDC->LineTo(rc.right - nMid, rc.bottom - 2);
			pDC->MoveTo(rc.right - 2, rc.bottom - nMid - 1);
			pDC->LineTo(rc.right - 2, rc.top + 1);

			pDC->SelectObject(&m_penThumbDarker);
			pDC->MoveTo(rc.right - nMid, rc.bottom - 2);
			pDC->LineTo(rc.right - 1, rc.bottom - nMid - 1);
		}

		rc.DeflateRect(1, 0, 1, 2);
	}	// if point at bottom of thumb

	// Only draw the gripper if the slider is enabled.
	if (IsWindowEnabled())
	{
		if (nWidth > 5)
		{
			pDC->SelectObject(&m_penThumbLight);
			pDC->MoveTo(rc.left + 1, rc.top + 5);
			pDC->LineTo(rc.left + 1, rc.bottom - 5);
			pDC->MoveTo(rc.left + 3, rc.top + 5);
			pDC->LineTo(rc.left + 3, rc.bottom - 5);
			if (nWidth > 9)
			{
				pDC->MoveTo(rc.left + 5, rc.top + 5);
				pDC->LineTo(rc.left + 5, rc.bottom - 5);
				pDC->MoveTo(rc.left + 7, rc.top + 5);
				pDC->LineTo(rc.left + 7, rc.bottom - 5);
			}
		}

		if (nWidth > 5)
		{
			pDC->SelectObject(&m_penThumbDarker);
			pDC->MoveTo(rc.left + 2, rc.top + 5);
			pDC->LineTo(rc.left + 2, rc.bottom - 4);
			pDC->MoveTo(rc.left + 4, rc.top + 5);
			pDC->LineTo(rc.left + 4, rc.bottom - 4);
			if (nWidth > 9)
			{
				pDC->MoveTo(rc.left + 6, rc.top + 5);
				pDC->LineTo(rc.left + 6, rc.bottom - 4);
			}
		}

		if (nWidth > 5)
		{
			pDC->SetPixel(rc.left + 1, rc.top + 4, m_crThumbLighter);
			pDC->SetPixel(rc.left + 3, rc.top + 4, m_crThumbLighter);
			if (nWidth > 9)
			{
				pDC->SetPixel(rc.left + 5, rc.top + 4, m_crThumbLighter);
				pDC->SetPixel(rc.left + 7, rc.top + 4, m_crThumbLighter);
			}
		}
	}	// if (IsWindowEnabled())

	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush);
}	// DrawHorizontalThumb

//-------------------------------------------------------------------
//
void CMacSliderCtrl::DrawVerticalThumb(CDC *pDC, const CRect &rect)
//
// Return Value:	None.
//
// Parameters	:	pDC - Specifies a device context.
//						rect - The rectangle for the thumb.
//
// Remarks		:	Draws a thumb for a vertical slider control.
//
{
	CRect rc(rect);
	int nHeight = rect.Height();
	int nMid = nHeight >> 1;

	CPen penFrame(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOWFRAME));
	CPen *pOldPen = pDC->SelectObject(&penFrame);	
	CBrush br(m_crThumb);
	CBrush *pOldBrush = pDC->SelectObject(&br);

	DWORD dwStyle = GetStyle();

⌨️ 快捷键说明

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