写一个根据现有窗体生成自绘窗体代码
有时候需要自绘窗体,但是一个一个手动摆放,太麻烦,写了一个代码自动生成器,VS2017下可以运行,只支持Button,Label,这里生成的Dotnetbar代码,其它原生控件换成对应的。
using DevComponents.DotNetBar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; namespace Form1.Test { public partial class FormOwerDrawCreator : Form { public FormOwerDrawCreator() { InitializeComponent(); } [Serializable] class ItemInfo { String name;//名称 Rectangle rect; //区域 Color backColor;//背景颜色 Color foreColor;//前景颜色 Image img;//显示图标 Boolean isClickable;//是否能点击 Boolean isHaveTip;//是否有Tip Boolean isCanCheck;//是否允许选中 Boolean isChecked;//是否处于选中状态 Boolean visible;//是否显示 Font textFont;//字体大小 String text;//显示文本 StringAlignment textAlign;//文本对齐方式 StringAlignment textLineAlign;//文本垂直对齐方式 String toolTipText;//显示的TooTip; public Rectangle Rect { get => rect; set => rect = value; } public Color BackColor { get => backColor; set => backColor = value; } public Color ForeColor { get => foreColor; set => foreColor = value; } public Image Img { get => img; set => img = value; } public bool IsClickable { get => isClickable; set => isClickable = value; } public bool IsHaveTip { get => isHaveTip; set => isHaveTip = value; } public bool IsCanCheck { get => isCanCheck; set => isCanCheck = value; } public bool IsChecked { get => isChecked; set => isChecked = value; } public string Name { get => name; set => name = value; } public Font TextFont { get => textFont; set => textFont = value; } public string Text { get => text; set => text = value; } public StringAlignment TextAlign { get => textAlign; set => textAlign = value; } public StringAlignment TextLineAlign { get => textLineAlign; set => textLineAlign = value; } public bool Visible { get => visible; set => visible = value; } public string ToolTipText { get => toolTipText; set => toolTipText = value; } } //首字母小写 public string TitleToLower( string str) { if (string.IsNullOrWhiteSpace(str)) return string.Empty; char[] s = str.ToCharArray(); char c = s[0]; if ('A' <= c && c <= 'Z') c=char.ToLower(c); s[0] = c; return new string(s); } private void buttonXCreate_Click(object sender, EventArgs e) { Control control = ucPatientCard1; StringBuilder stringBuilderDefine = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder(); #region stringBuilderDefine.AppendLine("public class ItemInfo"); stringBuilderDefine.AppendLine("{"); stringBuilderDefine.AppendLine("private String name;//名称"); stringBuilderDefine.AppendLine("private Rectangle rect; //区域"); stringBuilderDefine.AppendLine("private Color backColor;//背景颜色"); stringBuilderDefine.AppendLine("private Color foreColor;//前景颜色"); stringBuilderDefine.AppendLine("private Image img;//显示图标"); stringBuilderDefine.AppendLine("private Boolean isClickable;//是否能点击"); stringBuilderDefine.AppendLine("private Boolean isHaveTip;//是否有Tip"); stringBuilderDefine.AppendLine("private Boolean isCanCheck;//是否允许选中"); stringBuilderDefine.AppendLine("private Boolean isChecked;//是否处于选中状态"); stringBuilderDefine.AppendLine("private Boolean visible;//是否显示"); stringBuilderDefine.AppendLine("private Font textFont;//字体大小"); stringBuilderDefine.AppendLine("private String text;//显示文本"); stringBuilderDefine.AppendLine("private String toolTipText;//显示的TooTip"); stringBuilderDefine.AppendLine("StringAlignment textAlign;//文本对齐方式"); stringBuilderDefine.AppendLine("StringAlignment textLineAlign;//文本垂直对齐方式"); stringBuilderDefine.AppendLine("public Rectangle Rect { get => rect; set => rect = value; }"); stringBuilderDefine.AppendLine("public Color BackColor { get => backColor; set => backColor = value; }"); stringBuilderDefine.AppendLine("public Color ForeColor { get => foreColor; set => foreColor = value; }"); stringBuilderDefine.AppendLine("public Image Img { get => img; set => img = value; }"); stringBuilderDefine.AppendLine("public bool IsClickable { get => isClickable; set => isClickable = value; }"); stringBuilderDefine.AppendLine("public bool IsHaveTip { get => isHaveTip; set => isHaveTip = value; }"); stringBuilderDefine.AppendLine("public bool IsCanCheck { get => isCanCheck; set => isCanCheck = value; }"); stringBuilderDefine.AppendLine("public bool IsChecked { get => isChecked; set => isChecked = value; }"); stringBuilderDefine.AppendLine("public string Name { get => name; set => name = value; }"); stringBuilderDefine.AppendLine("public Font TextFont { get => textFont; set => textFont = value; }"); stringBuilderDefine.AppendLine("public string Text { get => text; set => text = value; }"); stringBuilderDefine.AppendLine("public StringAlignment TextAlign { get => textAlign; set => textAlign = value; }"); stringBuilderDefine.AppendLine("public StringAlignment TextLineAlign { get => textLineAlign; set => textLineAlign = value; }"); stringBuilderDefine.AppendLine("public bool Visible { get => visible; set => visible = value; }"); stringBuilderDefine.AppendLine("public string ToolTipText { get => toolTipText; set => toolTipText = value; }"); stringBuilderDefine.AppendLine("}"); #endregion stringBuilderDefine.AppendFormat(" Dictionary<string, ItemInfo> dictionary = new Dictionary<string, ItemInfo>();\n"); for (int i = 0; i < control.Controls.Count; i++) { var v = control.Controls[i]; if (v.Tag == null) continue;//可能会有不可见控件忘记设置 ItemInfo itemInfo = new ItemInfo(); itemInfo.IsCanCheck = false; itemInfo.Name = v.Tag.ToString(); itemInfo.Img = null; itemInfo.ForeColor = v.ForeColor; itemInfo.BackColor = v.BackColor; itemInfo.IsChecked = false; itemInfo.IsClickable = false; itemInfo.IsHaveTip = false; itemInfo.TextFont = v.Font; stringBuilderDefine.AppendFormat("//说明|示例:{0}\n", v.Text); stringBuilderDefine.AppendFormat("ItemInfo {0}=new ItemInfo();\n", TitleToLower(itemInfo.Name)); stringBuilder.AppendFormat("{0}.Name=\"{1}\";\n", TitleToLower(itemInfo.Name), TitleToLower(itemInfo.Name)); stringBuilder.AppendFormat("{0}.Rect=new Rectangle({1},{2},{3},{4});\n", TitleToLower(itemInfo.Name), v.Bounds.Left,v.Bounds.Top,v.Bounds.Width,v.Bounds.Height); stringBuilder.AppendFormat("{0}.TextFont= new Font(\"{1}\",{2},FontStyle.{3});\n", TitleToLower(itemInfo.Name), v.Font.FontFamily.Name,Math.Round(v.Font.SizeInPoints),v.Font.Style); stringBuilder.AppendFormat("{0}.Text= \"{1}\";\n", TitleToLower(itemInfo.Name), v.Text); stringBuilder.AppendFormat("{0}.ForeColor= Color.FromArgb({1},{2},{3});\n", TitleToLower(itemInfo.Name), v.ForeColor.R,v.ForeColor.G,v.ForeColor.B); stringBuilder.AppendFormat("{0}.BackColor= Color.FromArgb({1},{2},{3});\n", TitleToLower(itemInfo.Name), v.BackColor.R, v.BackColor.G, v.BackColor.B); if (v.GetType().ToString() == "DevComponents.DotNetBar.LabelX") { stringBuilder.AppendFormat("{0}.TextAlign= StringAlignment.{1};\n", TitleToLower(itemInfo.Name), ((LabelX)(v)).TextAlignment); stringBuilder.AppendFormat("{0}.TextLineAlign= StringAlignment.{1};\n", TitleToLower(itemInfo.Name), ((LabelX)(v)).TextLineAlignment); } else if (v.GetType().ToString() == "DevComponents.DotNetBar.ButtonX") { stringBuilder.AppendFormat("{0}.TextAlign= StringAlignment.{1};\n", TitleToLower(itemInfo.Name), ((ButtonX)(v)).TextAlignment); stringBuilder.AppendFormat("{0}.TextLineAlign= StringAlignment.Center;\n", TitleToLower(itemInfo.Name)); } stringBuilder.AppendFormat("dictionary.Add(\"{0}\",{1});\n", itemInfo.Name, TitleToLower(itemInfo.Name)); } richTextBoxEx1.Text = "#region/////////////////////////////////控件定义部分/////////////////////////////////////\n"; richTextBoxEx1.Text += stringBuilderDefine.ToString()+"\n"; richTextBoxEx1.Text += "/////////////////////////////////控件初始化部分/////////////////////////////////////\n"; richTextBoxEx1.Text += "public void Init()"; richTextBoxEx1.Text += "{"; richTextBoxEx1.Text += stringBuilder.ToString(); richTextBoxEx1.Text += "}"; richTextBoxEx1.Text += "#endregion/////////////////////////////////控件初始化部分/////////////////////////////////////\n"; //Stream fStream = new FileStream(@"E:\测试\test.xml", FileMode.Create); //XmlSerializer xmlFormat = new XmlSerializer(typeof(Dictionary<string, ItemInfo>)); //xmlFormat.Serialize(fStream, dictionary);//序列化对象 } private void buttonXCopy_Click(object sender, EventArgs e) { richTextBoxEx1.SelectAll(); richTextBoxEx1.Copy(); //MessageBox.Show("COPY FININSH"); } private void ucTest1_Load(object sender, EventArgs e) { } } }
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。