1. 程式人生 > >讓控制元件隨窗體大小變化而變化~WinForms控制元件自適應窗體大小

讓控制元件隨窗體大小變化而變化~WinForms控制元件自適應窗體大小

簡單的來說就是監控,定位+保持比例。

例如介面分為左右下三部分,其中下部分最好解決。使用Dock屬性直接定位到Bottom。難點就是上面的左右兩塊。

我的做法是:外面套一層,然後分割為左右兩部分。這裡採用TableLayoutPanel為例。將TableLayoutPanel調整為一行兩列左右各佔50%的單元格形式。

並將TableLayoutPanel的Dock屬性調整為Fill。這樣兩個控制元件就會各占上下兩部分。

這時候可以執行,會發現無論窗體大小怎麼變,控制元件都是會自動調整。不會死板板的。

下面是重點:

放兩個需要測試的控制元件分別到TableLayoutPanel的兩個單元格中。我這裡放的是ListBox。然後將兩個ListBox的Anchor屬性都設為Top,Bottom,Left,Right。既上下左右。最後調整兩個ListBox到合適大小。好了,完事。。。

下面貼出的是Designer部分的程式碼。直接貼上進去看效果吧

#region Windows 窗體設計器生成的程式碼

/// <summary>/// 設計器支援所需的方法 - 不要
/// 使用程式碼編輯器修改此方法的內容。
/// </summary>private void InitializeComponent()
{
this.lstBottom = new System.Windows.Forms.ListBox();
this.tlpSpilt = new System.Windows.Forms.TableLayoutPanel();
this.lstRight = new System.Windows.Forms.ListBox();
this
.lstLeft = new System.Windows.Forms.ListBox();
this.tlpSpilt.SuspendLayout();
this.SuspendLayout();
//// lstBottom
// this.lstBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lstBottom.Font = new System.Drawing.Font("微軟雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte
)(134)));
this.lstBottom.FormattingEnabled = true;
this.lstBottom.HorizontalExtent = 2000;
this.lstBottom.HorizontalScrollbar = true;
this.lstBottom.ItemHeight = 19;
this.lstBottom.Items.AddRange(new object[] {
""});
this.lstBottom.Location = new System.Drawing.Point(0, 296);
this.lstBottom.Name = "lstBottom";
this.lstBottom.Size = new System.Drawing.Size(849, 156);
this.lstBottom.TabIndex = 12;
//// tlpSpilt
// this.tlpSpilt.ColumnCount = 2;
this.tlpSpilt.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tlpSpilt.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tlpSpilt.Controls.Add(this.lstRight, 0, 0);
this.tlpSpilt.Controls.Add(this.lstLeft, 0, 0);
this.tlpSpilt.Dock = System.Windows.Forms.DockStyle.Fill;
this.tlpSpilt.Location = new System.Drawing.Point(0, 0);
this.tlpSpilt.Name = "tlpSpilt";
this.tlpSpilt.RowCount = 1;
this.tlpSpilt.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tlpSpilt.Size = new System.Drawing.Size(849, 296);
this.tlpSpilt.TabIndex = 13;
//// lstRight
// this.lstRight.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstRight.Font = new System.Drawing.Font("微軟雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lstRight.FormattingEnabled = true;
this.lstRight.ItemHeight = 19;
this.lstRight.Items.AddRange(new object[] {
""});
this.lstRight.Location = new System.Drawing.Point(427, 3);
this.lstRight.Name = "lstRight";
this.lstRight.Size = new System.Drawing.Size(419, 289);
this.lstRight.TabIndex = 6;
//// lstLeft
// this.lstLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstLeft.Font = new System.Drawing.Font("微軟雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lstLeft.FormattingEnabled = true;
this.lstLeft.ItemHeight = 19;
this.lstLeft.Items.AddRange(new object[] {
""});
this.lstLeft.Location = new System.Drawing.Point(3, 3);
this.lstLeft.Name = "lstLeft";
this.lstLeft.Size = new System.Drawing.Size(418, 289);
this.lstLeft.TabIndex = 5;
//// Form2
// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(849, 452);
this.Controls.Add(this.tlpSpilt);
this.Controls.Add(this.lstBottom);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Demo";
this.tlpSpilt.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ListBox lstBottom;
private System.Windows.Forms.TableLayoutPanel tlpSpilt;
private System.Windows.Forms.ListBox lstRight;
private System.Windows.Forms.ListBox lstLeft;