Gauge

Torak 06.05.06 23:03

Auton mittaria muistuttava komponentti

 Tekstiversio  Arvo: 6 (6 ääntä)  Äänestä: +  -
/// Gauge 1.1
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;

namespace Gauge
{
        /// <summary>
        /// Summary description for UserControl1.
        /// </summary>
        public class UserControl1 : System.Windows.Forms.UserControl
        {
                uint Value = 90;
        bool bValue = true;
        bool bNumbers = true;
        uint Steps = 6;
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;

                public UserControl1()
                {
                        // This call is required by the Windows.Forms Form Designer.
                        InitializeComponent();

                        // TODO: Add any initialization after the InitComponent call

                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if( 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()
                {
                        //
                        // UserControl1
                        //
                        this.Name = "UserControl1";
                        this.Resize += new System.EventHandler(this.UserControl1_Resize);
                        this.Load += new System.EventHandler(this.UserControl1_Load);
                        this.Paint += new System.Windows.Forms.PaintEventHandler(this.UserControl1_Paint);

                }
                #endregion


                private void UserControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
                {
                        Graphics g = e.Graphics;    // Graphics interface
                        double start;               // starting value in radians
                        double end;                 // ending value in radians
                        double step;                // angle between marks in radians
                        double x;                   // counter
                        double s = 224;             // starting value in angle
                        double val;                 // meter angle
            int n = 0;                  // number value

                        // Creating pens
                        Pen p = new Pen(Color.LightGray, 2);
                        Pen pRed = new Pen(Color.Red, 3);

                        // Creating rect
                        Rectangle rect = new Rectangle(0, 0, Width-1, Height-1);
                        LinearGradientBrush linearBrush =
                                new LinearGradientBrush( rect,
                                Color.FromArgb(0, 0, 0),
                                Color.FromArgb(100, 100, 100),
                                225);

                        // Put antialias on
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        // draw outher ellipse
                        g.FillEllipse(linearBrush , 0, 0, Width-1, Height-1);
                       
                        // draw insinde meter
                        linearBrush.LinearColors = new Color[] {Color.FromArgb(100, 100, 100),
                                                                                                           Color.FromArgb(0, 0, 0)};
                        g.FillEllipse(linearBrush, 10, 10, Width-21, Height-21);

                        // draw inner ellipce
                        linearBrush.LinearColors = new Color[] {Color.FromArgb(0, 0, 0),
                                                                                                        Color.FromArgb(120, 120, 120)};  
                        g.FillEllipse( linearBrush, 13, 13, Width-26, Height-26);
                       
                        // Calculating staring angle
                        start = (Math.PI/(180)) * s;
                        end   = (Math.PI/(180)) * (300+s);
                        step  = (Math.PI/180) * (300/11.0);

                        // Actual drawing
            for (x = start; x < end; x += step)
            {
                g.DrawLine(p,
                    Width / 2 + Convert.ToSingle(Math.Sin(x) * Width * 0.40),
                    Height / 2 - Convert.ToSingle(Math.Cos(x) * Height * 0.40),
                    Width / 2 + Convert.ToSingle(Math.Sin(x) * Width * 0.5),
                    Height / 2 - Convert.ToSingle(Math.Cos(x) * Height * 0.5));

                if (bNumbers)
                {
                    if (n < 50)
                        g.DrawString(n.ToString(), new Font("Arial", 10), System.Drawing.Brushes.LightGray,
                            Width / 2 + Convert.ToSingle(Math.Sin(x) * Width * 0.37) - 3,
                            Height / 2 - Convert.ToSingle(Math.Cos(x) * Height * 0.37) - 3);

                    else
                        g.DrawString(n.ToString(), new Font("Arial", 10), System.Drawing.Brushes.LightGray,
                            Width / 2 + Convert.ToSingle(Math.Sin(x) * Width * 0.30) - 3,
                            Height / 2 - Convert.ToSingle(Math.Cos(x) * Height * 0.37) - 3);
                    n += 10;
                }
            }
           
            if(bValue)
                g.DrawString(this.Value.ToString(),
                    new Font("Arial", 10), System.Drawing.Brushes.LightGray,
                    (Width / 2) - 12, (Height / 3) * 2 );

                        // draw pointer
            val = (Math.PI / 180) * (300 / (100 * (11 / 10d))) * this.Value;

                                g.DrawLine(pRed, Width/2, Height/2,
                                        Width/2+ Convert.ToSingle( Math.Sin(start+val) *Width*0.40) ,
                                        Height/2-Convert.ToSingle( Math.Cos(start+val) *Height*0.40) );    
                }

                private void UserControl1_Resize(object sender, System.EventArgs e)
                {
                        this.Refresh();
                }

        /// <summary>
        /// Increase meter value by one.
        /// </summary>
                public void Increase()
                {
                        if(this.Value < 100)
                                this.Value++;
                        this.Refresh();
                }

            /// <summary>
            /// Decrease meter value by one.
            /// </summary>
                public void Decrease()
                {
                        if(this.Value > 0)
                                this.Value--;
                        this.Refresh();
                }

        /// <summary>
        /// Set meter value.
        /// </summary>
        /// <param name="val">Number 0-100</param>
        public void SetValue(uint val)
        {
            if(val > 0 && val <= 100)
                this.Value = val;
        }
       
        /// <summary>
        /// Toggle visibility of numbers.
        /// </summary>
        /// <param name="bVisible">If true show the numbers.</param>
        public void ShowNumbers(bool bVisible)
        {
            bNumbers = bVisible;
        }

        /// <summary>
        /// Toggle visivility of value.
        /// </summary>
        /// <param name="bVisible">If true show the numbers.</param>
        public void ShowValue(bool bVisible)
        {
            bValue = bVisible;
        }

                private void UserControl1_Load(object sender, System.EventArgs e)
                {
                        this.SetStyle(
                        ControlStyles.AllPaintingInWmPaint |
                        ControlStyles.UserPaint |
                        ControlStyles.DoubleBuffer,true);
                }
        }
}

Torak 23:04 6.5.06 
webdev 23:18 6.5.06 
Nätti. plussat siitä!
editoitu: 20:59 26.5.06
ZcMander 20:57 26.5.06 
Ompas kiva kun kaikki funktioiden toteutukset tungetaan samaan tiedostoon, onneksi koodi on sen verta pieni että ymmärtää.
EDIT: eiku jaa, tää onkin C#, no siitä en tiedä miten sillä pitää tehdä.
EDIT2: Olisit voinut laittaa min ja max arvojen säätämisenkin vielä.
pierutyynykäs 15:47 4.7.06 
"Ompas kiva kun kaikki funktioiden toteutukset tungetaan samaan tiedostoon, onneksi koodi on sen verta pieni että ymmärtää.
EDIT: eiku jaa, tää onkin C#, no siitä en tiedä miten sillä pitää tehdä."

Joo, C#:ssa luokkien käyttö ei liity headereihin, vaan luokkien metatietoihin, joten erillisiä esittelyjä ja määrittelyjä ei tarvitse tehdä.
editoitu: 21:52 1.8.06
meltti 20:01 28.7.06 
Piti sanoa että hienoa eikä alkaa muuta kommentoimaan...
feenix 23:00 21.5.07 
ShowNumbers ja SetValue mieluummin propertyina kuin funkkareina (ei tämä mitään Javaa ole...) ja kontrollityyleillä sanotaan että piirrä uusiksi kun resize tulee, ei tarvitse itse käsitellä (olihan moinen ennen 2.0:aakin, olihan?).