C# - Analog Gösterge ve Saat Yapımı

C# projelerinde ProgressBar yerine kullanılabilecek bir gösterge ve analog saat yapımı.

C# projelerinde ProgressBar yerine kullanılabilecek bir gösterge ve analog saat yapımı.

Gauge.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Analog_Gauges
{
    public partial class Gauge : UserControl
    {
        private float m_Value = 0;
        private int m_Maximum = 300;
        
        public double GaugeScalingFactor { get; set; } = 0.47;
        public Color GaugeLineColor { get; set; } = Color.Black;
        public Color GaugeCenterColor { get; set; } = Color.Red;
        public float GaugeLineWidth { get; set; } = 2f;
        public float GaugeCenterRadius { get; set; } = 10f;

        public float Value
        {
            get { return m_Value; }
            set { m_Value = value; BackgroundImage = DrawGauge(); }
        }

        public int Maximum
        {
            get { return m_Maximum; }
            set { m_Maximum = value; BackgroundImage = DrawGauge(); }
        }

        public Gauge()
        {
            InitializeComponent();
        }

        private void Gauge_Load(object sender, EventArgs e)
        {
            BackgroundImage = DrawGauge();
        }

        private Bitmap DrawGauge()
        {
            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            g.Clear(BackColor);

            double center = Math.Min(Width, Height) / 2;
            double radius = Math.Min(Width, Height) * GaugeScalingFactor;

            Pen linePen = new Pen(ForeColor, 2f);

            float angle;
            bool isBold = true;
            int count = 0;

            for (float i = 120; i <= 420.5f; i += 300f / Maximum)
            {
                angle = i % 360;
                isBold = count++ % 10 == 0;

                float x1 = (float)(center + Math.Cos(angle * Math.PI / 180.0) * radius);
                float y1 = (float)(center + Math.Sin(angle * Math.PI / 180.0) * radius);

                double scale = isBold ? 0.85 : 0.9;

                float x2 = (float)(center + Math.Cos(angle * Math.PI / 180.0) * radius * scale);
                float y2 = (float)(center + Math.Sin(angle * Math.PI / 180.0) * radius * scale);

                linePen.Width = isBold ? 3f : 2f;
                g.DrawLine(linePen, x1, y1, x2, y2);

                int textFrequency = 20;

                if (Maximum <= 20) textFrequency = 2;
                else if (Maximum <= 50) textFrequency = 5;
                else if (Maximum <= 140) textFrequency = 10;
                else if (Maximum <= 300) textFrequency = 20;
                else textFrequency = 50;

                if (count % textFrequency == 1)
                {
                    float x3 = (float)(center + Math.Cos(angle * Math.PI / 180.0) * radius * 0.7);
                    float y3 = (float)(center + Math.Sin(angle * Math.PI / 180.0) * radius * 0.7);
                    String val = (count - 1).ToString();
                    SizeF size = g.MeasureString(val, Font);
                    g.DrawString(val, Font, new SolidBrush(ForeColor), x3 - size.Width / 2, y3 - size.Height / 2);
                }
            }

            angle = 300f * Value / Maximum + 120;
            angle = (float)(angle * Math.PI / 180.0);

            float x = (float)(center + Math.Cos(angle) * radius * 0.8);
            float y = (float)(center + Math.Sin(angle) * radius * 0.8);

            Pen pen = new Pen(Brushes.Red, 3f);
            pen.EndCap = LineCap.Round;
            g.DrawLine(pen, (float)center, (float)center, x, y);

            g.FillEllipse(new SolidBrush(GaugeCenterColor), (float)center - GaugeCenterRadius,
                (float)center - GaugeCenterRadius, GaugeCenterRadius * 2, GaugeCenterRadius * 2);

            return bitmap;
        }

        private void Gauge_Resize(object sender, EventArgs e)
        {
            Width = Height = Math.Max(Width, Height);
            BackgroundImage = DrawGauge();
        }
    }
}

Gauge.Designer.cs

