C#中窗体程序中的this.SuspendLayout()和this.ResumeLayout()

 

   挂起布局逻辑与恢复布局逻辑

    有时候,需要重新布置整个窗口里的控件的位置布局,如刚刚初始化时就是一个例子。这样在每增加一个有dock等属性的控件时,由于这些布局都是相对性布局,都需要重新计算位置什么的,并实际执行布局。如果一个窗口上有50个控件,这个布局处理就要执行50次,并触发50 * n次的布局有关的事件。【n】

而实际上,在定义布局方案过程中,这些布局是没有必要每个控件定义之后就立即执行布局的,也没有必要处理因此发出的布局事件。

 

    更合理的办法,是在全部定义完所有控件的布局方案之后,才一次性计算布局结果,并一次执行。

 

    于是,就设计了这两个函数。第一个函数用于暂停实际的布局,而第二个函数用于恢复实际布局。

    在程序里,如果你需要大规模调整布局方案时,就可以先调用suspendlayout,暂停布局执行,然后定义谁在前,谁在后,谁在上,谁在下,谁填充左,谁填充右。

    布置完毕。resumelayout,于是,一次性处理完毕。

 

     如果注释这两个方法的调用,效果上也不会有什么变化,只是在性能上会造成不必要的开销。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 窗体可以使用 Panel 控件来实现分区域布局。Panel 控件是一个容器控件,可以包含其他控件,它可以按照指定的布局方式排列内部的控件。 以下是一个简单的例子,演示了如何将窗体分成两个区域: 1. 在窗体上添加两个 Panel 控件,分别命名为 panelLeft 和 panelRight。 2. 设置 panelLeft 的 Dock 属性为 Left,设置 panelRight 的 Dock 属性为 Fill。 3. 在 panelLeft 添加需要显示的控件,例如 Label、Button 等。 4. 在 panelRight 添加需要显示的控件,例如 TextBox、DataGridView 等。 代码如下: ```csharp public partial class Form1 : Form { public Form1() { InitializeComponent(); } } ``` 在 Designer.cs : ```csharp private System.Windows.Forms.Panel panelLeft; private System.Windows.Forms.Panel panelRight; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.DataGridView dataGridView1; private void InitializeComponent() { this.panelLeft = new System.Windows.Forms.Panel(); this.panelRight = new System.Windows.Forms.Panel(); this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.panelLeft.SuspendLayout(); this.panelRight.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // panelLeft // this.panelLeft.Controls.Add(this.button1); this.panelLeft.Controls.Add(this.textBox1); this.panelLeft.Controls.Add(this.label1); this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left; this.panelLeft.Location = new System.Drawing.Point(0, 0); this.panelLeft.Name = "panelLeft"; this.panelLeft.Size = new System.Drawing.Size(200, 450); this.panelLeft.TabIndex = 0; // // panelRight // this.panelRight.Controls.Add(this.dataGridView1); this.panelRight.Dock = System.Windows.Forms.DockStyle.Fill; this.panelRight.Location = new System.Drawing.Point(200, 0); this.panelRight.Name = "panelRight"; this.panelRight.Size = new System.Drawing.Size(600, 450); this.panelRight.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 18); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 17); this.label1.TabIndex = 0; this.label1.Text = "Label1"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(15, 53); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(165, 22); this.textBox1.TabIndex = 1; // // button1 // this.button1.Location = new System.Drawing.Point(15, 94); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 2; this.button1.Text = "Button1"; this.button1.UseVisualStyleBackColor = true; // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Location = new System.Drawing.Point(22, 18); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 24; this.dataGridView1.Size = new System.Drawing.Size(544, 409); this.dataGridView1.TabIndex = 0; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.panelRight); this.Controls.Add(this.panelLeft); this.Name = "Form1"; this.Text = "Form1"; this.panelLeft.ResumeLayout(false); this.panelLeft.PerformLayout(); this.panelRight.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值