Advertisement
Jimi2000

GaugeSampleControl

Jan 9th, 2019
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. /*
  2. In a Form, add a TrackBar (called "tbarSpeed") and a Panel (called "Canvas"), with Size (200, 200). Use the Custom Panel provided here, which just adds some ControlStyles in the costructor, to avoid flickering and artifacts. Wire up the TrackBar "tbarSpeed_Scroll" event and the Panel "Canvas_Paint" event. That's all.
  3. */
  4.  
  5.     using System.Drawing;
  6.     using System.Drawing.Drawing2D;
  7.  
  8.     float GaugeValue = 88.0f;
  9.     float GaugeSweepAngle = 270.0f;
  10.     float GaugeStartAngle = 135.0F;
  11.  
  12.     private void Canvas_Paint(object sender, PaintEventArgs e)
  13.     {
  14.         Control canvas = sender as Control;
  15.         e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  16.         Rectangle outerRectangle = new Rectangle(10, 10, 180, 180);
  17.         Rectangle innerRectangle = new Rectangle(30, 30, 140, 140);
  18.         Rectangle blendRectangle = new Rectangle(10, 10, 180, 160);
  19.         PointF innerCenter = new PointF(outerRectangle.Left + (outerRectangle.Width / 2),
  20.                                         outerRectangle.Top + (outerRectangle.Height / 2));
  21.         float gaugeLength = (outerRectangle.Width / 2) - 2;
  22.  
  23.         using (GraphicsPath path = new GraphicsPath())
  24.         {
  25.             path.AddPie(outerRectangle, GaugeStartAngle, GaugeSweepAngle);
  26.             path.AddPie(innerRectangle, GaugeStartAngle, GaugeSweepAngle);
  27.             innerRectangle.Inflate(-1, -1);
  28.  
  29.             using (Pen pen = new Pen(Color.White, 3f))
  30.             using (SolidBrush backgroundbrush = new SolidBrush(canvas.BackColor))
  31.             using (LinearGradientBrush lgbrush = new LinearGradientBrush(innerRectangle,
  32.                     Color.FromArgb(128, 48, 0, 228), Color.FromArgb(255, canvas.BackColor) , LinearGradientMode.Vertical))
  33.             using (LinearGradientBrush gradientBrush = new LinearGradientBrush(blendRectangle,
  34.                     Color.Green, Color.Red, LinearGradientMode.ForwardDiagonal))
  35.             {
  36.                 Blend blend = new Blend()
  37.                 {
  38.                     Factors = new[] { 0.0f, 0.0f, 0.1f, 0.3f, 0.7f, 1.0f },
  39.                     Positions = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f }
  40.                 };
  41.  
  42.                 Blend bgBlend = new Blend()
  43.                 {
  44.                     Factors = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f },
  45.                     Positions = new[] { 0.0f, 0.2f, 0.4f, 0.5f, 0.6f, 0.8f, 1.0f }
  46.                 };
  47.  
  48.                 lgbrush.Blend = bgBlend;
  49.                 gradientBrush.Blend = blend;
  50.                 e.Graphics.FillPath(gradientBrush, path);
  51.                 e.Graphics.DrawPath(pen, path);
  52.  
  53.                 e.Graphics.FillEllipse(backgroundbrush, innerRectangle);
  54.                 e.Graphics.FillEllipse(lgbrush, innerRectangle);
  55.  
  56.                 using (StringFormat format = new StringFormat())
  57.                 {
  58.                     format.Alignment = StringAlignment.Center;
  59.                     format.LineAlignment = StringAlignment.Center;
  60.                     innerRectangle.Location = new Point(innerRectangle.X, innerRectangle.Y + canvas.Font.Height);
  61.                     e.Graphics.DrawString(GaugeValue.ToString() + "%", canvas.Font, Brushes.White, innerRectangle, format);
  62.                 }
  63.  
  64.                 using (Matrix matrix = new Matrix())
  65.                 {
  66.                     matrix.RotateAt(GaugeStartAngle + 90 + (GaugeValue * (GaugeSweepAngle / 100)), innerCenter);
  67.                     e.Graphics.Transform = matrix;
  68.                     e.Graphics.DrawLine(pen, innerCenter, new PointF(innerCenter.X, innerCenter.Y - gaugeLength));
  69.                     e.Graphics.ResetTransform();
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     private void tbarSpeed_Scroll(object sender, EventArgs e)
  76.     {
  77.         GaugeValue = tbarSpeed.Value;
  78.         Canvas.Invalidate();
  79.     }
  80.  
  81. //----------------------- CUSTOM PANEL --------------------------
  82.  
  83.     using System.ComponentModel;
  84.     using System.Windows.Forms;
  85.  
  86.     [DesignerCategory("code")]
  87.     public class DrawingPanel : Panel
  88.     {
  89.         public DrawingPanel()
  90.         {
  91.             this.SetStyle(ControlStyles.AllPaintingInWmPaint |
  92.                           ControlStyles.UserPaint |
  93.                           ControlStyles.OptimizedDoubleBuffer, true);
  94.             this.UpdateStyles();
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement