C#用DesignSurface实现一个简单的窗体设计器



  System.ComponentModel.Design.DesignSurface是为设计组件提供一个用户界面,通过它可以实现一个简单的窗体设计器。

    在构建之前,我们需要引入System.Design.dll,否则会出现找不到DesignSurface的错误。

复制代码
 1         private void Form1_Load(object sender, EventArgs e)
 2         {
 3            //引用System.Deisgn.dll
 4            DesignSurface ds = new DesignSurface();
 5             //开始加载窗体
 6             ds.BeginLoad(typeof(Form));
 7             Control designerContorl = (Control)ds.View;
 8             designerContorl.Dock = DockStyle.Fill;
 9             this.Controls.Add(designerContorl);
10         }
复制代码

运行后出现简单的一个UI设计器

但是该设计器并不能实现控件拖放和UI设计器,以及控件的属性配置。

为了支持从源代码加载初始化窗体,需要对源码中的相关方法进行解析,这里我们 CodeDomDesignerLoader来实现定制化业务,CodeDomDesignerLoader是提供用于实现基于 CodeDOM 的设计器加载程序的基类。

继承它的类需要重写CodeCompileUnit Parse()方法,来实现加载窗体:

复制代码
 1         protected override CodeCompileUnit Parse()
 2         {
 3          
 4             #region 源文件读取
 5             var sw = new StreamReader(@"E:\FrmUser.cs");
 6             var sw_designer = new StreamReader(@"E:\FrmUser.Designer.cs");
 7 
 8             string formCodeCS = sw.ReadToEnd();
 9             string formCodeDesigner = sw_designer.ReadToEnd();
10 
11             List<string> source = new List<string>();
12             source.Add(formCodeCS);
13             source.Add(formCodeDesigner);
14 
15             #endregion
16             //Rolsyn解析C#
17             var rootDesigner = Source2CodeDom.Parse(formCodeDesigner);
18             codeDesingerCompileUnit = Source2CodeDom.GetDesignerCodeComplieUnit(rootDesigner);
19             var rootCS = Source2CodeDom.Parse(formCodeCS);
20             codeCSCompileUnit = Source2CodeDom.GetCodeComplieUnit(rootCS);
21             //MergeFormSource
22             string mergeS = Source2CodeDom.MergeFormSource(formCodeDesigner, formCodeCS);
23             codeMergeCompileUnit = Source2CodeDom.GetMergeDesignerCodeComplieUnit(mergeS);
24             return codeMergeCompileUnit;
复制代码

解析的方法如下,但是此解析只是用于代码的生成,并不能用户UI界面的显示:

复制代码
  1        public static CodeCompileUnit GetDesignerCodeComplieUnit2(CompilationUnitSyntax root)
  2         {
  3             CodeCompileUnit ccu = new CodeCompileUnit();
  4             var firstMember = root.Members[0];
  5             var namespaceDeclration = (NamespaceDeclarationSyntax)firstMember;
  6             var designClassDeclaration = (ClassDeclarationSyntax)namespaceDeclration.Members[0];
  7             var myDesignerClass = new CodeTypeDeclaration(designClassDeclaration.Identifier.ToString());
  8             var initializeComponent = new CodeMemberMethod();
  9             var ns = new CodeNamespace(namespaceDeclration.Name.ToString());
 10 
 11             foreach (var m in designClassDeclaration.Members)
 12             {
 13 
 14                 if (m is ConstructorDeclarationSyntax)
 15                 {
 16                     var ctor = ((ConstructorDeclarationSyntax)m);
 17                     var codeBody = ctor.Body.ToString();
 18                     codeBody = codeBody.Trim().TrimStart('{
    ').TrimEnd('}').Trim().TrimEnd(';');
 19                     CodeSnippetExpression csbody = new CodeSnippetExpression(codeBody);
 20                     CodeExpressionStatement stmt = new
  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C#非常好的学习源码资料 【核心代码】 ├── FormDesigner │ └── FormDesigner-master │ ├── Core │ │ ├── Analysis.cs │ │ ├── Collections.cs │ │ ├── ControlHelper.cs │ │ ├── ControlSave.cs │ │ ├── CustomProperty │ │ │ ├── CustomProperty.cs │ │ │ ├── CustomPropertyCollection.cs │ │ │ └── CustomPropertyDescriptor.cs │ │ ├── GetFileEncoding.cs │ │ ├── MessageFilter.cs │ │ ├── OldAnalysis.cs │ │ ├── Program.cs │ │ ├── Recter.cs │ │ ├── SQLStringEditor.cs │ │ ├── Select.cs │ │ ├── Tools.cs │ │ └── UndoAndRedo.cs │ ├── FormDesigner.csproj │ ├── FormDesigner.sln │ ├── Forms │ │ ├── AddControlDialog.Designer.cs │ │ ├── AddControlDialog.cs │ │ ├── AddControlDialog.resx │ │ ├── HostFrame.Designer.cs │ │ ├── HostFrame.cs │ │ ├── HostFrame.resx │ │ ├── Notepad.Designer.cs │ │ ├── Notepad.cs │ │ └── Notepad.resx │ ├── FromDesigner.Designer.cs │ ├── FromDesigner.cs │ ├── FromDesigner.resx │ ├── Icon │ │ ├── align_add_level_72px.png │ │ ├── align_add_vertica_72px.png │ │ ├── align_bottom_72px.png │ │ ├── align_box_all_72px.png │ │ ├── align_box_level_72px.png │ │ ├── align_box_vertical_72px.png │ │ ├── align_center_level_72px.png │ │ ├── align_center_vertical_72px.png │ │ ├── align_left_72px.png │ │ ├── align_right_72px.png │ │ ├── align_top_72px.png │ │ ├── edit_128px_blue.ico │ │ ├── edit_128px_yellow.ico │ │ ├── edit_72px_blue.ico │ │ ├── notepad_128px_blue.ico │ │ ├── null.png │ │ ├── reset_center_72px.png │ │ ├── reset_justify_72pxpng.png │ │ ├── reset_left_72px.png │ │ ├── reset_left_indent_72px.png │ │ ├── reset_list_72px.png │ │ ├── reset_numbered_list_72px.png │
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值