Csharp/CSharp Tutorial/GUI Windows Forms/Control

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

Control Size and Location

using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlSizeLocation : Form
{
  private Button btnShow;
  private Button btnChange;
  private Label lbl;
  public ControlSizeLocation()
  {
    BackColor = Color.LightBlue;
    ForeColor = Color.DarkBlue;
    Size = new Size(350,200);
    btnShow = new Button();
    btnShow.Location = new Point(50,50);
    btnShow.Size = new Size(100,23);
    btnShow.Text = "Show";
    btnShow.Click += new System.EventHandler(btnShow_Click);
    btnShow.Parent = this;
    btnChange = new Button();
    btnChange.Location = new Point(200,50);
    btnChange.Size = new Size(100,23);
    btnChange.Text = "Change";
    btnChange.Click += new System.EventHandler(btnChange_Click);
    btnChange.Parent = this;
    lbl = new Label();
    lbl.Text = "Control Size and Location";
    lbl.Size = new Size(400,25);
    lbl.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new ControlSizeLocation());
  }
  private void btnShow_Click(object sender, EventArgs e)
  {
    Console.WriteLine("Button Bottom:" + btnShow.Bottom.ToString());
    Console.WriteLine("Button Top:" + btnShow.Top.ToString() );
    Console.WriteLine("Button Left:" + btnShow.Left.ToString() );
    Console.WriteLine("Button Right:" + btnShow.Right.ToString() );
    Console.WriteLine("Button Location:" + btnShow.Location.ToString() );
    Console.WriteLine("Button Width:" + btnShow.Width.ToString() ); 
    Console.WriteLine("Button Height:" + btnShow.Height.ToString() );
    Console.WriteLine("Button Size:" + btnShow.Size.ToString() );
    Console.WriteLine("Button ClientSize:" + btnShow.ClientSize.ToString() );
    Console.WriteLine("Form Size:" + this.Size.ToString() );
    Console.WriteLine("Form ClientSize:" + this.ClientSize.ToString());
  }
  private void btnChange_Click(object sender, EventArgs e)
  {
    this.Size = new Size(800,200);
  }
}

Control Size and Location - Dynamic

using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlDynamicSizeLocation : Form
{
  private Button btnShow = new Button();
  private Label lbl = new Label();
  int xButtonSize, yButtonSize;
  public ControlDynamicSizeLocation()
  {
    btnShow.Parent = this;
    btnShow.Text = "Show Button Properties";
    Size = new Size(400,400);
    xButtonSize = (int)(Font.Height * .75) * btnShow.Text.Length;
    yButtonSize = Font.Height * 2;
    btnShow.Size = new Size(xButtonSize, yButtonSize);
    btnShow.Click += new System.EventHandler(btnShow_Click);
    lbl.Text = "Control Size and Location - Dynamic";
    lbl.AutoSize = true;
    lbl.Parent = this;
    OnResize(EventArgs.Empty);
  }
  protected override void OnResize(EventArgs e)
  {
    base.OnResize(e);
    int xPosition = (int)(this.ClientSize.Width / 2) - (int)(xButtonSize / 2);
    int yPosition = (int)(this.ClientSize.Height / 2) - (int)(yButtonSize / 2);
    btnShow.Location = new Point(xPosition, yPosition);
  }
  static void Main() 
  {
    Application.Run(new ControlDynamicSizeLocation());
  }
  private void btnShow_Click(object sender, EventArgs e)
  {
    Console.WriteLine("Button Bottom:" + btnShow.Bottom.ToString());
    Console.WriteLine("Button Top:" + btnShow.Top.ToString() );
    Console.WriteLine("Button Left:" + btnShow.Left.ToString() );
    Console.WriteLine("Button Right:" + btnShow.Right.ToString() );
    Console.WriteLine("Button Location:" + btnShow.Location.ToString() );
    Console.WriteLine("Button Width:" + btnShow.Width.ToString() );
    Console.WriteLine("Button Height:" + btnShow.Height.ToString() );
    Console.WriteLine("Button Size:" + btnShow.Size.ToString() );
    Console.WriteLine("Button ClientSize:" + btnShow.ClientSize.ToString() );
    Console.WriteLine("Font:" + btnShow.Font.ToString());
  }
}

Control: TabIndex, Size and Location

using System;
using System.Windows.Forms;
using System.Drawing;
public class NotePadWindowsForms : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
    
  public NotePadWindowsForms()
  {
    button1 = new System.Windows.Forms.Button();
        
    button1.Location = new System.Drawing.Point(8, 32);
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size(104, 32);
    button1.TabIndex = 0;
    button1.Text = "Click Me";
    // Adding controls to the fomr
    Controls.AddRange(new System.Windows.Forms.Control[]{  button1} );
    button1.Click += new System.EventHandler(button1_Click);
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show("Button is clicked");
  }
  public static int Main()
  {
    Application.Run(new NotePadWindowsForms());
    return 0;
  }       
}

Control Tag Property

using System;
using System.Drawing;
using System.Windows.Forms;
public class Tags : Form
{
  Label lbl;
  public Tags()
  {
    Size = new Size(300,200);
    lbl = new Label();
    lbl.Text = "Label";
    lbl.AutoSize = true;
    lbl.Parent = this;
    lbl.Location = new Point(0,0);
    FontStyle theEnum = new FontStyle();
    FontStyle[] theStyles = (FontStyle[])Enum.GetValues(theEnum.GetType());
    int i = 1;
    foreach (FontStyle style in theStyles)
    {
      Button btn = new Button();
      btn.Parent = this;
      btn.Location = new Point(25,25 * i++);
      btn.Size = new Size(75,20);
      btn.Text = style.ToString();
      btn.Tag = style;
      btn.Click += new System.EventHandler(btn_Click);
    }
  }
  static void Main() 
  {
    Application.Run(new Tags());
  }
  private void btn_Click(object sender, EventArgs e)
  {
    Button btn = (Button)sender;
    FontStyle fs = (FontStyle)btn.Tag;
    lbl.Font = new Font(lbl.Font, fs);
  }
}

Create User Control based on Control class

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class SpriteTest : Form
{
    public SpriteTest()
    {
        this.SuspendLayout();
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(295, 270);
        this.Text = "Sprite Test";
        this.Load += new System.EventHandler(this.SpriteTest_Load);
        this.ResumeLayout(false);
    }
    private bool isDraggingA = false;
    private bool isDraggingB = false;
    private EllipseShape ellipseA, ellipseB;
    private void SpriteTest_Load(object sender, EventArgs e)
    {
        ellipseA = new EllipseShape();
        ellipseA.Width = ellipseA.Height = 100;
        ellipseA.Top = ellipseA.Left = 30;
        ellipseA.BackColor = Color.Red;
        this.Controls.Add(ellipseA);
        ellipseB = new EllipseShape();
        ellipseB.Width = ellipseB.Height = 100;
        ellipseB.Top = ellipseB.Left = 130;
        ellipseB.BackColor = Color.Azure;
        this.Controls.Add(ellipseB);
        ellipseA.MouseDown += new MouseEventHandler(Ellipse_MouseDown);
        ellipseA.MouseUp += new MouseEventHandler(Ellipse_MouseUp);
        ellipseA.MouseMove += new MouseEventHandler(Ellipse_MouseMove);
        ellipseB.MouseDown += new MouseEventHandler(Ellipse_MouseDown);
        ellipseB.MouseUp += new MouseEventHandler(Ellipse_MouseUp);
        ellipseB.MouseMove += new MouseEventHandler(Ellipse_MouseMove);
    }
    private void Ellipse_MouseDown(object sender, MouseEventArgs e)
    {
        Control control = (Control)sender;
        if (e.Button == MouseButtons.Left)
        {
            control.Tag = new Point(e.X, e.Y);
            if (control == ellipseA)
            {
                isDraggingA = true;
            }
            else
            {
                isDraggingB = true;
            }
        }
    }
    private void Ellipse_MouseUp(object sender, MouseEventArgs e)
    {
        isDraggingA = false;
        isDraggingB = false;
    }
    private void Ellipse_MouseMove(object sender, MouseEventArgs e)
    {
        Control control = (Control)sender;
        if ((isDraggingA && control == ellipseA) || (isDraggingB && control == ellipseB))
        {
            Point point = (Point)control.Tag;
            control.Left = e.X + control.Left - point.X;
            control.Top = e.Y + control.Top - point.Y;
        }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new SpriteTest());
    }
    
}
class EllipseShape : Control
{
    public EllipseShape(){}
    private GraphicsPath path = null;
    private void RefreshPath()
    {
        path = new GraphicsPath();
        path.AddEllipse(this.ClientRectangle);
        this.Region = new Region(path);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (path != null)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillPath(new SolidBrush(this.BackColor), path);
            e.Graphics.DrawPath(new Pen(this.ForeColor, 1), path);
        }
    }
    protected override void OnResize(System.EventArgs e)
    {
        base.OnResize(e);
        RefreshPath();
        this.Invalidate();
    }
}

