当前位置:C++技术网 > 资讯 > 汽车里程表的绘制

汽车里程表的绘制

更新时间:2015-12-06 22:11:18浏览次数:1+次

将平移变换和旋转变换结合起来使用,能绘制很多复杂的图片。下面带你回汽车里程表:

新建工程,加入GDI+程序必备的代码,你可以在之前我写的文章去找,有介绍。新建菜单项“油表”,子菜单项“绘制油表”,建立消息响应:

void C油表View::OnDrawwatch()
{
	// TODO: 在此添加命令处理程序代码
	Graphics graphics(this->m_hWnd);
	this->RedrawWindow();
	CRect rect;
	this->GetClientRect(&rect);
	//////设置里程表的高和宽
	int width = rect.Width() / 2;
	int height = rect.Height() / 2;
	/////设置文本输出外观
	graphics.SetSmoothingMode(SmoothingModeAntiAlias);
	FontFamily fontfamily(L"Times New Roman");
	Gdiplus::Font font(&fontfamily, 14, FontStyleBold, UnitPixel);
	///////将绘图平面的坐标原点移到窗口中心
	graphics.TranslateTransform(width / 2, height / 2);
	///////绘制仪表盘
	graphics.FillEllipse(&SolidBrush(Color::Black), height / -2, height / -2, height, height);
	///////输出文本
	graphics.DrawString(L"小时", -1, &font, PointF(-26, height / -4 - font.GetHeight(0.0)), &SolidBrush(Color::Green));
	///////绘制刻度标记
	graphics.RotateTransform(225);
	for (int x = 0; x < 55; x++)
	{
		graphics.FillRectangle(&SolidBrush(Color::Green), -2, (height / 2 - 2)*(-1), 3, 15);
		graphics.RotateTransform(5);
	}
	///////重置绘图平面的坐标转换
	graphics.ResetTransform();
	graphics.TranslateTransform(width / 2, height / 2);
	graphics.RotateTransform(255);
	int sp = 0;
	CString tmp;
	//////绘制刻度值(整数类)
	for (int x = 0; x < 7; x++)
	{
		tmp.Format(L"%d", sp);
		CStringW tmpW((LPCTSTR)(tmp));
		//////绘制红色刻度
		graphics.FillRectangle(&SolidBrush(Color::Red), -3, (height / 2 - 2)*(-1), 6, 25);
		//////绘制数值
		graphics.DrawString(tmpW, -1, &font, PointF(tmp.GetLength()*(-6), (height / -2) + 25), &SolidBrush(Color::Green));
		////旋转45°
		graphics.RotateTransform(45);
		tmp.ReleaseBuffer();
		sp += 20;
	}
	//////重置绘图平面的坐标转换
	graphics.ResetTransform();
	graphics.TranslateTransform(width / 2, height / 2);
	//////绘制指针在30公里/秒的情形
	float angle;
	angle = 30 * 2.25 + 255;
	graphics.RotateTransform((float)angle);
	Pen pen(Color::Blue, 14);
	//////设置线帽
	pen.SetEndCap(LineCapArrowAnchor);
	pen.SetStartCap(LineCapRoundAnchor);
	//////画指针
	graphics.DrawLine(&pen, PointF(0, 0), PointF(0, (-1)*(height / 2.75)));
	graphics.ResetTransform();
	graphics.TranslateTransform(width / 2, height / 2);
	//////绘制中心点
	graphics.FillEllipse(&SolidBrush(Color::Black), -6, -9, 14, 14);
	graphics.FillEllipse(&SolidBrush(Color::Red), -7, -7, 14, 14);
	///////绘制速度极限标记
	pen.SetWidth(4);
	pen.SetColor(Color::Black);
	pen.SetEndCap(LineCapRound);
	pen.SetStartCap(LineCapFlat);
	graphics.DrawLine(&pen, PointF(height / 15.75, height / 3.95), PointF(height / 10.75, height / 5.2));
	pen.SetColor(Color::Red);
	graphics.DrawLine(&pen, PointF(height / 15.75, height / 3.95), PointF(height / 15.75, height / 4.6));
}
代码实现:

对于相关的函数,请你看《文字特效处理之旋转字体》《文字特效处理之文字特效》,后面我将详细解释GDI+这方面的内容,不能错过哦!