roundsliderctrl.cpp

来自「完整的MP3播放器源码」· C++ 代码 · 共 749 行 · 第 1/2 页

CPP
749
字号
// RoundSliderCtrl.cpp: Implementation file

#include "stdafx.h"
#include "resource.h"
#include "RoundSliderCtrl.h"

#include <math.h>

#include "MemDC.h"
#define USE_MEM_DC // Remove this, if you don't want to use CMemDC

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



// prototypes
COLORREF GetColour3(double dAngle, COLORREF crBright, COLORREF crDark);
void DrawCircle3(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed = FALSE);
void DrawCircle3(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark);


// Calculate colour for a point at the given angle by performing a linear
// interpolation between the colours crBright and crDark based on the cosine
// of the angle between the light source and the point.
//
// Angles are measured from the +ve x-axis (i.e. (1,0) = 0 degrees, (0,1) = 90 degrees )
// But remember: +y points down!

COLORREF GetColour3(double dAngle, COLORREF crBright, COLORREF crDark)
{
#define Rad2Deg	180.0/3.1415 
#define LIGHT_SOURCE_ANGLE	-2.356		// -2.356 radians = -135 degrees, i.e. From top left

	ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
	double dAngleDifference = LIGHT_SOURCE_ANGLE - dAngle;

	if (dAngleDifference < -3.1415) dAngleDifference = 6.293 + dAngleDifference;
	else if (dAngleDifference > 3.1415) dAngleDifference = 6.293 - dAngleDifference;

	double Weight = 0.5*(cos(dAngleDifference)+1.0);

	BYTE Red   = (BYTE) (Weight*GetRValue(crBright) + (1.0-Weight)*GetRValue(crDark));
	BYTE Green = (BYTE) (Weight*GetGValue(crBright) + (1.0-Weight)*GetGValue(crDark));
	BYTE Blue  = (BYTE) (Weight*GetBValue(crBright) + (1.0-Weight)*GetBValue(crDark));

//	TRACE("LightAngle = %0.0f, Angle = %3.0f, Diff = %3.0f, Weight = %0.2f, RGB
//%3d,%3d,%3d\n", 
//		  LIGHT_SOURCE_ANGLE*Rad2Deg, dAngle*Rad2Deg, dAngleDifference*Rad2Deg,
//Weight,Red,Green,Blue);

	return RGB(Red, Green, Blue);
}

void DrawCircle3(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed)
{
	const int nDashLength = 1;
	LONG lError, lXoffset, lYoffset;
	int  nDash = 0;
	BOOL bDashOn = TRUE;

	//Check to see that the coordinates are valid
	ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
	ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );

	//Set starting values
	lXoffset = lRadius;
	lYoffset = 0;
	lError	 = -lRadius;

	do {
		if (bDashOn) {
			pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
			pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
			pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
			pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
			pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
			pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
			pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
			pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
		}

		//Advance the error term and the constant X axis step
		lError += lYoffset++;

		//Check to see if error term has overflowed
		if ((lError += lYoffset) >= 0)
			lError -= --lXoffset * 2;

		if (bDashed && (++nDash == nDashLength)) {
			nDash = 0;
			bDashOn = !bDashOn;
		}

	} while (lYoffset <= lXoffset);	//Continue until halfway point
} 

void DrawCircle3(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark)
{
	LONG lError, lXoffset, lYoffset;

	//Check to see that the coordinates are valid
	ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
	ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );

	//Set starting values
	lXoffset = lRadius;
	lYoffset = 0;
	lError	 = -lRadius;

	do {
		const double Pi = 3.141592654, 
					 Pi_on_2 = Pi * 0.5,
					 Three_Pi_on_2 = Pi * 1.5;
		COLORREF crColour;
		double	 dAngle = atan2(lYoffset, lXoffset);

		//Draw the current pixel, reflected across all eight arcs
		crColour = GetColour3(dAngle, crBright, crDark);
		pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);

		crColour = GetColour3(Pi_on_2 - dAngle, crBright, crDark);
		pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);

		crColour = GetColour3(Pi_on_2 + dAngle, crBright, crDark);
		pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);

		crColour = GetColour3(Pi - dAngle, crBright, crDark);
		pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);

		crColour = GetColour3(-Pi + dAngle, crBright, crDark);
		pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);

		crColour = GetColour3(-Pi_on_2 - dAngle, crBright, crDark);
		pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);

		crColour = GetColour3(-Pi_on_2 + dAngle, crBright, crDark);
		pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);

		crColour = GetColour3(-dAngle, crBright, crDark);
		pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);

		//Advance the error term and the constant X axis step
		lError += lYoffset++;

		//Check to see if error term has overflowed
		if ((lError += lYoffset) >= 0)
			lError -= --lXoffset * 2;

	} while (lYoffset <= lXoffset);	//Continue until halfway point
} 

