0

由于表单System.Windows.Forms继承自 Control,我想知道是否有一种方法可以创建自定义表单及其设计器,其中包含一些选项(快捷方式)来创建标题或类似的东西。

我试过这个,但什么也没发生,我调用的表格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);
    }

}
4

1 回答 1

0

当您单击表单内控件上的小箭头时(如果对象是组件,则在设计器底部的组件上)显示操作列表。

您可以做的其他事情是管理动词。动词处理在 ControlDesigner 类(在您的情况下为 ManagedFormDesigner)上实现。您可以在单击鼠标右键或属性底部看到动词(即 TabControl 有 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;
    }
于 2014-04-11T08:05:23.897 回答