将菜单项添加到默认上下文菜单

时间:2021-01-30 07:31:49

I'd like to add a menu item to the default ContextMenu of a RichTextBox.

我想将一个菜单项添加到RichTextBox的默认ContextMenu中。

I could create a new context menu but then I lose the spell check suggestions that show up in the default menu.

我可以创建一个新的上下文菜单但是我丢失了默认菜单中显示的拼写检查建议。

Is there a way to add an item without re-implementing everything?

有没有办法添加项目而不重新实现一切?

1 个解决方案

#1


16  

It's not too tricky to reimplement the RichTextBox context menu with spelling suggestions, Cut, Paste, etc.

使用拼写建议,剪切,粘贴等重新实现RichTextBox上下文菜单并不太棘手。

Hook up the context menu opening event as follows:

连接上下文菜单打开事件,如下所示:

AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);

Within the event handler build the context menu as you need. You can recreate the existing context menu menu items with the following:

在事件处理程序中根据需要构建上下文菜单。您可以使用以下内容重新创建现有的上下文菜单项:

private IList<MenuItem> GetSpellingSuggestions()
{
    List<MenuItem> spellingSuggestions = new List();
    SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
    if (spellingError != null)
    {
        foreach (string str in spellingError.Suggestions)
        {
            MenuItem mi = new MenuItem();
            mi.Header = str;
            mi.FontWeight = FontWeights.Bold;
            mi.Command = EditingCommands.CorrectSpellingError;
            mi.CommandParameter = str;
            mi.CommandTarget = myRichTextBox;
            spellingSuggestions.Add(mi);
        }
    }
    return spellingSuggestions;
}

private IList<MenuItem> GetStandardCommands()
{
    List<MenuItem> standardCommands = new List();

    MenuItem item = new MenuItem();
    item.Command = ApplicationCommands.Cut;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Copy;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Paste;
    standardCommands.Add(item);

    return standardCommands;
}

If there are spelling errors, you can create Ignore All with:

如果存在拼写错误,您可以使用以下命令创建全部忽略:

MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "Ignore All";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
ignoreAllMI.CommandTarget = textBox;
newContextMenu.Items.Add(ignoreAllMI);

Add separators as required. Add those to the new context menu's items, and then add your shiny new MenuItems.

根据需要添加分隔符。将它们添加到新的上下文菜单的项目,然后添加闪亮的新MenuItems。

I'm going to keep looking for a way to obtain the actual context menu though, as this is relevant to something I'll be working on in the near future.

我将继续寻找获得实际上下文菜单的方法,因为这与我将在不久的将来工作的内容相关。

#1


16  

It's not too tricky to reimplement the RichTextBox context menu with spelling suggestions, Cut, Paste, etc.

使用拼写建议,剪切,粘贴等重新实现RichTextBox上下文菜单并不太棘手。

Hook up the context menu opening event as follows:

连接上下文菜单打开事件,如下所示:

AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);

Within the event handler build the context menu as you need. You can recreate the existing context menu menu items with the following:

在事件处理程序中根据需要构建上下文菜单。您可以使用以下内容重新创建现有的上下文菜单项:

private IList<MenuItem> GetSpellingSuggestions()
{
    List<MenuItem> spellingSuggestions = new List();
    SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
    if (spellingError != null)
    {
        foreach (string str in spellingError.Suggestions)
        {
            MenuItem mi = new MenuItem();
            mi.Header = str;
            mi.FontWeight = FontWeights.Bold;
            mi.Command = EditingCommands.CorrectSpellingError;
            mi.CommandParameter = str;
            mi.CommandTarget = myRichTextBox;
            spellingSuggestions.Add(mi);
        }
    }
    return spellingSuggestions;
}

private IList<MenuItem> GetStandardCommands()
{
    List<MenuItem> standardCommands = new List();

    MenuItem item = new MenuItem();
    item.Command = ApplicationCommands.Cut;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Copy;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Paste;
    standardCommands.Add(item);

    return standardCommands;
}

If there are spelling errors, you can create Ignore All with:

如果存在拼写错误,您可以使用以下命令创建全部忽略:

MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "Ignore All";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
ignoreAllMI.CommandTarget = textBox;
newContextMenu.Items.Add(ignoreAllMI);

Add separators as required. Add those to the new context menu's items, and then add your shiny new MenuItems.

根据需要添加分隔符。将它们添加到新的上下文菜单的项目,然后添加闪亮的新MenuItems。

I'm going to keep looking for a way to obtain the actual context menu though, as this is relevant to something I'll be working on in the near future.

我将继续寻找获得实际上下文菜单的方法,因为这与我将在不久的将来工作的内容相关。