Csharp/CSharp Tutorial/GUI Windows Forms/TrackBar

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Color Tracker

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class TrackColorForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TrackBar blueTrackBar;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TrackBar greenTrackBar;
  private System.Windows.Forms.TrackBar redTrackBar;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.Label lblCurrColor;
  private System.Windows.Forms.PictureBox colorBox;
  public TrackColorForm()
  {
    InitializeComponent();
    redTrackBar.Value = 100;
    greenTrackBar.Value = 255;
    blueTrackBar.Value = 0;
    UpdateColor();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.label4 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.panel1 = new System.Windows.Forms.Panel();
    this.blueTrackBar = new System.Windows.Forms.TrackBar();
    this.greenTrackBar = new System.Windows.Forms.TrackBar();
    this.redTrackBar = new System.Windows.Forms.TrackBar();
    this.colorBox = new System.Windows.Forms.PictureBox();
    this.lblCurrColor = new System.Windows.Forms.Label();
    this.panel1.SuspendLayout();
    ((System.ruponentModel.ISupportInitialize)(this.blueTrackBar)).BeginInit();
    ((System.ruponentModel.ISupportInitialize)(this.greenTrackBar)).BeginInit();
    ((System.ruponentModel.ISupportInitialize)(this.redTrackBar)).BeginInit();
    this.SuspendLayout();
    // 
    // label4
    // 
    this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F);
    this.label4.Location = new System.Drawing.Point(16, 88);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(240, 32);
    this.label4.TabIndex = 9;
    this.label4.Text = "Pick your slider here:";
    // 
    // label1
    // 
    this.label1.Font = new System.Drawing.Font("Arial", 15F);
    this.label1.Location = new System.Drawing.Point(24, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(88, 32);
    this.label1.TabIndex = 4;
    this.label1.Text = "Red:";
    // 
    // label3
    // 
    this.label3.Font = new System.Drawing.Font("Arial", 15F);
    this.label3.Location = new System.Drawing.Point(24, 104);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(88, 32);
    this.label3.TabIndex = 6;
    this.label3.Text = "Blue:";
    // 
    // label2
    // 
    this.label2.Font = new System.Drawing.Font("Arial", 15F);
    this.label2.Location = new System.Drawing.Point(24, 64);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(88, 32);
    this.label2.TabIndex = 5;
    this.label2.Text = "Green:";
    // 
    // panel1
    // 
    this.panel1.AutoScroll = true;
    this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.panel1.Controls.Add(this.label2);
    this.panel1.Controls.Add(this.blueTrackBar);
    this.panel1.Controls.Add(this.label3);
    this.panel1.Controls.Add(this.greenTrackBar);
    this.panel1.Controls.Add(this.redTrackBar);
    this.panel1.Controls.Add(this.label1);
    this.panel1.Location = new System.Drawing.Point(16, 120);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(384, 88);
    this.panel1.TabIndex = 8;
    // 
    // blueTrackBar
    // 
    this.blueTrackBar.Location = new System.Drawing.Point(120, 96);
    this.blueTrackBar.Maximum = 255;
    this.blueTrackBar.Name = "blueTrackBar";
    this.blueTrackBar.Size = new System.Drawing.Size(240, 45);
    this.blueTrackBar.TabIndex = 1;
    this.blueTrackBar.TickFrequency = 5;
    this.blueTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
    this.blueTrackBar.Scroll += new System.EventHandler(this.blueTrackBar_Scroll);
    // 
    // greenTrackBar
    // 
    this.greenTrackBar.Location = new System.Drawing.Point(120, 56);
    this.greenTrackBar.Maximum = 255;
    this.greenTrackBar.Name = "greenTrackBar";
    this.greenTrackBar.Size = new System.Drawing.Size(240, 45);
    this.greenTrackBar.TabIndex = 3;
    this.greenTrackBar.TickFrequency = 5;
    this.greenTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
    this.greenTrackBar.Scroll += new System.EventHandler(this.greenTrackBar_Scroll);
    // 
    // redTrackBar
    // 
    this.redTrackBar.Location = new System.Drawing.Point(120, 16);
    this.redTrackBar.Maximum = 255;
    this.redTrackBar.Name = "redTrackBar";
    this.redTrackBar.Size = new System.Drawing.Size(232, 45);
    this.redTrackBar.TabIndex = 2;
    this.redTrackBar.TickFrequency = 5;
    this.redTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
    this.redTrackBar.Scroll += new System.EventHandler(this.redTrackBar_Scroll);
    // 
    // colorBox
    // 
    this.colorBox.BackColor = System.Drawing.Color.Blue;
    this.colorBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.colorBox.Location = new System.Drawing.Point(16, 16);
    this.colorBox.Name = "colorBox";
    this.colorBox.Size = new System.Drawing.Size(384, 56);
    this.colorBox.TabIndex = 0;
    this.colorBox.TabStop = false;
    // 
    // lblCurrColor
    // 
    this.lblCurrColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblCurrColor.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
    this.lblCurrColor.Location = new System.Drawing.Point(16, 224);
    this.lblCurrColor.Name = "lblCurrColor";
    this.lblCurrColor.Size = new System.Drawing.Size(392, 40);
    this.lblCurrColor.TabIndex = 7;
    this.lblCurrColor.Text = "label4";
    // 
    // TrackColorForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(424, 285);
    this.Controls.Add(this.label4);
    this.Controls.Add(this.panel1);
    this.Controls.Add(this.lblCurrColor);
    this.Controls.Add(this.colorBox);
    this.panel1.ResumeLayout(false);
    ((System.ruponentModel.ISupportInitialize)(this.blueTrackBar)).EndInit();
    ((System.ruponentModel.ISupportInitialize)(this.greenTrackBar)).EndInit();
    ((System.ruponentModel.ISupportInitialize)(this.redTrackBar)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new TrackColorForm());
  }
  protected void greenTrackBar_Scroll (object sender, System.EventArgs e)
  {
    UpdateColor();
  }
  protected void redTrackBar_Scroll (object sender, System.EventArgs e)
  {
    UpdateColor();
  }
  protected void blueTrackBar_Scroll (object sender, System.EventArgs e)
  {
    UpdateColor();
  }
  private void UpdateColor()
  {
    Color c = Color.FromArgb(redTrackBar.Value, greenTrackBar.Value, blueTrackBar.Value);
    colorBox.BackColor = c;
    lblCurrColor.Text = string.Format("Current color is: ({0}, {1}, {2})", redTrackBar.Value, greenTrackBar.Value,blueTrackBar.Value);
  }
}

TrackBar on value changed event handler

using System;
using System.Drawing;
using System.Windows.Forms;
public class TrackBars : Form
{
  Panel pnl;
  PictureBox pb;
  TrackBar htbar;
  TrackBar vtbar;
  public TrackBars()
  {
    Size = new Size(500,520);
    Image img = Image.FromFile("YourFile.bmp");
    pnl = new Panel();
    pnl.Parent = this;
    pnl.BorderStyle = BorderStyle.FixedSingle;
    pnl.Size = new Size(400,400);
    pnl.Location = new Point(10,10);
    pb = new PictureBox();
    pb.Parent = pnl;
    pb.Size = new Size(200, 200);
    pb.Location = new Point((pnl.Size.Width / 2) - (pb.Size.Width / 2),
                (pnl.Size.Height / 2) - (pb.Size.Height /2));
    pb.BorderStyle = BorderStyle.FixedSingle;
    pb.SizeMode = PictureBoxSizeMode.StretchImage;
    pb.Image = img;
    htbar = new TrackBar();
    htbar.Parent = this;
    htbar.Orientation = Orientation.Horizontal;
    htbar.Size = new Size(pnl.Width, 10);
    htbar.Location = new Point(pnl.Left, pnl.Bottom + 25);
    htbar.TickStyle = TickStyle.BottomRight;
    htbar.TickFrequency = 25;
    htbar.Minimum = 25;
    htbar.Maximum = 400;
    htbar.SmallChange = 10;
    htbar.LargeChange = 25;
    htbar.BackColor = Color.Yellow;
    htbar.Value = pb.Width;
    htbar.ValueChanged += new EventHandler(htbar_OnValueChanged);
    vtbar = new TrackBar();
    vtbar.Parent = this;
    vtbar.Orientation = Orientation.Vertical;
    vtbar.Size = new Size(25, pnl.Height);  
    vtbar.Location = new Point(pnl.Right + 25, pnl.Top);
    vtbar.TickStyle = TickStyle.Both;
    vtbar.SetRange(25,400);
    vtbar.SmallChange = 10;
    vtbar.LargeChange = 50;
    vtbar.TickFrequency = vtbar.Maximum / 20;
    vtbar.BackColor = Color.Pink;
    vtbar.Value = pb.Height;
    vtbar.ValueChanged += new EventHandler(vtbar_OnValueChanged);
  }
  private void htbar_OnValueChanged(object sender, EventArgs e)
  {
    pb.Size = new Size(htbar.Value, pb.Height);
  }
  private void vtbar_OnValueChanged(object sender, EventArgs e)
  {
    pb.Size = new Size(pb.Width, vtbar.Value);
  }
  static void Main() 
  {
    Application.Run(new TrackBars());
  }
}

