让控件随窗体大小变化而变化~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;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值