使用C#来绘制汽车里程表

来源:互联网 发布:淘宝营销经典案例 编辑:程序博客网 时间:2024/06/10 01:18

 private void Form3_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   Graphics g=this.CreateGraphics();
   Rectangle rect=this.ClientRectangle;

   //设置高度和宽
    int WIDTH=rect.Width/2;
   int HEIGHT=rect.Height/2;

   g.SmoothingMode=SmoothingMode.AntiAlias;
   FontFamily f=new FontFamily("宋体");
   Font font=new Font(f,14,FontStyle.Bold,GraphicsUnit.Pixel);

   g.TranslateTransform(WIDTH/2,HEIGHT/2);//原点设在窗口中心

   g.FillEllipse(new SolidBrush(Color.Black),HEIGHT/-2,HEIGHT/-2,HEIGHT,HEIGHT);

   g.DrawString("公里/小时",font,new SolidBrush(Color.Green),new PointF(-26,HEIGHT/-4-font.GetHeight(0.0f)+20));

   g.RotateTransform(225);
   for(int x=0;x<55;x++)
   {
    g.FillRectangle(new SolidBrush(Color.Green),-2,(HEIGHT/2-2)*-1,3,15);
    g.RotateTransform(5);
   }

   g.ResetTransform();
   g.TranslateTransform(WIDTH/2,HEIGHT/2);
   g.RotateTransform(225);
   int sp=0;
   string tmp;
   for(int x=0;x<7;x++)
   {
    tmp=sp.ToString();
    g.FillRectangle(new SolidBrush(Color.Red),-3,(HEIGHT/2-2)*-1,6,25);
    g.DrawString(tmp,
     font,
     new SolidBrush(Color.Green),new PointF(tmp.Length*-6,(HEIGHT/-2)+25));
    g.RotateTransform(45);
    sp+=20;
    //g.DrawString(
    
   }

   g.ResetTransform();
   g.TranslateTransform(WIDTH/2,HEIGHT/2);

   float angle;
   angle=(float)(30.0*2.25)+225;
   g.RotateTransform(angle);

   Pen p=new Pen(Color.Blue,14);
   p.EndCap=LineCap.ArrowAnchor;
   p.StartCap=LineCap.RoundAnchor;

   g.DrawLine(p,new PointF(0,0),new PointF(0,-1*(HEIGHT/2.75f)));

   g.ResetTransform();
   g.TranslateTransform(WIDTH/2,HEIGHT/2);

   g.FillEllipse(new SolidBrush(Color.Black),-6,-9,14,14);
   g.FillEllipse(new SolidBrush(Color.Red),-7,-7,14,14);

   p.Width=4.0f;
   p.Color=Color.Black;
   p.EndCap=LineCap.Round;
   p.StartCap=LineCap.Flat;
   g.DrawLine(p,new PointF(HEIGHT/15.75f,HEIGHT/3.95f),new PointF(HEIGHT/10.75f,HEIGHT/5.2f));

   p.Color=Color.Red;
   g.DrawLine(p,new PointF(HEIGHT/15.75f,HEIGHT/3.95f),new PointF(HEIGHT/15.75f,HEIGHT/4.6f));
   g.Dispose();
  }

原创粉丝点击