c# - .NET 自定义表单设计器 : How to implement IMenuCommandService?

标签 c# winforms .net-2.0 form-designer

我有一个很久以前为数据库项目编写的报表设计器。它使用了很多 winapi 魔法,因此我被迫“以适当的方式”重写了一些部分。

感谢来自 MSDN 杂志的一些文章(herehere)和 CodeProject我能够实现设计器界面、工具箱和撤消/重做引擎。

  1. 到目前为止,我发现的关于该主题的所有资源都有点过时了。你能指出新鲜/全面的文章吗?

  2. 代码来自 article上面提到的似乎不起作用。

    MenuCommandService.ShowContextMenu 被调用但没有出现,因为 globalVerbs 集合中没有任何 DesignerVerbs。我应该添加“标准”的,对应于设计师的操作,如手动剪切/粘贴?如果是,我该如何完成?

最佳答案

感谢 SharpDevelop 资源,我能够找到解决方案

这个最小的实现(一些标准命令,仅此而已)对我有用

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace DesignerHost
{
    class MenuCommandServiceImpl : MenuCommandService
    {
        DesignerVerbCollection m_globalVerbs = new DesignerVerbCollection();

        public MenuCommandServiceImpl(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
            m_globalVerbs.Add(StandartVerb("Cut", StandardCommands.Cut));
            m_globalVerbs.Add(StandartVerb("Copy", StandardCommands.Copy));
            m_globalVerbs.Add(StandartVerb("Paste", StandardCommands.Paste));
            m_globalVerbs.Add(StandartVerb("Delete", StandardCommands.Delete));
            m_globalVerbs.Add(StandartVerb("Select All", StandardCommands.SelectAll));

        }

        private DesignerVerb StandartVerb(string text, CommandID commandID)
        {
            return new DesignerVerb(text,
                delegate(object o, EventArgs e) 
                {
                    IMenuCommandService ms = 
                        GetService(typeof(IMenuCommandService)) as IMenuCommandService;
                    Debug.Assert(ms != null);
                    ms.GlobalInvoke(commandID); 
                }
            );
        }

        class MenuItem : ToolStripMenuItem
        {
            DesignerVerb verb;

            public MenuItem(DesignerVerb verb)
                : base(verb.Text)
            {
                Enabled = verb.Enabled;
                this.verb = verb;
                Click += InvokeCommand;
            }

            void InvokeCommand(object sender, EventArgs e)
            {
                try
                {
                    verb.Invoke();
                }
                catch (Exception ex)
                {
                    Trace.Write("MenuCommandServiceImpl: " + ex.ToString());
                }
            }
        }

        private ToolStripItem[] BuildMenuItems()
        {
            List<ToolStripItem> items = new List<ToolStripItem>();

            foreach (DesignerVerb verb in m_globalVerbs) 
            {
                items.Add(new MenuItem(verb));
            }
            return items.ToArray();
        }

        #region IMenuCommandService Members

        /// This is called whenever the user right-clicks on a designer.
        public override void ShowContextMenu(CommandID menuID, int x, int y)
        {
            // Display our ContextMenu! Note that the coordinate parameters to this method
            // are in screen coordinates, so we've got to translate them into client coordinates.

            ContextMenuStrip cm = new ContextMenuStrip();
            cm.Items.AddRange(BuildMenuItems());

            ISelectionService ss = GetService(typeof (ISelectionService)) as ISelectionService;
            Debug.Assert(ss != null);

            Control ps = ss.PrimarySelection as Control;
            Debug.Assert(ps != null);

            Point s = ps.PointToScreen(new Point(0, 0));
            cm.Show(ps, new Point(x - s.X, y - s.Y));
        }

        #endregion

    }
}

更新

找到 similar solution

关于c# - .NET 自定义表单设计器 : How to implement IMenuCommandService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/888845/

相关文章:

c# - Visual Studio 2008 与 2005 或 C# 3.0 与 C# 2.0 的新增功能是什么?

c# - 如果 T 在泛型中为空怎么办?如何省略尖括号

c# - 如何将 IList<object> 转换为字符串数组?

C# 调整文本框大小以适应内容

c# - 使用 fiddlercore 后关闭应用程序时代理设置未更改

c# - WinForms ListView.HideSelection 属性没有效果?

c# - 通过 C#/Graph 禁用 MS Teams 邀请邮件

c# - IValueConverter 和可见性

.net - 为什么 New() 在我继承的控件上触发两次? (winforms)

c# - 在 C# 2.0(Windows 窗体)中使用注册表