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

📄 rotatebutton.cpp

📁 模拟了现实中的示波器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RoundButton.cpp : implementation file
//
// Round Buttons!
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright (c) 1997,1998.
// 
// Modified: 2 Feb 1998 - Fix vis problem, CRgn resource leak,
//                        button reposition code redone. CJM.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in this file is used in any commercial application 
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "math.h"
#include "RotateButton.h"

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

// prototypes
//COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark);
//void DrawCircle(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed = FALSE);
//void DrawCircle(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 CRotateButton::GetColour(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 CRotateButton::DrawCircle(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed)
{
	CPen penThin,penThick,*pOldPen;
	penThin.CreatePen(PS_SOLID, 1, crColour);
	penThick.CreatePen(PS_SOLID, 2, crColour);
	CBrush brush;
	brush.CreateStockObject(NULL_BRUSH);
	pOldPen = pDC->SelectObject(&penThin);
	pDC->SelectObject(&brush);
	pDC->Ellipse(p.x - lRadius, p.y - lRadius, p.x + lRadius, p.y + lRadius);
	const double Pi = 3.141592654;
	double pt[5][2];
	double multi[2] = {1-0.2, 1-0.4};//刻度线长度是半径的0.2倍
	int number = 5;
	
	for(int i = 0; i < number; i++)
	{
		pt[i][0] = sin(Pi*i/(2*number));
		pt[i][1] = cos(Pi*i/(2*number));
	}
	for(i = 0; i < number; i++)
	{
		if(i==0)
		{
			pDC->SelectObject(&penThick);
			//画0-90度
			pDC->MoveTo(p.x + int(lRadius * pt[i][0]), p.y - int(lRadius*pt[i][1]));
			pDC->LineTo(p.x + int(lRadius * pt[i][0] * multi[1]), p.y - int(lRadius*pt[i][1] * multi[1]));
			//画90-180度
			pDC->MoveTo(p.x + int(lRadius * pt[i][1]), p.y + int(lRadius*pt[i][0]));
			pDC->LineTo(p.x + int(lRadius * pt[i][1] * multi[1]), p.y + int(lRadius*pt[i][0] * multi[1]));
			//画180-270
			pDC->MoveTo(p.x - int(lRadius * pt[i][0]), p.y + int(lRadius*pt[i][1]));
			pDC->LineTo(p.x - int(lRadius * pt[i][0] * multi[1]), p.y + int(lRadius*pt[i][1] * multi[1]));
			//画270-360度
			pDC->MoveTo(p.x - int(lRadius * pt[i][1]), p.y -int(lRadius*pt[i][0]));
			pDC->LineTo(p.x - int(lRadius * pt[i][1] * multi[1]), p.y -int(lRadius*pt[i][0] * multi[1]));
		}
		else
		{
			pDC->SelectObject(&penThin);
			//画0-90度
			pDC->MoveTo(p.x + int(lRadius * pt[i][0]), p.y - int(lRadius*pt[i][1]));
			pDC->LineTo(p.x + int(lRadius * pt[i][0] * multi[0]), p.y - int(lRadius*pt[i][1] * multi[0]));
			//画90-180度
			pDC->MoveTo(p.x + int(lRadius * pt[i][1]), p.y + int(lRadius*pt[i][0]));
			pDC->LineTo(p.x + int(lRadius * pt[i][1] * multi[0]), p.y + int(lRadius*pt[i][0] * multi[0]));
			//画180-270
			pDC->MoveTo(p.x - int(lRadius * pt[i][0]), p.y + int(lRadius*pt[i][1]));
			pDC->LineTo(p.x - int(lRadius * pt[i][0] * multi[0]), p.y + int(lRadius*pt[i][1] * multi[0]));
			//画270-360度
			pDC->MoveTo(p.x - int(lRadius * pt[i][1]), p.y -int(lRadius*pt[i][0]));
			pDC->LineTo(p.x - int(lRadius * pt[i][1] * multi[0]), p.y -int(lRadius*pt[i][0] * multi[0]));
		}
	}

	CPen pen;
	pen.CreatePen(PS_SOLID, 2, RGB(0,0,0));
	pDC->SelectObject(&pen);
	double ptX = sin(Pi*m_nAngle/180);
	double ptY = cos(Pi*m_nAngle/180);
	if (ptX < 0.0) ptX = -ptX;
	if (ptY < 0.0) ptY = -ptY;

	if(m_nAngle>=0 && m_nAngle<=90)
	{	
		pDC->MoveTo(p.x + int(lRadius * ptX * 0.8), p.y - int(lRadius*ptY* 0.8));
		pDC->LineTo(p.x + int(lRadius * ptX * 0.3), p.y - int(lRadius*ptY* 0.3));
	}
	else if(m_nAngle>90 && m_nAngle<180)
	{
		//画90-180度
		pDC->MoveTo(p.x + int(lRadius * ptX*0.8), p.y + int(lRadius*ptY*0.8));
		pDC->LineTo(p.x + int(lRadius * ptX*0.3), p.y + int(lRadius*ptY*0.3));
	}
	else if(m_nAngle>=180 && m_nAngle<270)
	{
		pDC->MoveTo(p.x - int(lRadius * ptX* 0.8), p.y + int(lRadius*ptY* 0.8));
		pDC->LineTo(p.x - int(lRadius * ptX* 0.3), p.y + int(lRadius*ptY* 0.3));
	}
	else 
	{
		//画270-360度
		pDC->MoveTo(p.x - int(lRadius * ptX* 0.8), p.y -int(lRadius*ptY* 0.8));
		pDC->LineTo(p.x - int(lRadius * ptX* 0.3), p.y -int(lRadius*ptY* 0.3));
	}
	
	pDC->SelectObject(pOldPen);
	//画刻度
	//90度
//	pDC->MoveTo(p.x, p.y-lRadius);
//	pDC->LineTo(p.x, p.y-lRadius*4/5);
	//0度
//	pDC->MoveTo(p.x+lRadius, p.y);
//	pDC->LineTo(p.x+lRadius*4/5, p.y);
	//-90度=270
//	pDC->MoveTo(p.x, p.y+lRadius);
//	pDC->LineTo(p.x, p.y+lRadius*4/5);
	//180
//	pDC->MoveTo(p.x-lRadius, p.y);
//	pDC->LineTo(p.x-lRadius*4/5, p.y);


	
/*	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) {
			//0度
			pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
			pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
			//90
			pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
			//270
			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 CRotateButton::DrawCircle(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 = GetColour(dAngle, crBright, crDark);
		pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);

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

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

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

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

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

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

		crColour = GetColour(-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)

⌨️ 快捷键说明

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