gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:50:09 28 4
gpt4 key购买 nike

我有一个很久以前为数据库项目编写的报表设计器。它使用了很多 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com