/////////////////////////////////////////////////////////////////////////////
// CRoundSliderCtrl

IMPLEMENT_DYNAMIC(CRoundSliderCtrl, CSliderCtrl)

CRoundSliderCtrl::CRoundSliderCtrl()
{
	m_strText = "%ld°";
	m_nKnobRadius = 7;
	m_nZero = 0;
	m_bInverted = false;
	m_bDragging = false;

	::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
	m_font.CreateFontIndirect(&m_lf);
	m_crText = GetSysColor(COLOR_WINDOWTEXT);

	m_hKnobBrush = ::CreateSolidBrush(RGB(48,79,82));//GetSysColor(COLOR_3DFACE));
	m_hDialBrush = ::CreateSolidBrush(RGB(48,79,82));//GetSysColor(COLOR_3DFACE));
	m_hwndBrush = ::CreateSolidBrush(RGB(48,79,82));//GetSysColor(COLOR_3DFACE));
}

CRoundSliderCtrl::~CRoundSliderCtrl()
{
}

BEGIN_MESSAGE_MAP(CRoundSliderCtrl, CSliderCtrl)
	//{{AFX_MSG_MAP(CRoundSliderCtrl)
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CRoundSliderCtrl::PreSubclassWindow() 
{
	CSliderCtrl::PreSubclassWindow();

	SetRange(0, 359, FALSE);
	SetLineSize(1);
	SetPageSize(10);

	Init();
}

BOOL CRoundSliderCtrl::PreCreateWindow(CREATESTRUCT& cs) 
{
	if(!CSliderCtrl::PreCreateWindow(cs)) return FALSE;

	SetRange(0, 359, FALSE);
	SetLineSize(1);
	SetPageSize(10);

	Init();

	return TRUE;
}

void CRoundSliderCtrl::OnSize(UINT nType, int cx, int cy) 
{
	CSliderCtrl::OnSize(nType, cx, cy);
	
	Init();
}

void CRoundSliderCtrl::Init()
{
	CRect rc;
	GetClientRect(rc);

	// Resize the window to make it square
	rc.bottom = rc.right = min(rc.bottom, rc.right);

	// Get the vital statistics of the window
	m_ptCenter = rc.CenterPoint();
	m_nRadius = rc.bottom/2-(m_nKnobRadius+1);

	// Set the window region so mouse clicks only activate the round section 
	// of the slider
	m_rgn.DeleteObject(); 
	SetWindowRgn(NULL, FALSE);
	m_rgn.CreateEllipticRgnIndirect(rc);
	SetWindowRgn(m_rgn, TRUE);

	VerifyPos();
}

#pragma warning(disable:4100) // Unreferenced formal parameter
BOOL CRoundSliderCtrl::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}
#pragma warning(default:4100)

