📄 formexporter.cs
字号:
using System;using System.Xml;using System.IO;using System.Windows.Forms;namespace FormExport{ /// <summary> /// Summary description for Class1. /// </summary> public class FormExporter { private System.Windows.Forms.Form m_frm; public FormExporter( System.Windows.Forms.Form frm) { m_frm = frm; } private static void ProcessBasicStuff( XmlElement el, Control ctl ) { el.SetAttribute( "width", System.Convert.ToString( ctl.Width ) ); el.SetAttribute( "height", System.Convert.ToString( ctl.Height ) ); el.SetAttribute( "top", System.Convert.ToString( ctl.Top ) ); el.SetAttribute( "left", System.Convert.ToString( ctl.Left ) ); } private static void ProcessButton( XmlDocument doc, XmlElement el, System.Windows.Forms.Button btn ) { XmlElement newEl; newEl = doc.CreateElement( "Button" ); ProcessBasicStuff( newEl, btn ); newEl.SetAttribute( "text", btn.Text ); el.AppendChild( newEl ); } private static void ProcessCheckBox( XmlDocument doc, XmlElement el, CheckBox cb ) { XmlElement newEl; newEl = doc.CreateElement( "CheckBox" ); ProcessBasicStuff( newEl, cb ); newEl.SetAttribute( "text", cb.Text ); newEl.SetAttribute( "checked", cb.Checked ? "true" : "false" ); el.AppendChild( newEl ); } private static void ProcessTextBox( XmlDocument doc, XmlElement el, TextBox tb ) { XmlElement newEl; newEl = doc.CreateElement( "TextField" ); ProcessBasicStuff( newEl, tb ); newEl.SetAttribute( "text", tb.Text ); el.AppendChild( newEl ); } private static void ProcessLabel( XmlDocument doc, XmlElement el, Label lbl ) { XmlElement newEl; newEl = doc.CreateElement( "Label" ); ProcessBasicStuff( newEl, lbl ); newEl.SetAttribute( "text", lbl.Text ); el.AppendChild( newEl ); } private static void ProcessProgressBar( XmlDocument doc, XmlElement el, ProgressBar pb ) { XmlElement newEl; newEl = doc.CreateElement( "ProgressBar" ); ProcessBasicStuff( newEl, pb ); // Progress Bar specific newEl.SetAttribute( "minimum", System.Convert.ToString( pb.Minimum ) ); newEl.SetAttribute( "maximum", System.Convert.ToString( pb.Maximum ) ); newEl.SetAttribute( "value", System.Convert.ToString( pb.Value ) ); el.AppendChild( newEl ); } private static void ProcessTrackBar( XmlDocument doc, XmlElement el, TrackBar pb ) { XmlElement newEl; newEl = doc.CreateElement( "Slider" ); ProcessBasicStuff( newEl, pb ); // Progress Bar specific newEl.SetAttribute( "minimum", System.Convert.ToString( pb.Minimum ) ); newEl.SetAttribute( "maximum", System.Convert.ToString( pb.Maximum ) ); newEl.SetAttribute( "value", System.Convert.ToString( pb.Value ) ); el.AppendChild( newEl ); } private static void ProcessNumericUpDown( XmlDocument doc, XmlElement el, NumericUpDown pb ) { XmlElement newEl; newEl = doc.CreateElement( "Spinner" ); ProcessBasicStuff( newEl, pb ); // Progress Bar specific newEl.SetAttribute( "minimum", System.Convert.ToString( pb.Minimum ) ); newEl.SetAttribute( "maximum", System.Convert.ToString( pb.Maximum ) ); newEl.SetAttribute( "value", System.Convert.ToString( pb.Value ) ); el.AppendChild( newEl ); } private static void ProcessControl( XmlDocument doc, XmlElement el, System.Windows.Forms.Control ctl ) { XmlElement newEl; int numControls = ctl.Controls.Count; if ( numControls == 0 ) return; for( int i = 0 ; i < numControls ; i++ ) { string t = ctl.Controls[i].GetType().Name.ToLower(); System.Console.WriteLine( t ); switch ( t ) { case "button": ProcessButton( doc, el, (Button)ctl.Controls[i] ); break; case "label": ProcessLabel( doc, el, (Label)ctl.Controls[i] ); break; case "checkbox": ProcessCheckBox( doc, el, (CheckBox)ctl.Controls[i] ); break; case "textbox": ProcessTextBox( doc, el, (TextBox)ctl.Controls[i] ); break; case "progressbar": ProcessProgressBar( doc, el, (ProgressBar)ctl.Controls[i] ); break; case "trackbar": ProcessTrackBar( doc, el, (TrackBar)ctl.Controls[i] ); break; case "numericupdown": ProcessNumericUpDown( doc, el, (NumericUpDown)ctl.Controls[i] ); break; default: newEl = doc.CreateElement( "Component" ); newEl.SetAttribute( "width", System.Convert.ToString( ctl.Controls[i].Width ) ); newEl.SetAttribute( "height", System.Convert.ToString( ctl.Controls[i].Height ) ); newEl.SetAttribute( "top", System.Convert.ToString( ctl.Controls[i].Top ) ); newEl.SetAttribute( "left", System.Convert.ToString( ctl.Controls[i].Left ) ); ProcessControl( doc, newEl, ctl.Controls[i] ); el.AppendChild( newEl ); break; } } } public XmlDocument Export() { XmlDocument ret = new XmlDocument(); XmlElement AppEl = ret.CreateElement( "Application" ); XmlElement WinEl = ret.CreateElement( "Window" ); WinEl.SetAttribute( "width", System.Convert.ToString( this.m_frm.Width ) ); WinEl.SetAttribute( "height", System.Convert.ToString( this.m_frm.Height ) ); WinEl.SetAttribute( "caption", System.Convert.ToString( this.m_frm.Text ) ); WinEl.SetAttribute( "centered", "true" ); ProcessControl( ret, WinEl, m_frm ); AppEl.AppendChild( WinEl ); ret.AppendChild( AppEl ); return ret; } public string ExportXml() { return Export().OuterXml; } public static void Main(String[] args){ if ( args.Length < 1 ) { System.Console.WriteLine( "Must supply a path argument" ); return; } string path = args[0]; FileInfo fi = new FileInfo( path ); string ext = fi.Extension; System.Console.WriteLine( ext ); Form frm; switch ( ext.ToLower() ) { case ".exe": frm = FormLoader.LoadFromAssembly( path ); break; case ".dll": frm = FormLoader.LoadFromAssembly( path ); break; case ".cs": frm = FormLoader.LoadFromSource( path ); break; default: System.Console.WriteLine( "Unsupported file extension: " + ext ); return; } if ( frm == null ) return; FormExporter fe = new FormExporter( frm ); BindowsAppCreator bac = new BindowsAppCreator( frm.Text ); bac.Markup = fe.ExportXml(); bac.Create(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -