使用 c# 为窗体(不是其他控件)创建智能标记



由于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);
    }
}
单击

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

您可以做的其他事情是管理动词。谓词处理是在 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;
    }

最新更新