namespace Analog_Gauges
{
    partial class Gauge
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Gauge
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Name = "Gauge";
            this.Size = new System.Drawing.Size(300, 300);
            this.Load += new System.EventHandler(this.Gauge_Resize);
            this.Resize += new System.EventHandler(this.Gauge_Resize);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

AnalogClock.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Analog_Gauges
{
    public partial class AnalogClock : UserControl
    {
        public double ClockScalingFactor { get; set; } = 0.4;
        public Color ClockHourColor { get; set; } = Color.Black;
        public Color ClockMinuteColor { get; set; } = Color.Black;
        public Color ClockSecondColor { get; set; } = Color.Red;
        public float ClockHourWidth { get; set; } = 8f;
        public float ClockMinuteWidth { get; set; } = 4f;
        public float ClockSecondWidth { get; set; } = 2f;
        public LineCap ClockHourLineCap { get; set; } = LineCap.Round;
        public LineCap ClockMinuteLineCap { get; set; } = LineCap.Round;
        public LineCap ClockSecondLineCap { get; set; } = LineCap.Round;
        public Color ClockCenterColor { get; set; } = Color.Black;
        public float ClockCenterRadius { get; set; } = 10;

        public AnalogClock()
        {
            InitializeComponent();
        }

        private void AnalogClock_Load(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Tick += (s, ev) => { BackgroundImage = DrawClock(); };
            timer.Interval = 1000;
            timer.Enabled = true;
        }

        private Bitmap DrawClock()
        {
            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bitmap);

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(BackColor);

            double radius = Math.Min(Width, Height) * ClockScalingFactor;
            double center = Math.Min(Width, Height) / 2.0d;

            for (int i = 0; i < 12; i++)
            {
                float x = (float)(center + Math.Cos(i * Math.PI / 6) * radius);
                float y = (float)(center + Math.Sin(i * Math.PI / 6) * radius);

                int t = (i + 3) % 12;
                String hour = t == 0 ? "12" : t.ToString();
                SizeF size = g.MeasureString(hour, Font);

                g.DrawString(hour, Font, new SolidBrush(ForeColor), x - size.Width / 2, y - size.Height / 2);

                int h = (DateTime.Now.Hour + 9) % 12;
                float hx = (float)(center + Math.Cos(h * Math.PI / 6) * radius * 0.75f);
                float hy = (float)(center + Math.Sin(h * Math.PI / 6) * radius * 0.75f);
                Pen hourPen = new Pen(ClockHourColor, ClockHourWidth);
                hourPen.EndCap = ClockHourLineCap;
                g.DrawLine(hourPen, (float)center, (float)center, hx, hy);

                int m = (DateTime.Now.Minute + 45) % 60;
                float mx = (float)(center + Math.Cos(m * 6 * Math.PI / 180) * radius * 0.85f);
                float my = (float)(center + Math.Sin(m * 6 * Math.PI / 180) * radius * 0.85f);
                Pen minutePen = new Pen(ClockMinuteColor, ClockMinuteWidth);
                minutePen.EndCap = ClockMinuteLineCap;
                g.DrawLine(minutePen, (float)center, (float)center, mx, my);

                int s = (DateTime.Now.Second + 45) % 60;
                float sx = (float)(center + Math.Cos(s * 6 * Math.PI / 180) * radius * 0.92f);
                float sy = (float)(center + Math.Sin(s * 6 * Math.PI / 180) * radius * 0.92f);
                Pen secondPen = new Pen(ClockSecondColor, ClockSecondWidth);
                secondPen.EndCap = ClockSecondLineCap;
                g.DrawLine(secondPen, (float)center, (float)center, sx, sy);

                g.FillEllipse(new SolidBrush(ClockCenterColor), (float)center - ClockCenterRadius, 
                    (float)center - ClockCenterRadius, ClockCenterRadius * 2f, ClockCenterRadius * 2f);
            }

            return bitmap;
        }

        private void AnalogClock_Resize(object sender, EventArgs e)
        {
            Width = Height = Math.Max(Width, Height);
        }
    }
}

AnalogClock.Designer.cs

namespace Analog_Gauges
{
    partial class AnalogClock
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AnalogClock
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.Name = "AnalogClock";
            this.Size = new System.Drawing.Size(300, 300);
            this.Load += new System.EventHandler(this.AnalogClock_Load);
            this.Resize += new System.EventHandler(this.AnalogClock_Resize);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

FormMain.cs

using System;
using System.Windows.Forms;

namespace Analog_Gauges
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void trackValue_Scroll(object sender, EventArgs e)
        {
            gauge1.Value = trackValue.Value;
            labelValue.Text = trackValue.Value.ToString();
        }

        private void trackMaximum_Scroll(object sender, EventArgs e)
        {
            gauge1.Maximum = trackValue.Maximum = trackMaximum.Value;
            labelMaximum.Text = trackMaximum.Value.ToString();
        }
    }
}

FormMain.Designer.cs

