c#窗体
窗體的設置:
對于窗體,c#有個用于創建窗體的命名空間:System.Windows.Forms。想要創建一個窗體只需new 一個Form實例就行了。
但這只是一個空空的窗體,因此需要一些設置。
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 默認位置,其尺寸在窗體大小中設置。
?
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?設置窗體的最大化、最小化、關閉
不允許最小化:
?
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 }?
不允許最大化:
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 }
?
? 不允許對窗體的操作:
?
?
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 }?
為窗體設置背景圖片:
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
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
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
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
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處
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
轉載于:https://www.cnblogs.com/LiQingsong/p/8573739.html
總結
- 上一篇: 使用anaconda安装pytorch的
- 下一篇: 十天学Linux内核之第七天---电源开