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

📄 graphics.cpp

📁 漂亮美观实用的,有VC+编制的动态曲线显示源码,只需稍做改进就可用于各行各业
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    else if(m_StrAlign.HAlign == CENTER && m_StrAlign.VAlign == BOTTOM)
	{
		x = (int)(x - Width/2. * cos(thta) - Height*sin(thta));
		y = (int)(y + Width/2. * sin(thta) - Height*cos(thta));
	}
    else if(m_StrAlign.HAlign == RIGHT && m_StrAlign.VAlign == TOP)
	{
		x = (int)(x - Width * cos(thta));
		y = (int)(y + Width * sin(thta));
	}
    else if(m_StrAlign.HAlign == RIGHT && m_StrAlign.VAlign == CENTER)
	{
		x = (int)(x - Width * cos(thta) - Height/2.*sin(thta));
		y = (int)(y + Width * sin(thta) - Height/2.*cos(thta));
	}
    else if(m_StrAlign.HAlign == RIGHT && m_StrAlign.VAlign == BOTTOM)
	{
		x = (int)(x - Width * cos(thta) - Height*sin(thta));
		y = (int)(y + Width * sin(thta) - Height*cos(thta));
	}
    else //if(m_StrAlign.HAlign == LEFT && m_StrAlign.VAlign == BOTTOM)
	{             
		x = (int)(x - Height*sin(thta));
		y = (int)(y - Height*cos(thta));
	}                 
	::TextOut(m_hDC, x, y, fmt, lstrlen(fmt));
	::SetTextAlign(m_hDC, PreSet);
}

void CGraphics::Format(int decimal, char* str, float value)
{
	char tstr[24];
	switch(decimal)
	{
	case 0:
		sprintf(str, "%.f", value);
		return;
	case 1:
		sprintf(tstr, "%.1f", value);
		break;
	case 2:
		sprintf(tstr, "%.2f", value);
		break;
	case 3:
		sprintf(tstr, "%.3f", value);
		break;
	case 4:
		sprintf(tstr, "%.4f", value);
		break;
	default:
		sprintf(tstr, "%.5f", value);
		break;
	}

	if (fabs(value) > 0.99999)
	{
		_gcvt(atof(tstr), 12, str);
		if (str[lstrlen(str)-1] == '.')
			str[lstrlen(str)-1] = '\0';
	}
	else
	{
		if (fabs(value) <1.0e-10)
			lstrcpy(str, "0");
		else
		{
			lstrcpy(str, tstr);
			int n = lstrlen(str) - 1;
			while(str[n] == '0' && n >= 0)
				str[n--] = '\0';
		}
	}
}

void CGraphics::Grid()
{
	int i, j;                                    
                
	HPEN hPen	= ::CreatePen(PS_DOT, 0, m_nGridColor);
	HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);

	for(i = 1; i < XGridTicks; i ++)
	{
		j = GL + (int)(1.0 * i * (GR-GL) / XGridTicks);
		DrawLine(j, GT + 1, j, GB - 1);
	}

	for(i = 1; i < YGridTicks; i ++) 
	{
		j = GT + (int)(1.0 * i * (GB-GT) / YGridTicks);
		DrawLine(GL + 1, j, GR - 1, j);
	}

	::SelectObject(m_hDC, hOldPen);
	::DeleteObject(hPen);
}

void CGraphics::XAxis()
{
	int	  xb, yb, xe, ye;   
    int   i, j;                                  
	char  str[32];
	float value;

	yb=GB;
	j = XTicks / 10;
	for(i = 0; i <= XTicks; i ++) 
	{
		xb = xe = (int)(GL + 1.0 * PX * i / XTicks );
		if((i % j) == 0)
		{
			ye = GB + m_bM / 7;
			value = (float)(m_Scale.xmin + i * (m_Scale.xmax - m_Scale.xmin) / XTicks);
			Format(m_nXDecimal, str, value);
			PrintString(xb, GB + m_bM / 5, 0, str);
		}
		else 
			ye = GB + m_bM / 14;
		if (i > 0 && i < XTicks)
			DrawLine(xb, yb, xe, ye);
	}
}

