📄 form1.h
字号:
#pragma once
namespace Example
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Drawing::Drawing2D;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果您更改该类的名称,则需要更改
/// 与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。 否则,
/// 设计器将不能与此窗体关联的
/// 本地化资源正确交互。
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Panel * panel1;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->panel1 = new System::Windows::Forms::Panel();
this->SuspendLayout();
//
// panel1
//
this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->panel1->Location = System::Drawing::Point(0, 0);
this->panel1->Name = S"panel1";
this->panel1->Size = System::Drawing::Size(344, 246);
this->panel1->TabIndex = 0;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
this->ClientSize = System::Drawing::Size(344, 246);
this->Controls->Add(this->panel1);
this->MaximizeBox = false;
this->Name = S"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = S"演示绘制汽车速度计";
this->Paint += new System::Windows::Forms::PaintEventHandler(this, Form1_Paint);
this->ResumeLayout(false);
}
private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e)
{//绘制汽车速度计
Graphics* g=this->panel1->CreateGraphics();
Rectangle MyClientRect=Rectangle(0,0,this->panel1->get_Width(),this->panel1->get_Height());
//设置速度计的高和宽
float MyWidth=this->panel1->get_Width();
float MyHeight =this->panel1->get_Height();
//设置文本输出外观
g->set_SmoothingMode(SmoothingMode::AntiAlias);
FontFamily* MyFamily=new FontFamily("Times New Roman");
System::Drawing::Font* MyFont=new System::Drawing::Font(MyFamily, 14, FontStyle::Bold, GraphicsUnit::Pixel);
//将绘图平面的坐标原点移到窗口中心
g->TranslateTransform(MyWidth/2,MyHeight/2);
//绘制速度计仪表盘
g->FillEllipse(Brushes::Black,MyHeight/-2,MyHeight/-2,MyHeight, MyHeight);
g->DrawString("公里/小时",MyFont,Brushes::Green,PointF(-26,MyHeight/3-MyFont->get_Height()));
g->RotateTransform(225);
//绘制刻度标记
for(int x = 0; x<55;x++)
{
g->FillRectangle(Brushes::Green,Rectangle(-2,(MyHeight/2 -2)* -1,3,15));
g->RotateTransform(5);
}
//重置绘图平面的坐标变换
g->ResetTransform();
g->TranslateTransform(MyWidth/2,MyHeight/2);
g->RotateTransform(225);
int iSpeed = 0;
//绘制刻度值(整数类)
for(int x = 0; x<7;x++)
{
String* MyTemp=iSpeed.ToString();
//绘制红色刻度
g->FillRectangle(Brushes::Red,Rectangle(-3,(MyHeight/2 -2)* -1,6,25));
//绘制数值
g->DrawString(MyTemp,MyFont,Brushes::Green,PointF(MyTemp->get_Length()*(-6),(MyHeight/-2) + 25));
//旋转45度
g->RotateTransform(45);
iSpeed += 20;
}
//重置绘图平面的坐标变换
g->ResetTransform();
g->TranslateTransform(MyWidth /2,MyHeight/2);
//绘制指针在30公里/秒的情形
float angle= 30*2.25f+ 225;
g->RotateTransform((float)angle);
Pen* MyPen=new Pen(Color::Blue,14);
//设置线帽
MyPen->set_EndCap(LineCap::ArrowAnchor);
MyPen->set_StartCap(LineCap::RoundAnchor);
//画指针
g->DrawLine(MyPen,PointF(0,0),PointF(0,(-1)*(MyHeight/2.75F)));
g->ResetTransform();
g->TranslateTransform(MyWidth/2,MyHeight/2);
//绘制中心点
g->FillEllipse(Brushes::Black,-6,-9,14,14);
g->FillEllipse(Brushes::Red,-7,-7,14,14);
//绘制速度极限标记
MyPen->set_Width(4);
MyPen->set_Color(Color::Black);
MyPen->set_EndCap(LineCap::Round);
MyPen->set_StartCap(LineCap::Flat);
g->DrawLine(MyPen,PointF(MyHeight/15.75F,MyHeight/3.95F),
PointF(MyHeight/10.75F,MyHeight/5.2F));
MyPen->set_Color(Color::Red);
g->DrawLine(MyPen,PointF(MyHeight/15.75F,MyHeight/3.95F),
PointF(MyHeight/15.75F,MyHeight/4.6F));
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -