Csharp/CSharp Tutorial/GUI Windows Forms/Form Properties — различия между версиями

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

Текущая версия на 12:15, 26 мая 2010

Center Form to the screen

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class CenterForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components;
  public CenterForm()
  {
    InitializeComponent();
    CenterToScreen();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Text = "DataGridExample";
    this.Resize += new System.EventHandler(this.CenterForm_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.CenterForm_Paint);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new CenterForm());
  }
  private void CenterForm_Resize(object sender, System.EventArgs e)
  {
    Invalidate();
    Console.WriteLine("Resize");
  }
  private void CenterForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("Text", 
      new Font("Times New Roman", 20), 
      new SolidBrush(Color.Black), 
      this.DisplayRectangle);    
  }
}

Change Form ClientSize

using System;
using System.Drawing;
using System.Windows.Forms;
public class FormClientSize : Form
{
  private Button btnShow;
  private Button btnChange;
  private Label lbl;
  public FormClientSize()
  {
    BackColor = Color.LightBlue;
    ForeColor = Color.DarkBlue;
    ClientSize = new Size(350,200);
    Width = 350;
    Height = 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 FormClientSize());
  }
  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);
  }
}

Form Bounds Setting

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormBoundsSetting : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public FormBoundsSetting()
  {
    Top = 100;
    Left = 75;
    Height = 100;
    Width = 500;
    MessageBox.Show(Bounds.ToString(), "Current rect");
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );    
  }
  private void InitializeComponent()
  {
    this.ruponents = new System.ruponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormBoundsSetting());
  }
}

Form Opacity setting

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormOpacitySetting : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public FormOpacitySetting()
  {
    InitializeComponent();
    Opacity = 0.5d;
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Text = "Form1";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormOpacitySetting());
  }
  private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("String...", new Font("Times New Roman", 20), new SolidBrush(Color.Black), 40, 10);
  }
}

Form.StartPosition

using System.Drawing;
using System.Windows.Forms;
   
class FormProperties
{
     public static void Main()
     {
          Form form = new Form();
   
          form.StartPosition   = FormStartPosition.CenterScreen;
   
          Application.Run(form);
     }
}

Form with Maximize Box

using System;
using System.Drawing;
using System.ruponentModel;
using System.Windows.Forms;
public class FormSizable : System.Windows.Forms.Form
{
    public FormSizable()
    {
        this.MaximizeBox = true;
        this.FormBorderStyle = FormBorderStyle.Sizable;
    }
    static void Main()
    {
        Application.Run(new FormSizable());
    }
}

Form without MinimizeBox and MaximizedBox

using System;
using System.Drawing;
using System.Windows.Forms;
public class FormMoveDemo : Form
{
    private bool dragging;
    private Point pointClicked;
    public FormMoveDemo()
    {
        InitializeComponent();
    }
    private void lblDrag_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            dragging = true;
            pointClicked = new Point(e.X, e.Y);
        }
        else
        {
            dragging = false;
        }
    }
    private void lblDrag_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging){
            Point pointMoveTo;
            pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
            pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
            this.Location = pointMoveTo;
        }   
    }
    private void lblDrag_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }
    private void cmdClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new FormMoveDemo());
    }
    private System.Windows.Forms.Button cmdClose= new System.Windows.Forms.Button();
    private System.Windows.Forms.Label lblDrag = new System.Windows.Forms.Label();
    private System.ruponentModel.IContainer components = null;
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // cmdClose
        // 
        this.cmdClose.Location = new System.Drawing.Point(102, 215);
        this.cmdClose.Name = "cmdClose";
        this.cmdClose.Size = new System.Drawing.Size(76, 20);
        this.cmdClose.TabIndex = 5;
        this.cmdClose.Text = "Close";
        this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
        // 
        // lblDrag
        // 
        this.lblDrag.BackColor = System.Drawing.Color.Navy;
        this.lblDrag.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.lblDrag.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.lblDrag.ForeColor = System.Drawing.Color.White;
        this.lblDrag.Location = new System.Drawing.Point(94, 167);
        this.lblDrag.Name = "lblDrag";
        this.lblDrag.Size = new System.Drawing.Size(96, 36);
        this.lblDrag.TabIndex = 4;
        this.lblDrag.Text = "Click here to move the form!";
        this.lblDrag.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseUp);
        this.lblDrag.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseMove);
        this.lblDrag.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseDown);
        // 
        // FormMoveDemo
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.ControlBox = false;
        this.Controls.Add(this.cmdClose);
        this.Controls.Add(this.lblDrag);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.ResumeLayout(false);
    }
   
}

