通过强大的 DesignSurface (Extended) 类获得出色的设计时体验






4.93/5 (52投票s)
使用设计工具(TabOrder、UndoEngine、捕捉线/网格对齐)来设计 WinForms

摘要
本文介绍了一个继承自 DesignSurface
的类,它向 .NET 2.0 类添加了一些设计工具(TabOrder、UndoEngine、捕捉线/网格对齐)。 这个类托管在一个 DLL 程序集中,可以随时在任何 .NET 解决方案中使用。
引言
.NET 2.0 Framework 引入了 DesignSurface 类,它实现了用户所感知的“设计器”。 这个类是对 .NET 1.0 的改进,但仍然缺少一些对想要设计窗体的用户非常有用的设计工具。 我指的是:TabOrder、UndoEngine 和捕捉线/网格对齐。 所有这些功能都可以通过少量努力添加(可能除了 TabOrder),但我希望让任何想要制作自己的“设计器”的人的生活更轻松一些。
背景
您应该熟悉基本的 C# 编程以及 .NET 设计器使用的一些概念(特别是如果您深入研究源代码)。
如何使用 DesignSurfaceExt
简而言之
using DesignSurfaceExt;
...
Form frm = new Form();
IDesignSurfaceExt surface = new DesignSurfaceExt.DesignSurfaceExt();
surface.CreateRootComponent( typeof( Form ), new Size( 400, 400 ) );
surface.CreateControl( typeof( Button ), new Size( 100, 40 ), new Point( 10, 10 ) );
TextBox t1 = (TextBox) surface.CreateControl( typeof( TextBox ),
new Size( 300, 20 ), new Point( 10, 80 ) );
t1.Text = "Hello World by DesignSurfaceExt";
surface.GetView().Parent = frm;
frm.ShowDialog();
上面的代码片段只是简单地创建了一个新的 DesignSurfaceExt
实例。 通过它的 interface
来创建根组件和你想要的任何 .NET 控件,最后将 designsurface
托管到一个控件中,例如一个窗体; 这就是全部!
Tiny Form Designer,一个简单的演示项目,向您展示 DesignSurfaceExt 的强大功能
显然,你可以做任何你想做的事情,就像我在 DemoConsole
项目中向你展示的那样,我在那里创建了一个 Tiny Form Designer! 使用这个小设计器,你可以切换由四个 TabPages 托管的四个设计表面:一个让你使用捕捉线工具对齐控件;一个使用“旧”的网格;一个使用网格但不捕捉到网格本身;最后一个不提供任何对齐工具。 移动那些已经以编程方式部署的控件;从一个表面剪切和复制或剪切和粘贴到另一个表面;撤消和重做你的操作,并通过 TabOrder 工具更改控件的 TabIndex。
接口
真正有用的功能通过 interface IDesignSurfaceExt
公开,但是,如果你愿意,你可以忽略它并使用 DesignSurfaceExt
实例本身。
public interface IDesignSurfaceExt {
//- perform Cut/Copy/Paste/Delete commands
void DoAction( string command );
//- de/activate the TabOrder facility
void SwitchTabOrder();
//- select the controls alignment mode
void UseSnapLines();
void UseGrid ( System.Drawing.Size gridSize );
void UseGridWithoutSnapping ( System.Drawing.Size gridSize );
void UseNoGuides();
//- method useful to create control without the ToolBox facility
IComponent CreateRootComponent ( Type controlType, Size controlSize );
Control CreateControl ( Type controlType, Size controlSize, Point controlLocation );
//- Get the UndoEngineExtended object
UndoEngineExt GetUndoEngineExt();
//- Get the IDesignerHost of the .NET 2.0 DesignSurface
IDesignerHost GetIDesignerHost();
//- the View of the .NET 2.0 DesignSurface is just a Control
//- you can manipulate this Control just like any other WinForms Control
//- (you can dock it and add it to another Control just to display it)
//- Get the View
Control GetView();
}//end_interface
捕捉线/网格对齐指南
通过设置正确的属性,可以使用捕捉线来对齐 DesignSurface
托管的控件
surface.UseSnapLines();
如果您更喜欢使用网格来对齐 DesignSurface
托管的控件,则必须在调用正确的属性时指定网格的大小
surface.UseGrid ( new System.Drawing.Size ( 16, 16 ) );
您也可以完全不使用指南来对齐控件
surface.UseGridWithoutSnapping ( new System.Drawing.Size ( 32, 32 ) );
surface.UseNoGuides();
TabOrder
Taborder 呢? 只需调用 interface
方法来激活和停用该工具
surface.SwitchTabOrder();
UndoEngine
现在是 UndoEngine! 通过 Property 获取它:GetUndoEngineExt()
并使用它调用 Undo
/Redo
操作
surface.GetUndoEngineExt().Undo();
surface.GetUndoEngineExt().Redo();
剪切/复制/粘贴/删除控件
最后,像往常一样使用剪切/复制/粘贴和删除
surface.DoAction( "Cut" );
surface.DoAction( "Copy" );
surface.DoAction( "Paste" );
surface.DoAction( "Del" );
结论
DesignTime
与 RunTime
完全不同,但 .NET Framework 让我们以相对简单的方式处理它。 享受这个,恕我直言,有用的类,如果您觉得它有用,请告诉我!
历史
- 2008 年 3 月 14 日 - 首次发布