Get controls on a form and verify its type

using System;
using System.Windows.Forms;
public class GetControlOnFormDemo {
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new GetControlsOnForm());
    }
}
public partial class GetControlsOnForm : Form
{
    public GetControlsOnForm()
    {
        InitializeComponent();
    }
    private void cmdProcessAll_Click(object sender, System.EventArgs e)
    {
        ProcessControls(this);
    }
    private void ProcessControls(Control ctrl)
    {
        // Ignore the control unless it"s a textbox.
        if (ctrl.GetType() == typeof(TextBox))
        {
            ctrl.Text = "";
        }
        foreach (Control ctrlChild in ctrl.Controls)
        {
            ProcessControls(ctrlChild);
        }
    }
}
partial class GetControlsOnForm
{
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Button cmdProcessAll;
    private System.ruponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.textBox4 = new System.Windows.Forms.TextBox();
        this.cmdProcessAll = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.textBox1.Location = new System.Drawing.Point(16, 16);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(252, 20);
        this.textBox1.TabIndex = 0;
        this.textBox1.Text = "textBox1";
        this.textBox2.Location = new System.Drawing.Point(16, 48);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(252, 20);
        this.textBox2.TabIndex = 1;
        this.textBox2.Text = "textBox2";
        this.textBox3.Location = new System.Drawing.Point(16, 80);
        this.textBox3.Name = "textBox3";
        this.textBox3.Size = new System.Drawing.Size(252, 20);
        this.textBox3.TabIndex = 2;
        this.textBox3.Text = "textBox3";
        this.textBox4.Location = new System.Drawing.Point(16, 112);
        this.textBox4.Name = "textBox4";
        this.textBox4.Size = new System.Drawing.Size(252, 20);
        this.textBox4.TabIndex = 3;
        this.textBox4.Text = "textBox4";
        this.cmdProcessAll.Location = new System.Drawing.Point(20, 220);
        this.cmdProcessAll.Name = "cmdProcessAll";
        this.cmdProcessAll.Size = new System.Drawing.Size(116, 28);
        this.cmdProcessAll.TabIndex = 4;
        this.cmdProcessAll.Text = "Process Text Boxes";
        this.cmdProcessAll.Click += new System.EventHandler(this.cmdProcessAll_Click);
        this.ruponents = new System.ruponentModel.Container();
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.cmdProcessAll,
                                  this.textBox4,
                                  this.textBox3,
                                  this.textBox2,
                                  this.textBox1});
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Text = "GetControlsOnForm";
    }
}

Override the DefaultSize property to gain better performance

using System;
using System.Drawing;
using System.Windows.Forms;
public class FormWithDefaultSize : Form
{
  private Button btnShow;
  private Button btnChange;
  private Label lbl;
  protected override Size DefaultSize
  {
    get
    {
      return new Size(400,400);
    }
  }
  public FormWithDefaultSize()
  {
    BackColor = Color.LightBlue;
    ForeColor = Color.DarkBlue;

    btnShow = new Button();
    btnShow.Location = new Point(50,50);
    btnShow.Size = new Size(100,23);
    btnShow.Text = "Show";
    btnShow.Click += new System.EventHandler(btnShow_Click);
    btnShow.Parent = this;
    btnChange = new Button();
    btnChange.Location = new Point(200,50);
    btnChange.Size = new Size(100,23);
    btnChange.Text = "Change";
    btnChange.Click += new System.EventHandler(btnChange_Click);
    btnChange.Parent = this;
    lbl = new Label();
    lbl.Text = "Control Size and Location";
    lbl.Size = new Size(400,25);
    lbl.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FormWithDefaultSize());
  }
  private void btnShow_Click(object sender, EventArgs e)
  {
    Console.WriteLine("Button Bottom:" + btnShow.Bottom.ToString());
    Console.WriteLine("Button Top:" + btnShow.Top.ToString() );
    Console.WriteLine("Button Left:" + btnShow.Left.ToString() );
    Console.WriteLine("Button Right:" + btnShow.Right.ToString() );
    Console.WriteLine("Button Location:" + btnShow.Location.ToString() );
    Console.WriteLine("Button Width:" + btnShow.Width.ToString() ); 
    Console.WriteLine("Button Height:" + btnShow.Height.ToString() );
    Console.WriteLine("Button Size:" + btnShow.Size.ToString() );
    Console.WriteLine("Button ClientSize:" + btnShow.ClientSize.ToString() );
    Console.WriteLine("Form Size:" + this.Size.ToString() );
    Console.WriteLine("Form ClientSize:" + this.ClientSize.ToString());
  }
  private void btnChange_Click(object sender, EventArgs e)
  {
    this.Size = new Size(800,200);
  }
}

Render onto the button

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class ButtonRenerer : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Button btnRenderedButton;
  private System.Windows.Forms.Button btnRenderToOtherButton;
  public ButtonRenerer()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.btnRenderedButton = new System.Windows.Forms.Button();
    this.btnRenderToOtherButton = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // btnRenderedButton
    // 
    this.btnRenderedButton.Location = new System.Drawing.Point(168, 120);
    this.btnRenderedButton.Name = "btnRenderedButton";
    this.btnRenderedButton.Size = new System.Drawing.Size(112, 136);
    this.btnRenderedButton.TabIndex = 0;
    this.btnRenderedButton.Text = "Click on other button!";
    // 
    // btnRenderToOtherButton
    // 
    this.btnRenderToOtherButton.Location = new System.Drawing.Point(168, 8);
    this.btnRenderToOtherButton.Name = "btnRenderToOtherButton";
    this.btnRenderToOtherButton.Size = new System.Drawing.Size(112, 56);
    this.btnRenderToOtherButton.TabIndex = 1;
    this.btnRenderToOtherButton.Text = "Render to button";
    // 
    // ButtonRenerer
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.btnRenderToOtherButton);
    this.Controls.Add(this.btnRenderedButton);
    this.Name = "ButtonRenerer";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Basic Paint Form";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.ButtonRenerer_Paint);
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ButtonRenerer());
  }
  private void ButtonRenerer_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics buttonGraphics = Graphics.FromHwnd(btnRenderedButton.Handle);
    
    HatchBrush b = new HatchBrush(HatchStyle.Cross, Color.Purple, Color.Gold);
    
    
    buttonGraphics.FillRectangle(b,  0, 0, 50, btnRenderedButton.Height);
  }
}

Set Caption(title) of the form

using System;
using System.Windows.Forms;
using System.Drawing;
public class FormTitleSetting : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  public FormTitleSetting()
  {
    Text = "Test WinForm";
    ForeColor = System.Drawing.Color.Yellow;
    button1 = new System.Windows.Forms.Button();
    textBox1 = new System.Windows.Forms.TextBox();
    // button control and its properties
    button1.Location = new System.Drawing.Point(8, 32);
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size(104, 32);
    button1.TabIndex = 0;
    button1.Text = "Click Me";
        
    // text box control and its properties
    textBox1.Location = new System.Drawing.Point(24, 104);
    textBox1.Name = "textBox1";
    textBox1.Size = new System.Drawing.Size(184, 20);
    textBox1.TabIndex = 1;
    textBox1.Text = "textBox1";
        
    // Adding controls to the fomr
    Controls.AddRange(new System.Windows.Forms.Control[]{textBox1, button1} );
    button1.Click += new System.EventHandler(button1_Click);
                    
  }
  private void button1_Click(object sender,System.EventArgs e)
  {
    textBox1.Text = "Button is clicked";
    MessageBox.Show("Button is clicked");
  }
  public static int Main()
  {
    Application.Run(new FormTitleSetting());
    return 0;
  }       
}

Set form forground color

using System;
using System.Windows.Forms;
using System.Drawing;
public class FormForegroundColorSetting : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  public FormForegroundColorSetting()
  {
    Text = "Test WinForm";
    ForeColor = System.Drawing.Color.Yellow;
    button1 = new System.Windows.Forms.Button();
    textBox1 = new System.Windows.Forms.TextBox();
    // button control and its properties
    button1.Location = new System.Drawing.Point(8, 32);
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size(104, 32);
    button1.TabIndex = 0;
    button1.Text = "Click Me";
        
    // text box control and its properties
    textBox1.Location = new System.Drawing.Point(24, 104);
    textBox1.Name = "textBox1";
    textBox1.Size = new System.Drawing.Size(184, 20);
    textBox1.TabIndex = 1;
    textBox1.Text = "textBox1";
        
    // Adding controls to the fomr
    Controls.AddRange(new System.Windows.Forms.Control[]{textBox1, button1} );
    button1.Click += new System.EventHandler(button1_Click);
                    
  }
  private void button1_Click(object sender,System.EventArgs e)
  {
    textBox1.Text = "Button is clicked";
    MessageBox.Show("Button is clicked");
  }
  public static int Main()
  {
    Application.Run(new FormForegroundColorSetting());
    return 0;
  }       
}