Resize redraw form: get/set Form style

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormStyleResizeRedraw : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button btnGetStyles;
  private System.ruponentModel.Container components = null;
  public FormStyleResizeRedraw()
  {
    InitializeComponent();
    SetStyle(ControlStyles.ResizeRedraw, true);
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.btnGetStyles = new System.Windows.Forms.Button();
    this.btnGetStyles.Location = new System.Drawing.Point(24, 64);
    this.btnGetStyles.Size = new System.Drawing.Size(160, 23);
    this.btnGetStyles.TabIndex = 0;
    this.btnGetStyles.Text = "Get Form Styles";
    this.btnGetStyles.Click += new System.EventHandler(this.btnGetStyles_Click);
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(211, 104);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnGetStyles});
    this.Text = "A Form with Style!";

  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormStyleResizeRedraw());
  }
  private void btnGetStyles_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show(GetStyle(ControlStyles.ResizeRedraw).ToString(), "Do you have ResizeRedraw?");
  }
}

Set form background color

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class SetBackgroundForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components;
  public SetBackgroundForm()
  {
    InitializeComponent();
    BackColor = Color.LemonChiffon;
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Resize += new System.EventHandler(this.SetBackgroundForm_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.SetBackgroundForm_Paint);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new SetBackgroundForm());
  }
  private void SetBackgroundForm_Resize(object sender, System.EventArgs e)
  {
    Invalidate();
    Console.WriteLine("Resize");
  }
  private void SetBackgroundForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("Text", 
      new Font("Times New Roman", 20), 
      new SolidBrush(Color.Black), 
      this.DisplayRectangle);    
  }
}

Set FormBorderStyle

using System.Drawing;
using System.Windows.Forms;
   
class FormProperties
{
     public static void Main()
     {
          Form form = new Form();
   
          form.FormBorderStyle = FormBorderStyle.FixedSingle;
   
          Application.Run(form);
     }
}

Set form caption

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class SetFormCaption : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components;
  public SetFormCaption()
  {
    InitializeComponent();
    Text = "www.nfex.ru";    
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Text = "DataGridExample";
    this.Resize += new System.EventHandler(this.SetFormCaption_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.SetFormCaption_Paint);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new SetFormCaption());
  }
  private void SetFormCaption_Resize(object sender, System.EventArgs e)
  {
    Invalidate();
    Console.WriteLine("Resize");
  }
  private void SetFormCaption_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("Text", 
      new Font("Times New Roman", 20), 
      new SolidBrush(Color.Black), 
      this.DisplayRectangle);    
  }
}

Set Form Height

using System.Drawing;
using System.Windows.Forms;
   
class FormProperties
{
     public static void Main()
     {
          Form form = new Form();
   
          form.Height         /= 2;
   
          Application.Run(form);
     }
}

Set Form size

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class SetFormSize : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components;
  public SetFormSize()
  {
    InitializeComponent();
    Size = new Size(200, 200);
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Resize += new System.EventHandler(this.SetFormSize_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.SetFormSize_Paint);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new SetFormSize());
  }
  private void SetFormSize_Resize(object sender, System.EventArgs e)
  {
    Invalidate();
    Console.WriteLine("Resize");
  }
  private void SetFormSize_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("Text", 
      new Font("Times New Roman", 20), 
      new SolidBrush(Color.Black), 
      this.DisplayRectangle);    
  }
}

Set Form Width

using System.Drawing;
using System.Windows.Forms;
   
class FormProperties
{
     public static void Main()
     {
          Form form = new Form();
   
          form.Width          *= 2;
   
          Application.Run(form);
     }
}

Use Form.DialogResult

using System;
using System.Drawing;
using System.Windows.Forms;
   
class SimpleDialog: Form
{
     public static void Main()
     {
          Application.Run(new SimpleDialog());
     }
     public SimpleDialog()
     {
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
     }
     void MenuOnClick(object obj, EventArgs ea)
     {
          SimpleDialogBox dlg = new SimpleDialogBox();
   
          dlg.ShowDialog();
   
          Console.WriteLine(dlg.DialogResult);
          
     }
}
class SimpleDialogBox: Form
{
     public SimpleDialogBox()
     {
          Text = "Simple Dialog Box";
   
          FormBorderStyle = FormBorderStyle.FixedDialog;
          ControlBox      = false;
          MaximizeBox     = false;
          MinimizeBox     = false;
          ShowInTaskbar   = false;
   
          Button btn = new Button();
          btn.Parent   = this;
          btn.Text     = "OK";
          btn.Location = new Point(50, 50);
          btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
          btn.Click   += new EventHandler(ButtonOkOnClick);
   
          btn = new Button();
          btn.Parent   = this;
          btn.Text     = "Cancel";
          btn.Location = new Point(50, 100);
          btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
          btn.Click   += new EventHandler(ButtonCancelOnClick);
     }
     void ButtonOkOnClick(object obj, EventArgs ea)
     {
          DialogResult = DialogResult.OK;
     }
     void ButtonCancelOnClick(object obj, EventArgs ea)
     {
          DialogResult = DialogResult.Cancel;
     }
}