c#窗体

窗体的设置:
对于窗体,c#有个用于创建窗体的命名空间:System.Windows.Forms。想要创建一个窗体只需new 一个Form实例就行了。

c#窗体

1 using System;
2 using System.Windows.Forms;
3 namespace MyFirstWin {
4   class First : Form {
5     static void Main() {
6       Application.Run(new First());
7     }
8   }
9 }

但这只是一个空空的窗体,因此需要一些设置。

Width: 窗体的宽

Height: 窗体的高

Size: 窗体的大小

BackColor: 窗体的背景色

ForeColor: 窗体的前景色

FormBorderStyle: 窗体的边框样式

StartPosition: 窗体开始的位置

MaximizeBox: 设置是否可以最大化

MinimizeBox: 设置是否可以最小化

ControlBox: 设置是否可以操控窗体

Opacity: 窗体的透明度

Name: 窗体的名称

Text: 窗体的标题

Location: 设置窗体的位置

BackgroundImage: 设置背景图片

TabIndex: 获取或设置控件在其容器内的 Tab 键顺序

WindowState: 获取或设置一个值,该值指示窗体是最小化、最大化还是正常


FormBorderStyle的值分别有:

  Fixed3D: 三维边框

  FixedDialog: 对话框粗边框

  FixedSingle: 固定的单行边框

  FixedToolWindow: 固定的工具栏不可调整边框

  Sizable: 可调整边框

  SizableTollWindow: 可调整的工具栏边框

  None: 没有边框


StartPosition的值设置是枚举一个FormStartPosition来取值:

  CenterScreen: 相对于当前窗口中居中

  CenterParent: 居中父窗体

  Manual: 窗体的位置由Location设置

  WindowsDefaultBounds: 位置为Window 默认位置,其边界也由Window决定

  WindowsDefaultLocation: 位置为window 默认位置,其尺寸在窗体大小中设置。

 

c#窗体

 1 using System;
 2 using System.Drawing;
 3 using System.Windows.Forms;
 4 
 5 namespace Program {
 6     class Form1 : Form {
 7         static void Main() {
 8             Form1 f = new Form1();
 9 
10             f.Name = "Form1";
11             // 设置标题
12             f.Text = "第二个窗体";
13             // 宽高
14             f.Width = 500;
15             f.Height = 300;
16             // 背景颜色
17             f.BackColor = Color.FromArgb(0, 255, 255);
18             // 前景色
19             f.ForeColor = Color.Black;
20             // 边框样式
21             f.FormBorderStyle = FormBorderStyle.Fixed3D;
22             // 窗体位置
23             f.StartPosition = FormStartPosition.WindowsDefaultLocation;
24             // 透明度
25             f.Opacity = .8;
26 
27             // 显示窗体
28             f.ShowDialog();
29         }
30     }
31 }

 

为窗体设置MaximizzeBox、MinimizeBox、ControlBox 设置窗体的最大化、最小化、关闭

  不允许最小化:

  c#窗体

   

1     static void Main() {
2             Form1 f = new Form1();
3             f.Width = 500;
4             f.Height = 300;
5             f.BackColor = Color.FromArgb(0, 255, 255);
6             f.Text = "禁止最小化";
7             f.MinimizeBox = false;
8             f.ShowDialog();
9         }

 

  不允许最大化:

  c#窗体

  

1         static void Main() {
2             Form1 f = new Form1();
3             f.Width = 500;
4             f.Height = 300;
5             f.BackColor = Color.FromArgb(0, 255, 255);
6             f.Text = "禁止最大化";
7             f.MaximizeBox = false;
8             f.ShowDialog();
9         }    

 

  不允许对窗体的操作:

 c#窗体

   

1         static void Main() {
2             Form1 f = new Form1();
3             f.Width = 500;
4             f.Height = 300;
5             f.BackColor = Color.FromArgb(0, 255, 255);
6             f.Text = "禁止对窗体的操作";
7             f.ControlBox = false;
8             f.ShowDialog();
9         }

 

  为窗体设置背景图片:

    c#窗体

1 static void Main() {
2             Form1 f = new Form1();
3 
4             f.Width = 500;
5             f.Height = 300;
6             f.Text = "我有背景图片";
7             f.BackgroundImage = Image.FromFile(@"E:\server\Apache24\htdocs\www\tianmao\images\hc-1.jpg");
8             f.ShowDialog();
9         }

 

 对窗体位置的操作:

   为StartPosition设置CenterScreen

    c#窗体

  

1         static void Main() {
2             Form1 f = new Form1();
3 
4             f.Width = 500;
5             f.Height = 300;
6             f.Text = "StartPosition-CenterScreen";
7             f.StartPosition = FormStartPosition.CenterScreen;
8             f.ShowDialog();
9         }

 

  为StartPosition设置CenterParent

  c#窗体

  

1         static void Main() {
2             Form1 f = new Form1();
3 
4             f.Width = 500;
5             f.Height = 300;
6             f.Text = "StartPosition-CenterParent";
7             f.StartPosition = FormStartPosition.CenterParent;
8             f.ShowDialog();
9         }

  

  为StartPosition设置WindowsDefaultBounds

  c#窗体

1 static void Main() {
2             Form1 f = new Form1();
3 
4             f.Width = 500;
5             f.Height = 300;
6             f.Text = "StartPosition-WindowsDefaultBounds";
7             f.StartPosition = FormStartPosition.WindowsDefaultBounds;
8             f.ShowDialog();
9         }

 

  为StartPosition设置WindowsDefaultLocation

  c#窗体

1 static void Main() {
2             Form1 f = new Form1();
3 
4             f.Width = 500;
5             f.Height = 300;
6             f.Text = "StartPosition-WindowsDefaultLocation";
7             f.StartPosition = FormStartPosition.WindowsDefaultLocation;
8             f.ShowDialog();
9         }

 

   为StartPosition设置Manual, 并定位到100 * 100处

  c#窗体

 1 static void Main() {
 2             Form1 f = new Form1();
 3 
 4             f.Width = 500;
 5             f.Height = 300;
 6             f.Location = new Point(100, 100);
 7             f.Text = "StartPosition-Nanual";
 8             f.StartPosition = FormStartPosition.Manual;
 9             f.ShowDialog();
10         }

 

  更多的设置可以参考: https://msdn.microsoft.com/zh-cn/library/system.windows.forms.form(v=vs.110).aspx