本人英文不好,所以还请大家包涵呀。
原文地址:http://www.codeproject.com/Articles/24385/Have-a-Great-DesignTime-Experience-with-a-Powerful
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=60175&av=75878&display=Mobile
摘要:
这篇文章描述一个增加了设计功能(TabOrder, UndoEngine,对齐线/网格对齐)的.net 2.0 中DesignSurface扩展类。这个类托管在DLL程序集中并准备在.net解决方案中使用。
简介:
.net 2.0框架介绍DesignSurface是实现用户设计感知的类,这个类是从。Net 1.0中的DesignSurface 改善而来的。但是它任然忽略了一些对用户设计感知非常有用的设计功能。正如我现在说的: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实例。通过使用他的接口去创建根组件和你想要的任何.net 控件,最后将这个实例寄宿在控件中,例如一个窗体;
一个form的设计,简单的实例项目帮你演示DesignSurfaceExt的强大
明显地你可以做任何你想做的就像我在我创建的DemoConsole小form设计的程序一样!在这个小的设计程序中,你可以选择通过寄宿在4个TabPage上的4个设计页签:一个是通过使用对齐线功能使控件对齐,一个是使用“旧的”网格对齐,一个是使用没对齐线的网格自己对齐,最后一个是不使用任何对齐工具。移动已经部署的控件;剪切和复制或者剪切和粘贴从一个外观到另一个;撤销和恢复你的操作和使用TabOrder改变控件的TabIndex.
接口:
真正有用的特性是通过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?简单的说就是接口中激活和停用功能的方法。
surface.SwitchTabOrder();
The UndoEngine(撤销引擎的实现)
当前的撤销引擎,获得它通过GetUndoEngineExt()属性并调用Undo/Redo方法来使用它。
surface.GetUndoEngineExt().Undo();
surface.GetUndoEngineExt().Redo();
控件的Cut/Copy/Paste/Delete实现
最后是我们经常使用的cut/copy/paste and delete
surface.DoAction( "Cut" );
surface.DoAction( "Copy" );
surface.DoAction( "Paste" );
surface.DoAction( "Del" );
总结:
DesignTime真的与RunTime是有区别的,但.NET Framework 可以让我们以相对简单的方式实现它。享用它吧,以个人之见,它是非常有用的一类,如果你也这么觉得也请告诉我.
源码下载: http://download.csdn.net/detail/andycode/4153509