具有強大的DesignSurface的大DesignTime經驗(擴展)類

本人英文不好,所以還請大家包涵呀。

原文地址: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
發佈了79 篇原創文章 · 獲贊 8 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章