void CRoundSliderCtrl::OnPaint() 
{
	const int nMin = GetRangeMin();
	const int nMax = GetRangeMax()+1;

	CPaintDC dc(this); // device context for painting

#ifdef USE_MEM_DC
	CMemDC pDC(&dc);
#else
	CDC* pDC = &dc;
#endif

	int nRadius = m_nRadius;

	// Draw (clear) the background
	CRect rc;
	GetClientRect(rc);
	
	pDC->SelectStockObject(NULL_BRUSH);
	pDC->FillSolidRect(rc,RGB(48,79,82));// ::GetSysColor(COLOR_BTNFACE));

	// Draw the sliders channel
	DrawCircle3(pDC, m_ptCenter, nRadius--, ::GetSysColor(COLOR_3DDKSHADOW),
::GetSysColor(COLOR_3DHIGHLIGHT));
	DrawCircle3(pDC, m_ptCenter, nRadius, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
	CBrush * pOldBrush = pDC->SelectObject(CBrush::FromHandle(m_hDialBrush));
	pDC->Ellipse(m_ptCenter.x - nRadius, m_ptCenter.y + nRadius, m_ptCenter.x + nRadius, m_ptCenter.y
- nRadius);
	pDC->SelectObject(pOldBrush);	

	int nPos = (((GetPos()-nMin)*360/(nMax-nMin)) + m_nZero + 360) % 360;
	if(m_bInverted) nPos = 360-nPos;

	const double dPos = ((double)(nPos))*pi/180.0;
#pragma warning(disable:4244) // Disable warning "Converting 'double' to 'int', possible loss of
//data"
	const CPoint ptKnobCenter(m_ptCenter.x + nRadius * sin(dPos), m_ptCenter.y - nRadius * cos(dPos));
#pragma warning(default:4244)
	nRadius-=2;

	DrawCircle3(pDC, m_ptCenter, nRadius--, ::GetSysColor(COLOR_3DHIGHLIGHT),
::GetSysColor(COLOR_3DDKSHADOW));
	DrawCircle3(pDC, m_ptCenter, nRadius--, ::GetSysColor(COLOR_3DLIGHT), ::GetSysColor(COLOR_3DSHADOW));
	
	// Draw the knob
	int nKnobRadius = m_nKnobRadius;

	const CRect rcKnob(ptKnobCenter.x - nKnobRadius, ptKnobCenter.y - nKnobRadius, ptKnobCenter.x +
nKnobRadius, ptKnobCenter.y + nKnobRadius);

	CRgn rgnKnob;
	rgnKnob.CreateEllipticRgnIndirect(rcKnob);
	pDC->FillRgn(&rgnKnob, CBrush::FromHandle(m_hKnobBrush));
	rgnKnob.DeleteObject();

	if(m_bDragging)
	{
		DrawCircle3(pDC, ptKnobCenter, --nKnobRadius, ::GetSysColor(COLOR_3DDKSHADOW),
::GetSysColor(COLOR_3DHIGHLIGHT));
		DrawCircle3(pDC, ptKnobCenter, --nKnobRadius, ::GetSysColor(COLOR_3DSHADOW),
::GetSysColor(COLOR_3DLIGHT));
		CPen *pOldPen = (CPen *)pDC->SelectStockObject(NULL_PEN);
		CBrush newBrush,*pOldBrush;
		if(newBrush.CreateSolidBrush(RGB(255,0,0)))
			pOldBrush = pDC->SelectObject(&newBrush);
		CPoint ptCenter;
	ptCenter =m_ptCenter;
	     CRect rcCenter;
		 rcCenter = CRect(ptCenter.x -4,ptCenter.y -4,ptCenter.x +4,ptCenter.y +4);
		// pDC->Ellipse(&rcCenter);
		 pDC->SelectObject(pOldPen);
		pDC->MoveTo(ptCenter);
		pDC->LineTo(ptKnobCenter);
		pDC->SelectObject(pOldBrush);
		
	}
	else
	{
		DrawCircle3(pDC, ptKnobCenter, --nKnobRadius, ::GetSysColor(COLOR_3DHIGHLIGHT),
::GetSysColor(COLOR_3DDKSHADOW));
		DrawCircle3(pDC, ptKnobCenter, --nKnobRadius, ::GetSysColor(COLOR_3DLIGHT),
::GetSysColor(COLOR_3DSHADOW));
	CPen *pOldPen = (CPen *)pDC->SelectStockObject(NULL_PEN);
		CBrush newBrush,*pOldBrush;
		if(newBrush.CreateSolidBrush(RGB(255,0,0)))
			pOldBrush = pDC->SelectObject(&newBrush);
		CPoint ptCenter;
	ptCenter =m_ptCenter;
	     CRect rcCenter;
		 rcCenter = CRect(ptCenter.x -4,ptCenter.y -4,ptCenter.x +4,ptCenter.y +4);
		// pDC->Ellipse(&rcCenter);
		 pDC->SelectObject(pOldPen);
		pDC->MoveTo(ptCenter);
		pDC->LineTo(ptKnobCenter);
		pDC->SelectObject(pOldBrush);
	}

	// Draw the focus circle on the inside of the knob
	if(GetFocus() == this)
	{
		DrawCircle3(pDC, ptKnobCenter, nKnobRadius-2, RGB(0, 0, 0), TRUE);
	}

	// Draw the text
	const CString strFormattedText = OnFormatText();

	if(!strFormattedText.IsEmpty())
	{
/**
		CWnd* pOwner = GetParentOwner();
		if(pOwner)
		{
			CFont* pFont = pOwner->GetFont();
			pDC->SelectObject(pFont);
		}
**/
		CFont * pOldFont = pDC->SelectObject(&m_font);

		const CSize szExtent = pDC->GetTextExtent(strFormattedText);

⌨️ 快捷键说明

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