如何让Windows Form上的控件跟随窗体的缩放而自适应大小
2个回答
2016-07-27
展开全部
简单的来说就是监控,定位+保持比例。
例如界面分为左右下三部分,其中下部分最好解决。使用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;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询