2014-02-26 36 views
0

由於System.Windows.Forms的表單是繼承自Control的,我想知道是否有辦法創建一個自定義窗體及其設計器,並用一些選項(快捷方式)創建一個標題或類似的話。用c#創建智能標籤(不是其他控件)使用c#

我想這一點,但是沒有什麼happend,形式I卡列斯ManagedForm

[Designer(typeof(ManagedFormDesigner))] 
public class ManagedForm : Form{ 
    //code here 
} 


[PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
public class ManagedFormDesigner : ControlDesigner { 
    private DesignerActionListCollection actionLists; 
    public override DesignerActionListCollection ActionLists { 
     get { 
      if (actionLists == null) { 
       actionLists = new DesignerActionListCollection(); 
       actionLists.Add(new ManagedFormDesignerActionList(this.Component)); 
      } 
      return actionLists; 
     } 
    } 
} 



public class ManagedFormDesignerActionList : DesignerActionList { 
    private ManagedForm managedForm = null; 
    private DesignerActionUIService designerActionUISvc = null; 

    public ManagedFormDesignerActionList(IComponent component) : base(component) { 
     this.managedForm = component as ManagedForm; 
     this.designerActionUISvc = 
     GetService(typeof(DesignerActionUIService)) 
     as DesignerActionUIService; 
    } 

    public override DesignerActionItemCollection GetSortedActionItems() { 
     DesignerActionItemCollection items = new DesignerActionItemCollection(); 
     items.Add(new DesignerActionMethodItem(this, "CreateTitle", "Create Title", "Appearence", true)); 
     return items; 
    } 

    public void CreateTitle() { 
     Panel pTitulo = new Panel(); 
     pTitulo.Size= new Size(100,25); 
     pTitulo.Dock = DockStyle.Top; 
     (this.Component as ManagedForm).Controls.Add(pTitulo); 
    } 

} 

回答

0

動作列表是顯示當你的小箭頭點擊控件的窗體中(或底部的組件設計師如果該對象是一個組件)。

你可以做的其他事情是管理動詞。 動詞處理在ControlDesigner類(您的案例中的ManagedFormDesigner)上實現。 您可以看到動詞單擊鼠標右鍵或屬性的底部(即TabControl ha 2動詞,添加選項卡和刪除選項卡)。

您可以實現動詞增加ControlDesigner(或ComponentDesigner)類這樣的事情

private DesignerVerbCollection _verbs; 

    public override DesignerVerbCollection Verbs 
    { 
     get 
     { 
      if (_verbs == null) 
      { 
       _verbs = new DesignerVerbCollection(); 
       _verbs.Add(new DesignerVerb("Create Title", new EventHandler(MyCreateTitleHandler))); 
      } 
      return _verbs; 
     } 
    } 

    private void MyCreateTitleHandler(object sender, EventArgs e) 
    { 
     // Do here something but take care to show things via IUIService service 
     IUIService uiService = GetService(typeof(IUIService)) as IUIService; 
    }