博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
写一个根据现有窗体生成自绘窗体代码
阅读量:5309 次
发布时间:2019-06-14

本文共 9943 字,大约阅读时间需要 33 分钟。

有时候需要自绘窗体,但是一个一个手动摆放,太麻烦,写了一个代码自动生成器,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
dictionary = new Dictionary
();\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
)); //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) { } }}

 

转载于:https://www.cnblogs.com/zhaogaojian/p/8426079.html

你可能感兴趣的文章
关于VMare中安装Ubuntu的一些说明
查看>>
字符串类型的相互转换
查看>>
HTTP状态码
查看>>
iOS如何过滤掉文本中特殊字符
查看>>
python - wmi模块学习(windwos硬件信息获取)
查看>>
Maven------使用maven新建web项目出现问题 项目名称出现红色交叉
查看>>
基础学习:C#中float的取值范围和精度
查看>>
Akka-Cluster(3)- ClusterClient, 集群客户端
查看>>
MongoDB-CRUD
查看>>
javaagent 简介
查看>>
python升级安装后的yum的修复
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
两数和
查看>>
移动设备和SharePoint 2013 - 第3部分:推送通知
查看>>
SOPC Builder中SystemID
查看>>
MySQL数据库备份工具mysqldump的使用(转)
查看>>