void CGraphics::YAxis()
{
    int	  xb, yb, xe, ye;   
    int   i, j;                                  
	char  str[32];
	float value;

	xe = GL;
	j = YTicks / 10;
	for(i = 0; i <= YTicks; i ++)     
	{
		yb = ye = (int)(GT + 1.0 * PY * i / YTicks );
		if((i % j) == 0) 
		{
			xb = GL - m_lM / 10;
			value = (float)(m_Scale.ymax - i * (m_Scale.ymax - m_Scale.ymin) / YTicks);
			Format(m_nYDecimal, str, value);
			PrintString(GL - m_lM / 6, yb, 0, str);
		}
		else
			xb = GL - m_lM / 20;
		if (i > 0 && i < YTicks)
			DrawLine(xb, yb, xe, ye);
	}    
}

void CGraphics::Ticks()
{
	SetStringAlign(CENTER, TOP);
	XAxis();
	SetStringAlign(RIGHT, CENTER);
	YAxis();
}

void CGraphics::RightYTick()
{
	if (m_bEnableLegend)
		return;

    int	  xb, yb, xe, ye;   
    int   i, j, k;                                  
	char  str[32];
	float value;

	xb = GR;
	SetStringAlign(LEFT, CENTER);
	if (m_nAxesType == XY || m_nAxesType == XLOG)
	{
		j = YTicks / 10;
		for(i = 0; i <= YTicks; i ++)     
		{
			yb = ye = (int)(GT + 1.0 * PY * i / YTicks );
			if((i % j) == 0) 
			{
				xe = GR + m_rM / 10;
				value = (float)(m_Scale.ymax - i * (m_Scale.ymax - m_Scale.ymin) / YTicks);
				Format(m_nYDecimal, str, value);
				PrintString(GR + m_rM / 6, yb, 0, str);
			}
			else
				xe = GR + m_rM / 20;
			if (i > 0 && i < YTicks)
				DrawLine(xb, yb, xe, ye);
		}
	}
	else
	{
		for(i = 1; i <= m_nYStep; i ++)
		{
			for(j = 1; j <= 10; j ++)
			{
				if (k == 0)   
					xe = GR + m_rM / 10;
				else
					xe = GR + m_rM / 20;
				yb = ye = GB - (int)(log10(j) * PY / m_nYStep + 1.0 * (i - 1) * PY / m_nYStep);
				if (j == 1)
				{
					value = (float)(pow(10.0, m_Scale.ymin) * pow(10.0, i - 1));
					Format(m_nYDecimal, str, value);
					PrintString(GR + m_rM / 6, yb, 0, str);
				}
				if ((i != 1 || j != 1) && (i != m_nYStep || j != 10))
					DrawLine(xb, yb, xe, ye);
				k = 1;
			}
			k=0;
		}

		value = (float)(pow(10.0, m_Scale.ymin) * pow(10.0, i - 1));
		Format(m_nYDecimal, str, value);
		PrintString(GR + m_rM / 6, yb, 0, str);
	}
}

void CGraphics::Axes()
{
  	int bm		 = ::SetBkMode(m_hDC, TRANSPARENT);
    HPEN hPen    = ::CreatePen(PS_SOLID, 0, m_nBorderColor);
    HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);

	DrawRectangle(GL, GT, GR, GB);
	m_LogFont.lfHeight = (int)(m_Size.cx / -25.0);
	if (m_LogFont.lfHeight > -10) 
		m_LogFont.lfHeight = -10;
	m_LogFont.lfWeight     = 500;
	m_LogFont.lfOrientation= 0;
	m_LogFont.lfEscapement = 0;
	m_Font = ::CreateFontIndirect(&m_LogFont);
	if (m_Font)
	{
		HFONT hOldFont = (HFONT)::SelectObject(m_hDC, m_Font);
		SetTextColor(m_hDC, m_nTickColor); 
		Ticks();
		RightYTick();
		::SelectObject(m_hDC, hOldFont);
		::DeleteObject(m_Font);
	}

    ::SelectObject(m_hDC, hOldPen);
    ::DeleteObject(hPen);  
	::SetBkMode(m_hDC, bm);
}

