程序开始运行时花的很多时间在Form1.Designer.cs里,所以以下3个方法都是用来精简Form1.Designer.cs里的代码.
方法1:
凭经验,简化Form1.Designer.cs里的代码,并删掉一些没用的代码
方法2:
启动时只加载界面需要的控件,例如Form控件,textbox控件等,一些启动看不到的控件例如:ContextMenuStrip/Contextmenu,Tabcontrol的tabPage2等,当opening/popup,Click时再加载,下面用ContextMenuStrip示范一下:
给ContextMenuStrip加了三个菜单添加opening事件后,Form1.Designer.cs里的代码:
namespace SpeedTest { partial class Form1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.contextMenu1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenu2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenu3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.contextMenu1ToolStripMenuItem, this.contextMenu2ToolStripMenuItem, this.contextMenu3ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(143, 70); this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening); // // contextMenu1ToolStripMenuItem // this.contextMenu1ToolStripMenuItem.Name = "contextMenu1ToolStripMenuItem"; this.contextMenu1ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu1ToolStripMenuItem.Text = "contextMenu1"; // // contextMenu2ToolStripMenuItem // this.contextMenu2ToolStripMenuItem.Name = "contextMenu2ToolStripMenuItem"; this.contextMenu2ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu2ToolStripMenuItem.Text = "contextMenu2"; // // contextMenu3ToolStripMenuItem // this.contextMenu3ToolStripMenuItem.Name = "contextMenu3ToolStripMenuItem"; this.contextMenu3ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu3ToolStripMenuItem.Text = "contextMenu3"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.ContextMenuStrip = this.contextMenuStrip1; this.Name = "Form1"; this.Text = "Form1"; this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem contextMenu1ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem contextMenu2ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem contextMenu3ToolStripMenuItem; }
将Form1.Designer.cs中的下面代码剪切粘贴到opening事件:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { // // contextMenu1ToolStripMenuItem // this.contextMenu1ToolStripMenuItem.Name = "contextMenu1ToolStripMenuItem"; this.contextMenu1ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu1ToolStripMenuItem.Text = "contextMenu1"; // // contextMenu2ToolStripMenuItem // this.contextMenu2ToolStripMenuItem.Name = "contextMenu2ToolStripMenuItem"; this.contextMenu2ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu2ToolStripMenuItem.Text = "contextMenu2"; // // contextMenu3ToolStripMenuItem // this.contextMenu3ToolStripMenuItem.Name = "contextMenu3ToolStripMenuItem"; this.contextMenu3ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.contextMenu3ToolStripMenuItem.Text = "contextMenu3"; // this.contextMenuStrip1.Opening -= new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);//去掉该事件的订阅 }
方法3:
Form1.Designer.cs中可以写到Form1的Activated事件里的都剪切过去,这方法在wince上效果非常明显.以下是Activated中我从Form1.Designer.cs搬过去的代码:
private void Form1_Activated(object sender, EventArgs e) { // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.contextMenu1ToolStripMenuItem, this.contextMenu2ToolStripMenuItem, this.contextMenu3ToolStripMenuItem}); this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening); this.contextMenuStrip1.Name = "contextMenuStrip1"; // // Form1 // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ContextMenuStrip = this.contextMenuStrip1; this.Text = "Form1"; this.Name = "Form1"; this.Activated -= new System.EventHandler(this.Form1_Activated);//去掉该事件的订阅 }
这方法要注意:
一般控件的size布局语句,不要剪切过去,保留在Form1.Designer.cs,就是这句:
this.contextMenuStrip1.Size = new System.Drawing.Size(66, 70);
如果有point语句也要保留,例如:
this.tabPage1.Location = new System.Drawing.Point(5, 0);
Form1的Activated订阅语句要记住保留:
this.Activated += new System.EventHandler(this.Form1_Activated);
这样修改可能引起的问题:
Form1.cs [设计],页面无法打开,出现下面的画面:
虽然不会影响程序的运行,但已无法再设计,所以建议备份Form1.Designer.cs文件
精简后的Form1.Designer.cs代码如下:
namespace SpeedTest { partial class Form1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.contextMenu1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenu2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenu3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // contextMenuStrip1 // this.contextMenuStrip1.Size = new System.Drawing.Size(66, 70); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.ClientSize = new System.Drawing.Size(292, 266); this.Activated += new System.EventHandler(this.Form1_Activated); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem contextMenu1ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem contextMenu2ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem contextMenu3ToolStripMenuItem; } }