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

  1. // Creates a reference to the System.DateTime type.
  2. CodeTypeReference typeRef1 = new CodeTypeReference("System.DateTime");
  3. // Creates a typeof expression for the specified type reference.
  4. CodeTypeOfExpression typeof1 = new CodeTypeOfExpression(typeRef1);
  5. // Create a C# code provider
  6. CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
  7. // Generate code and send the output to the console
  8. provider.GenerateCodeFromExpression(typeof1, Console.Out, new CodeGeneratorOptions());
  9. // The code generator produces the following source code for the preceeding example code:
  10. // typeof(System.DateTime)

可以使用

  1. CodeVariableDeclarationStatement _resources
  2. = new CodeVariableDeclarationStatement(
  3. typeof(System.ComponentModel.ComponentResourceManager),
  4. "resources",
  5. new CodeObjectCreateExpression(" System.ComponentModel.ComponentResourceManager",
  6. new CodeTypeOfExpression(new CodeTypeReference("MainFrm"))
  7. )
  8. );

生成:

  1. System.ComponentModel.ComponentResourceManager resources = new
  2. System.ComponentModel.ComponentResourceManager(typeof(MainFrm));

例如,CodeDom代码如下:

  1. // this.components = new System.ComponentModel.Container();
  2. CodeStatement createComponents = new CodeAssignStatement(
  3. new CodePropertyReferenceExpression(
  4. new CodeThisReferenceExpression(), "components"),
  5. new CodeObjectCreateExpression("System.ComponentModel.Container", new CodeExpression[]{}));
  6. cmm.Statements.Add(createComponents);
  7. //this.groupBox1 = new GroupBox(this.components);
  8. CodeStatement create = new CodeAssignStatement(
  9. new CodePropertyReferenceExpression(
  10. new CodeThisReferenceExpression(), groupBox.Name),
  11. new CodeObjectCreateExpression("GroupBox",
  12. new CodeExpression[]
  13. {
  14. new CodePropertyReferenceExpression(
  15. new CodeThisReferenceExpression(), "components")
  16. }));
  17. cmm.Statements.Add(create);
  18. //((System.ComponentModel.ISupportInitialize)(this.groupBox1)).BeginInit();
  19. CodeStatement beginInit new CodeMethodInvokeExpression(
  20. new CodeCastExpression("System.ComponentModel.ISupportInitialize",
  21. new CodePropertyReferenceExpression(
  22. new CodeThisReferenceExpression(), ctrlName)),
  23. "BeginInit",
  24. new CodeExpression[] { });
  25. cmm.Statements.Add(beginInit);
  26. //this.groupBox1.SuspendLayout();
  27. CodeStatement suspendLayout = new CodeMethodInvokeExpression(
  28. new CodePropertyReferenceExpression(
  29. new CodeThisReferenceExpression(), ctrlName),
  30. "SuspendLayout",
  31. new CodeExpression[] { });
  32. cmm.Statements.Add(suspendLayout);
  33. //this.groupBox1.Appearance.BackColor = System.Drawing.SystemColors.Control;
  34. CodeStatement backColor = new CodeAssignStatement(
  35. new CodePropertyReferenceExpression(
  36. new CodePropertyReferenceExpression(
  37. new CodeThisReferenceExpression(), ctrlName), "Appearance.BackColor"),
  38. new CodeSnippetExpression("System.Drawing.SystemColors.Control")
  39. );
  40. cmm.Statements.Add(backColor);
  41. //this.groupBox1.Appearance.Options.UseBackColor = true;
  42. CodeStatement useBackColor = new CodeAssignStatement(
  43. new CodePropertyReferenceExpression(
  44. new CodePropertyReferenceExpression(
  45. new CodeThisReferenceExpression(), ctrlName), "Appearance.Options.UseBackColor"),
  46. new CodeSnippetExpression("true"));
  47. cmm.Statements.Add(useBackColor);
  48. //Controls.Add
  49. foreach (Control ctrl in groupBox.Controls)
  50. {
  51. CodeStatement addControl = new CodeMethodInvokeExpression(
  52. new CodePropertyReferenceExpression(
  53. new CodePropertyReferenceExpression(
  54. new CodeThisReferenceExpression(), ctrlName), "Controls"), "Add",
  55. new CodeExpression[]
  56. {
  57. new CodePropertyReferenceExpression(
  58. new CodeThisReferenceExpression(), childName)
  59. });
  60. cmm.Statements.Add(addControl);
  61. }
  62. //this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  63. CodeStatement dock = new CodeAssignStatement(
  64. new CodePropertyReferenceExpression(
  65. new CodePropertyReferenceExpression(
  66. new CodeThisReferenceExpression(), ctrlName), "Dock"),
  67. new CodePropertyReferenceExpression(
  68. new CodeTypeReferenceExpression("System.Windows.Forms.DockStyle"), "Fill"));
  69. cmm.Statements.Add(dock);
  70. //this.groupBox1.Location = new System.Drawing.Point(242, 54);
  71. CodeStatement location = new CodeAssignStatement(
  72. new CodePropertyReferenceExpression(
  73. new CodePropertyReferenceExpression(
  74. new CodeThisReferenceExpression(), ctrlName), "Location"),
  75. new CodeObjectCreateExpression(new CodeTypeReference("System.Drawing.Point"),
  76. new CodeExpression[]
  77. {
  78. new CodePrimitiveExpression(242),
  79. new CodePrimitiveExpression(54)
  80. }));
  81. cmm.Statements.Add(location);
  82. //this.groupBox1.Name = "groupBox1";
  83. CodeStatement name = new CodeAssignStatement(
  84. new CodePropertyReferenceExpression(
  85. new CodePropertyReferenceExpression(
  86. new CodeThisReferenceExpression(), ctrlName), "Name"),
  87. new CodePrimitiveExpression("groupBox1"));
  88. cmm.Statements.Add(name);
  89. //this.groupBox1.Size = new System.Drawing.Size(233, 42);
  90. CodeStatement size = new CodeAssignStatement(
  91. new CodePropertyReferenceExpression(
  92. new CodePropertyReferenceExpression(
  93. new CodeThisReferenceExpression(), ctrlName), "Size"),
  94. new CodeObjectCreateExpression(new CodeTypeReference("System.Drawing.Size"),
  95. new CodeExpression[]
  96. {
  97. new CodePrimitiveExpression(233),
  98. new CodePrimitiveExpression(42)
  99. }));
  100. cmm.Statements.Add(size);
  101. //this.groupBox1.TabIndex = 7;
  102. CodeStatement tabIndex = new CodeAssignStatement(
  103. new CodePropertyReferenceExpression(
  104. new CodePropertyReferenceExpression(
  105. new CodeThisReferenceExpression(), ctrlName), "TabIndex"),
  106. new CodePrimitiveExpression(7));
  107. cmm.Statements.Add(tabIndex);
  108. //this.groupBox1.Text = "groupBox1";
  109. CodeStatement text = new CodeAssignStatement(
  110. new CodePropertyReferenceExpression(
  111. new CodePropertyReferenceExpression(
  112. new CodeThisReferenceExpression(), ctrlName), "Text"),
  113. new CodePrimitiveExpression("groupBox1"));
  114. cmm.Statements.Add(text);
  115. //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
  116. CodeStatement autoScaleMode = new CodeAssignStatement(
  117. new CodePropertyReferenceExpression(
  118. new CodeThisReferenceExpression(), "AutoScaleMode"),
  119. new CodePropertyReferenceExpression(
  120. new CodeTypeReferenceExpression("System.Windows.Forms.AutoScaleMode"), "Inherit"));
  121. cmm.Statements.Add(autoScaleMode);
  122. //this.ClientSize = new System.Drawing.Size(534, 470);
  123. CodeStatement clientSize = new CodeAssignStatement(
  124. new CodePropertyReferenceExpression(
  125. new CodeThisReferenceExpression(), "ClientSize"),
  126. new CodeObjectCreateExpression(
  127. new CodeTypeReference("System.Drawing.Size"),
  128. new CodeExpression[]
  129. {
  130. new CodePrimitiveExpression(534),
  131. new CodePrimitiveExpression(470)
  132. }));
  133. cmm.Statements.Add(clientSize);
  134. //this.groupBox1.Controls.Add(XXX);
  135. CodeExpression ctrls = new CodeMethodInvokeExpression(
  136. new CodePropertyReferenceExpression(
  137. new CodeThisReferenceExpression(), "Controls"), "Add",
  138. new CodeExpression[]
  139. {
  140. new CodePropertyReferenceExpression(
  141. new CodeThisReferenceExpression(), ctrl.Name)
  142. });
  143. cmm.Statements.Add(ctrls);
  144. //this.Name = "Form2";
  145. CodeStatement name = new CodeAssignStatement(
  146. new CodePropertyReferenceExpression(
  147. new CodeThisReferenceExpression(), "Name"),
  148. new CodePrimitiveExpression(winForm.Text));
  149. cmm.Statements.Add(name);
  150. //this.Text = "Form2";
  151. CodeStatement text = new CodeAssignStatement(
  152. new CodePropertyReferenceExpression(
  153. new CodeThisReferenceExpression(), "Text"),
  154. new CodePrimitiveExpression(winForm.Text));
  155. cmm.Statements.Add(text);
  156. //((System.ComponentModel.ISupportInitialize)(this.group2)).EndInit();
  157. CodeStatement new CodeMethodInvokeExpression(
  158. new CodeCastExpression("System.ComponentModel.ISupportInitialize",
  159. new CodePropertyReferenceExpression(
  160. new CodeThisReferenceExpression(), ctrlName)),
  161. "EndInit",
  162. new CodeExpression[] { });
  163. //this.group2.ResumeLayout(false);
  164. CodeStatement resumeLayout = new CodeMethodInvokeExpression(
  165. new CodePropertyReferenceExpression(
  166. new CodeThisReferenceExpression(), ctrlName),
  167. "ResumeLayout",
  168. new CodeExpression[]
  169. {
  170. new CodeSnippetExpression("false")
  171. });
  172. cmm.Statements.Add(resumeLayout);
  173. //this.ResumeLayout(false);
  174. CodeExpression resumeLayout = new CodeMethodInvokeExpression(
  175. new CodeThisReferenceExpression(), "ResumeLayout",
  176. new CodeExpression[]
  177. {
  178. new CodeSnippetExpression("false")
  179. });
  180. 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