void CGraphics::DrawMarker(int x, int y, int mode, int n)
{
	if (m_bPrinting)	n *= m_nPrintScale;
    switch(mode)
	{
		case CROSS:
			DrawLine(x - n, y, x + n, y );
			DrawLine(x, y - n, x, y + n );
			break;

		case STAR:
			DrawLine(x - n, y, x + n, y );
			DrawLine(x - n / 2, (int)(y + n * sqrt(3.) / 2), x + n / 2, 
					(int)(y - n * sqrt(3.) / 2));
			DrawLine(x - n / 2, (int)(y - n * sqrt(3.) / 2), x + n / 2, 
					(int)(y + n * sqrt(3.) / 2));
			break;

		case FCIRCLE:
			DrawFilledCircle(x, y, n);
			break;

		case FTRIANGLE:
		{
			POINT  p[3];
			p[0].x = x - n;
			p[0].y = (int)(y + n * sqrt(3.) / 3);
			p[1].x = x + n;
			p[1].y = (int)(y + n * sqrt(3.) / 3);
			p[2].x = x;
			p[2].y = (int)(y - 2 * n * sqrt(3.) / 3);
			::Polygon(m_hDC, p, 3);
			break;
		}

		case XCROSS:
			DrawLine((int)(x + n * sqrt(2.) / 2),
					 (int)(y - n * sqrt(2.) / 2), 
					 (int)(x - n * sqrt(2.) / 2),
					 (int)(y + n * sqrt(2.) / 2));
			DrawLine((int)(x - n * sqrt(2.) / 2),
					 (int)(y - n * sqrt(2.) / 2), 
					 (int)(x + n * sqrt(2.) / 2),
					 (int)(y + n * sqrt(2.) / 2));
			break;

		case CIRCLE:
			DrawCircle(x, y, n);
			break;

		case TRIANGLE:
			DrawLine(x - n, (int)(y + n * sqrt(3.) / 3),
					 x + n, (int)(y + n * sqrt(3.) / 3));
			DrawLine(x + n, (int)(y + n * sqrt(3.) / 3),
					 x, (int)(y - 2 * n * sqrt(3.) / 3));
			DrawLine(x, (int)(y - 2 * n * sqrt(3.) / 3),
					 x - n, (int)(y + n * sqrt(3.) / 3));  
			break;

		case FSQUARE:
			::Rectangle(m_hDC, x - n, y - n, x + n, y + n);
			break;

		case SQUARE:      
			DrawRectangle(x - n, y - n, x + n, y + n );
			break;

		case FDIAMOND:
		{
			POINT  p[4];
			p[0].x = x - n;
			p[0].y = y;
			p[1].x = x;
			p[1].y = y + n;
			p[2].x = x + n;
			p[2].y = y;
			p[3].x = x;
			p[3].y = y - n;
			::Polygon(m_hDC, p, 4);
			break;
		}

		case DIAMOND:
			::MoveToEx(m_hDC, x - n, y, NULL);
			::LineTo(m_hDC, x, y + n);
			::LineTo(m_hDC, x + n, y);
			::LineTo(m_hDC, x, y - n);
			::LineTo(m_hDC, x - n, y);
			break;
	}
}

void CGraphics::DrawShadow(int n)
{
	HPEN m_CurPen = ::CreatePen(PS_SOLID, 0, RGB(127,127,127));
	HPEN m_OldPen = (HPEN)::SelectObject(m_hDC, m_CurPen);
	HBRUSH hBrush = ::CreateSolidBrush(RGB(127,127,127));
	HBRUSH hBrold = (HBRUSH)::SelectObject(m_hDC, hBrush);
	int w  = (m_Rect.right - GR) / 20 + 1;
	int xb = GR + 2 * n;
	::Rectangle(m_hDC, GR + (int)(2.2 * w), GT - m_LogFont.lfHeight, 
				m_Rect.right - (int)(0.8 * w), GT - 2 * (n + 1) * m_LogFont.lfHeight);
	::SelectObject(m_hDC, hBrold);
	::DeleteObject(hBrush);
	::SelectObject(m_hDC, m_OldPen);
	::DeleteObject(m_CurPen);

	hBrush = ::CreateSolidBrush(RGB(255, 255, 255));
	hBrold = (HBRUSH)::SelectObject(m_hDC, hBrush);
	::Rectangle(m_hDC, GR + w, GT, m_Rect.right - 2 * w, 
				GT - 2 * (n + 1) * m_LogFont.lfHeight + m_LogFont.lfHeight);
	::SelectObject(m_hDC, hBrold);
	::DeleteObject(hBrush);
}

⌨️ 快捷键说明

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