在调整窗体大小时,可动态调整该窗体上控件的大小。
Windows 窗体提供三种控制窗体布局的方法:
锚定
停靠
自定义
锚定
如果将控件锚定到其容器的边缘,当调整该容器的大小时,该控件与指定边缘间的距离保持不变。控件可锚定到任意组合的容器边缘。如果将控件锚定到其容器的相对边缘,则在调整该容器大小时调整该控件的大小。
例如,如果将 TextBox 控件锚定到窗体的左右边缘,则当调整窗体的大小时,TextBox 的宽度发生变化。
C# source:
textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
vb source:
textBox1.Anchor = AnchorStyles.Top BitOr AnchorStyles.Left BitOr AnchorStyles.Right
VB source:
Imports System Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Namespace Microsoft.Samples.WinForms.VB.Anchoring
Public Class Anchoring
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
Anchoring = Me
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'设置窗体的最小大小
Me.MinimumSize = New Size(392, (117 + SystemInformation.CaptionHeight))
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'应用程序的主入口点
<STAThread()> Shared Sub Main()
System.Windows.Forms.Application.Run(New Anchoring())
End Sub
#Region " Windows 窗体设计器生成的代码 "
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.Container
Private WithEvents button1 As System.Windows.Forms.Button
Private WithEvents textBox1 As System.Windows.Forms.TextBox
Private WithEvents Anchoring As System.Windows.Forms.Form
'注意:下面的过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改它。
'不要使用代码编辑器修改它。
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.button1 = New System.Windows.Forms.Button()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.Text = "锚定示例"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 117)
button1.Size = New System.Drawing.Size(120, 40)
button1.TabIndex = 1
button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
button1.Location = New System.Drawing.Point(256, 64)
button1.Text = "请单击我!"
textBox1.Location = New System.Drawing.Point(16, 24)
textBox1.Text = "Hello Windows Forms World"
textBox1.TabIndex = 0
textBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
textBox1.Size = New System.Drawing.Size(360, 20)
Me.Controls.Add(textBox1)
Me.Controls.Add(button1)
End Sub
#End Region
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
MessageBox.Show("文本是:'" + textBox1.Text + "'")
End Sub
End Class
End Namespace
C# source:
namespace Microsoft.Samples.WinForms.Cs.Anchoring {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class Anchoring : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Button button1;
protected internal System.Windows.Forms.TextBox textBox1;
public Anchoring() {
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//为 button1 启动事件
button1.Click += new System.EventHandler(button1_Click);
this.MinimumSize = new Size(392, (117 + SystemInformation.CaptionHeight));
}
/// <summary>
/// 清理正在使用的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所必需的方法,不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.button1 = new System.Windows.Forms.Button ();
this.textBox1 = new System.Windows.Forms.TextBox ();
this.Text = "锚定示例";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (392, 117);
button1.Size = new System.Drawing.Size (120, 40);
button1.TabIndex = 1;
button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
button1.Location = new System.Drawing.Point (256, 64);
button1.Text = "请单击我!";
textBox1.Location = new System.Drawing.Point (16, 24);
textBox1.Text = "Hello Windows Forms World";
textBox1.TabIndex = 0;
textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBox1.Size = new System.Drawing.Size (360, 20);
this.Controls.Add (this.button1);
this.Controls.Add (this.textBox1);
}
private void button1_Click(object sender, System.EventArgs e) {
MessageBox.Show("文本是:'" + textBox1.Text + "'");
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string[] args) {
Application.Run(new Anchoring());
}
}
}