namespace Analog_Gauges
{
    partial class FormMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
            this.label1 = new System.Windows.Forms.Label();
            this.trackValue = new System.Windows.Forms.TrackBar();
            this.labelValue = new System.Windows.Forms.Label();
            this.labelMaximum = new System.Windows.Forms.Label();
            this.trackMaximum = new System.Windows.Forms.TrackBar();
            this.label2 = new System.Windows.Forms.Label();
            this.gauge1 = new Analog_Gauges.Gauge();
            this.analogClock1 = new Analog_Gauges.AnalogClock();
            this.label3 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.trackValue)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackMaximum)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 315);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(43, 18);
            this.label1.TabIndex = 1;
            this.label1.Text = "Value";
            // 
            // trackValue
            // 
            this.trackValue.Location = new System.Drawing.Point(12, 336);
            this.trackValue.Maximum = 100;
            this.trackValue.Name = "trackValue";
            this.trackValue.Size = new System.Drawing.Size(300, 45);
            this.trackValue.TabIndex = 2;
            this.trackValue.TickStyle = System.Windows.Forms.TickStyle.None;
            this.trackValue.Scroll += new System.EventHandler(this.trackValue_Scroll);
            // 
            // labelValue
            // 
            this.labelValue.Location = new System.Drawing.Point(15, 363);
            this.labelValue.Name = "labelValue";
            this.labelValue.Size = new System.Drawing.Size(297, 18);
            this.labelValue.TabIndex = 3;
            this.labelValue.Text = "0";
            this.labelValue.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // labelMaximum
            // 
            this.labelMaximum.Location = new System.Drawing.Point(15, 442);
            this.labelMaximum.Name = "labelMaximum";
            this.labelMaximum.Size = new System.Drawing.Size(297, 18);
            this.labelMaximum.TabIndex = 6;
            this.labelMaximum.Text = "100";
            this.labelMaximum.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // trackMaximum
            // 
            this.trackMaximum.Location = new System.Drawing.Point(12, 415);
            this.trackMaximum.Maximum = 1000;
            this.trackMaximum.Minimum = 20;
            this.trackMaximum.Name = "trackMaximum";
            this.trackMaximum.Size = new System.Drawing.Size(300, 45);
            this.trackMaximum.TabIndex = 5;
            this.trackMaximum.TickStyle = System.Windows.Forms.TickStyle.None;
            this.trackMaximum.Value = 100;
            this.trackMaximum.Scroll += new System.EventHandler(this.trackMaximum_Scroll);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 394);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(70, 18);
            this.label2.TabIndex = 4;
            this.label2.Text = "Maximum";
            // 
            // gauge1
            // 
            this.gauge1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("gauge1.BackgroundImage")));
            this.gauge1.GaugeCenterColor = System.Drawing.Color.Red;
            this.gauge1.GaugeCenterRadius = 10F;
            this.gauge1.GaugeLineColor = System.Drawing.Color.Black;
            this.gauge1.GaugeLineWidth = 2F;
            this.gauge1.GaugeScalingFactor = 0.47D;
            this.gauge1.Location = new System.Drawing.Point(12, 12);
            this.gauge1.Maximum = 100;
            this.gauge1.Name = "gauge1";
            this.gauge1.Size = new System.Drawing.Size(300, 300);
            this.gauge1.TabIndex = 0;
            this.gauge1.Value = 0F;
            // 
            // analogClock1
            // 
            this.analogClock1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("analogClock1.BackgroundImage")));
            this.analogClock1.ClockCenterColor = System.Drawing.Color.Black;
            this.analogClock1.ClockCenterRadius = 10F;
            this.analogClock1.ClockHourColor = System.Drawing.Color.Black;
            this.analogClock1.ClockHourLineCap = System.Drawing.Drawing2D.LineCap.Round;
            this.analogClock1.ClockHourWidth = 8F;
            this.analogClock1.ClockMinuteColor = System.Drawing.Color.Black;
            this.analogClock1.ClockMinuteLineCap = System.Drawing.Drawing2D.LineCap.Round;
            this.analogClock1.ClockMinuteWidth = 4F;
            this.analogClock1.ClockScalingFactor = 0.4D;
            this.analogClock1.ClockSecondColor = System.Drawing.Color.Red;
            this.analogClock1.ClockSecondLineCap = System.Drawing.Drawing2D.LineCap.Round;
            this.analogClock1.ClockSecondWidth = 2F;
            this.analogClock1.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.analogClock1.Location = new System.Drawing.Point(512, 16);
            this.analogClock1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.analogClock1.Name = "analogClock1";
            this.analogClock1.Size = new System.Drawing.Size(355, 355);
            this.analogClock1.TabIndex = 7;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Calibri", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label3.Location = new System.Drawing.Point(640, 501);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(232, 42);
            this.label3.TabIndex = 8;
            this.label3.Text = "Yunus Akademi";
            // 
            // FormMain
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.ClientSize = new System.Drawing.Size(884, 561);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.analogClock1);
            this.Controls.Add(this.labelMaximum);
            this.Controls.Add(this.trackMaximum);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.labelValue);
            this.Controls.Add(this.trackValue);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.gauge1);
            this.Font = new System.Drawing.Font("Calibri", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.Name = "FormMain";
            this.Text = "Analog Clock & Gauge";
            ((System.ComponentModel.ISupportInitialize)(this.trackValue)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackMaximum)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private Gauge gauge1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TrackBar trackValue;
        private System.Windows.Forms.Label labelValue;
        private System.Windows.Forms.Label labelMaximum;
        private System.Windows.Forms.TrackBar trackMaximum;
        private System.Windows.Forms.Label label2;
        private AnalogClock analogClock1;
        private System.Windows.Forms.Label label3;
    }
}

 

Yorum Yaz

Yorum yazabilmek için üye girişi yapmanız gerekiyor!

Yukarı Git