InitializeComponent方法是Visual Studio设计器自动生成的,注释中也有警告除非必要不要修改里面的代码,因为在改变页面设计后,所做的修改会被覆盖。
但是要生成代码就需要写出这里面的代码了,InitializeComponent方法内容分下面几部分:
1.组件容器components初始化
2.控件生成&设置属性(部分控件需要SuspendLayout,防止属性设置过程中造成闪烁)
3.画面本身生成&属性设置
4.部分控件ResumeLayout
参考MSDN文档:
https://docs.microsoft.com/zh-cn/dotnet/api/system.codedom.codetypeofexpression?view=netframework-4.5
// Creates a reference to the System.DateTime type.
CodeTypeReference typeRef1 = new CodeTypeReference("System.DateTime");
// Creates a typeof expression for the specified type reference.
CodeTypeOfExpression typeof1 = new CodeTypeOfExpression(typeRef1);
// Create a C# code provider
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
// Generate code and send the output to the console
provider.GenerateCodeFromExpression(typeof1, Console.Out, new CodeGeneratorOptions());
// The code generator produces the following source code for the preceeding example code:
// typeof(System.DateTime)
可以使用
CodeVariableDeclarationStatement _resources
= new CodeVariableDeclarationStatement(
typeof(System.ComponentModel.ComponentResourceManager),
"resources",
new CodeObjectCreateExpression(" System.ComponentModel.ComponentResourceManager",
new CodeTypeOfExpression(new CodeTypeReference("MainFrm"))
)
);
生成:
System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(MainFrm));
例如,CodeDom代码如下:
// this.components = new System.ComponentModel.Container();
CodeStatement createComponents = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "components"),
new CodeObjectCreateExpression("System.ComponentModel.Container", new CodeExpression[]{}));
cmm.Statements.Add(createComponents);
//this.groupBox1 = new GroupBox(this.components);
CodeStatement create = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), groupBox.Name),
new CodeObjectCreateExpression("GroupBox",
new CodeExpression[]
{
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "components")
}));
cmm.Statements.Add(create);
//((System.ComponentModel.ISupportInitialize)(this.groupBox1)).BeginInit();
CodeStatement beginInit new CodeMethodInvokeExpression(
new CodeCastExpression("System.ComponentModel.ISupportInitialize",
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName)),
"BeginInit",
new CodeExpression[] { });
cmm.Statements.Add(beginInit);
//this.groupBox1.SuspendLayout();
CodeStatement suspendLayout = new CodeMethodInvokeExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName),
"SuspendLayout",
new CodeExpression[] { });
cmm.Statements.Add(suspendLayout);
//this.groupBox1.Appearance.BackColor = System.Drawing.SystemColors.Control;
CodeStatement backColor = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Appearance.BackColor"),
new CodeSnippetExpression("System.Drawing.SystemColors.Control")
);
cmm.Statements.Add(backColor);
//this.groupBox1.Appearance.Options.UseBackColor = true;
CodeStatement useBackColor = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Appearance.Options.UseBackColor"),
new CodeSnippetExpression("true"));
cmm.Statements.Add(useBackColor);
//Controls.Add
foreach (Control ctrl in groupBox.Controls)
{
CodeStatement addControl = new CodeMethodInvokeExpression(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Controls"), "Add",
new CodeExpression[]
{
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), childName)
});
cmm.Statements.Add(addControl);
}
//this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
CodeStatement dock = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Dock"),
new CodePropertyReferenceExpression(
new CodeTypeReferenceExpression("System.Windows.Forms.DockStyle"), "Fill"));
cmm.Statements.Add(dock);
//this.groupBox1.Location = new System.Drawing.Point(242, 54);
CodeStatement location = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Location"),
new CodeObjectCreateExpression(new CodeTypeReference("System.Drawing.Point"),
new CodeExpression[]
{
new CodePrimitiveExpression(242),
new CodePrimitiveExpression(54)
}));
cmm.Statements.Add(location);
//this.groupBox1.Name = "groupBox1";
CodeStatement name = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Name"),
new CodePrimitiveExpression("groupBox1"));
cmm.Statements.Add(name);
//this.groupBox1.Size = new System.Drawing.Size(233, 42);
CodeStatement size = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Size"),
new CodeObjectCreateExpression(new CodeTypeReference("System.Drawing.Size"),
new CodeExpression[]
{
new CodePrimitiveExpression(233),
new CodePrimitiveExpression(42)
}));
cmm.Statements.Add(size);
//this.groupBox1.TabIndex = 7;
CodeStatement tabIndex = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "TabIndex"),
new CodePrimitiveExpression(7));
cmm.Statements.Add(tabIndex);
//this.groupBox1.Text = "groupBox1";
CodeStatement text = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName), "Text"),
new CodePrimitiveExpression("groupBox1"));
cmm.Statements.Add(text);
//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
CodeStatement autoScaleMode = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "AutoScaleMode"),
new CodePropertyReferenceExpression(
new CodeTypeReferenceExpression("System.Windows.Forms.AutoScaleMode"), "Inherit"));
cmm.Statements.Add(autoScaleMode);
//this.ClientSize = new System.Drawing.Size(534, 470);
CodeStatement clientSize = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "ClientSize"),
new CodeObjectCreateExpression(
new CodeTypeReference("System.Drawing.Size"),
new CodeExpression[]
{
new CodePrimitiveExpression(534),
new CodePrimitiveExpression(470)
}));
cmm.Statements.Add(clientSize);
//this.groupBox1.Controls.Add(XXX);
CodeExpression ctrls = new CodeMethodInvokeExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "Controls"), "Add",
new CodeExpression[]
{
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrl.Name)
});
cmm.Statements.Add(ctrls);
//this.Name = "Form2";
CodeStatement name = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "Name"),
new CodePrimitiveExpression(winForm.Text));
cmm.Statements.Add(name);
//this.Text = "Form2";
CodeStatement text = new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "Text"),
new CodePrimitiveExpression(winForm.Text));
cmm.Statements.Add(text);
//((System.ComponentModel.ISupportInitialize)(this.group2)).EndInit();
CodeStatement new CodeMethodInvokeExpression(
new CodeCastExpression("System.ComponentModel.ISupportInitialize",
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName)),
"EndInit",
new CodeExpression[] { });
//this.group2.ResumeLayout(false);
CodeStatement resumeLayout = new CodeMethodInvokeExpression(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), ctrlName),
"ResumeLayout",
new CodeExpression[]
{
new CodeSnippetExpression("false")
});
cmm.Statements.Add(resumeLayout);
//this.ResumeLayout(false);
CodeExpression resumeLayout = new CodeMethodInvokeExpression(
new CodeThisReferenceExpression(), "ResumeLayout",
new CodeExpression[]
{
new CodeSnippetExpression("false")
});
cmm.Statements.Add(resumeLayout);
其中ctrlName都是“groupBox1”。
每部分生成的代码就是上面的注释部分。
当然可以考虑将一些通用的属性设置提取为公用方法,如Name, Text, Location, Size, TabIndex等等。
————————————————
版权声明:本文为CSDN博主「莱昂纳多达芬奇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/davinciyxw/article/details/7793089