TrackBar: SmallChange, LargeChange, Minimum, Maximum

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ColorTrackBar: Form
{
     Panel      panel;
     Label[]    alabelName  = new Label[3];
     Label[]    alabelValue = new Label[3];
     TrackBar[] atrackbar   = new TrackBar[3];
   
     public static void Main()
     {
          Application.Run(new ColorTrackBar());
     }
     public ColorTrackBar()
     {
          Color[] acolor = { Color.Red, Color.Green, Color.Blue };
   
          panel = new Panel();
          panel.Parent = this;
          panel.Location = new Point(0, 0);
          panel.BackColor = Color.White;
   
          for (int i = 0; i < 3; i++)
          {
               alabelName[i] = new Label();
               alabelName[i].Parent = panel;
               alabelName[i].ForeColor = acolor[i];
               alabelName[i].Text = "&" + acolor[i].ToKnownColor();
               alabelName[i].TextAlign = ContentAlignment.MiddleCenter;
   
               atrackbar[i] = new TrackBar();
               atrackbar[i].Parent = panel;
               atrackbar[i].Orientation = Orientation.Vertical;
               atrackbar[i].BackColor = acolor[i];
               atrackbar[i].SmallChange = 1;
               atrackbar[i].LargeChange = 16;
               atrackbar[i].Minimum = 0;
               atrackbar[i].Maximum = 255;
               atrackbar[i].TickFrequency = 16;
               atrackbar[i].ValueChanged += new EventHandler(TrackBarOnValueChanged);
   
               alabelValue[i] = new Label();
               alabelValue[i].Parent = panel;
               alabelValue[i].TextAlign = ContentAlignment.MiddleCenter;
          }
          Color color = BackColor;
          atrackbar[0].Value = color.R; // Generates ValueChanged event
          atrackbar[1].Value = color.G;
          atrackbar[2].Value = color.B;
   
          OnResize(EventArgs.Empty);
     }
     protected override void OnResize(EventArgs ea)
     {
          base.OnResize(ea);
   
          int cx = ClientSize.Width;
          int cy = ClientSize.Height;
          int cyFont = Font.Height;
   
          panel.Size = new Size(cx / 2, cy);
   
          for (int i = 0; i < 3; i++)
          {
               alabelName[i].Location = new Point(i * cx / 6, cyFont / 2);
               alabelName[i].Size = new Size(cx / 6, cyFont);
   
               atrackbar[i].Height = cy - 4 * cyFont;
               atrackbar[i].Location = 
                    new Point((1 + 2 * i) * cx / 12 - atrackbar[i].Width / 2,
                              2 * cyFont);
   
               alabelValue[i].Location = new Point(i * cx / 6,
                                                   cy - 3 * cyFont / 2);
               alabelValue[i].Size = new Size(cx / 6, cyFont);
          }
     }
     void TrackBarOnValueChanged(object obj, EventArgs ea)
     {
          for (int i = 0; i < 3; i++)
               if((TrackBar) obj == atrackbar[i])
                    alabelValue[i].Text = atrackbar[i].Value.ToString();
   
          BackColor = Color.FromArgb(atrackbar[0].Value, 
                                     atrackbar[1].Value,
                                     atrackbar[2].Value);
     }
}

TractBar Large/Small range

using System;
using System.Drawing;
using System.Windows.Forms;
public class TrackBars : Form
{
  Panel pnl;
  PictureBox pb;
  TrackBar htbar;
  TrackBar vtbar;
  public TrackBars()
  {
    Size = new Size(500,520);
    Image img = Image.FromFile("YourFile.bmp");
    pnl = new Panel();
    pnl.Parent = this;
    pnl.BorderStyle = BorderStyle.FixedSingle;
    pnl.Size = new Size(400,400);
    pnl.Location = new Point(10,10);
    pb = new PictureBox();
    pb.Parent = pnl;
    pb.Size = new Size(200, 200);
    pb.Location = new Point((pnl.Size.Width / 2) - (pb.Size.Width / 2),
                (pnl.Size.Height / 2) - (pb.Size.Height /2));
    pb.BorderStyle = BorderStyle.FixedSingle;
    pb.SizeMode = PictureBoxSizeMode.StretchImage;
    pb.Image = img;
    htbar = new TrackBar();
    htbar.Parent = this;
    htbar.Orientation = Orientation.Horizontal;
    htbar.Size = new Size(pnl.Width, 10);
    htbar.Location = new Point(pnl.Left, pnl.Bottom + 25);
    htbar.TickStyle = TickStyle.BottomRight;
    htbar.TickFrequency = 25;
    htbar.Minimum = 25;
    htbar.Maximum = 400;
    htbar.SmallChange = 10;
    htbar.LargeChange = 25;
    htbar.BackColor = Color.Yellow;
    htbar.Value = pb.Width;
    htbar.ValueChanged += new EventHandler(htbar_OnValueChanged);
    vtbar = new TrackBar();
    vtbar.Parent = this;
    vtbar.Orientation = Orientation.Vertical;
    vtbar.Size = new Size(25, pnl.Height);  
    vtbar.Location = new Point(pnl.Right + 25, pnl.Top);
    vtbar.TickStyle = TickStyle.Both;
    vtbar.SetRange(25,400);
    vtbar.SmallChange = 10;
    vtbar.LargeChange = 50;
    vtbar.TickFrequency = vtbar.Maximum / 20;
    vtbar.BackColor = Color.Pink;
    vtbar.Value = pb.Height;
    vtbar.ValueChanged += new EventHandler(vtbar_OnValueChanged);
  }
  private void htbar_OnValueChanged(object sender, EventArgs e)
  {
    pb.Size = new Size(htbar.Value, pb.Height);
  }
  private void vtbar_OnValueChanged(object sender, EventArgs e)
  {
    pb.Size = new Size(pb.Width, vtbar.Value);
  }
  static void Main() 
  {
    Application.Run(new TrackBars());
  }
}