禁用默认上下文菜单 [英] Disable default context menu

查看:137
本文介绍了禁用默认上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义工具条组合框,以允许它显示上下文菜单。这是代码。

I have created a custom toolstrip combobox to allow it display a context menu. Here is the code.

Option Strict Off
Option Explicit On

<System.ComponentModel.Description("Custom Control to add a context menu to a toolstrip combobox")> _
Public Class MyToolStripComboBox
    Inherits Forms.ToolStripComboBox
    Private mContextMenu As ContextMenuStrip
    ' allows a context menu to be added to a toolstrip combobox control
    Public Property ContextMenuStrip() As ContextMenuStrip
        Get
            Return mContextMenu
        End Get
        Set(ByVal iConMenu As ContextMenuStrip)
            mContextMenu = iConMenu
        End Set
    End Property

    Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        If e.Button = MouseButtons.Right Then
            If mContextMenu IsNot Nothing Then
                Dim iBounds = Me.Bounds
                Dim iParLoc As Point = Parent.Location
                Dim iPt As Point = iParLoc
                iPt.X += iBounds.Left
                iPt.Y += iBounds.Bottom
                mContextMenu.Show(Parent.PointToScreen(iPt))
            End If
        End If

    End Sub
End Class





除了默认的上下文菜单条(即剪切,复制,粘贴...)显示在它的顶部。



如何禁用默认的上下文菜单条?



我已经确定了问题的根源。使用上面的代码设置Y坐标,默认上下文菜单将显示在控件特定上下文菜单(mContextMenu)的顶部。如果注释掉该行或者Y坐标设置为



It works fine except that the default context menu strip (i.e. cut, copy, paste...) displays over the top of it.

How do I disable the default context menu strip?

I have determined the source of the issue. With the above code setting the Y coordinate, the default context menu will display on top of the control specific context menu (mContextMenu). If this line is remarked out or if the Y coordinate is set to

e.Location.Y

,则不会显示默认上下文菜单。此外,如果mContextMenu设置显示在

the default context menu will not display. Also, if mContextMenu is set display at

e.Location

mContextMenu.Show(Parent.PointToScreen(e.Location))

,默认上下文菜单将显示在控件上,mContextMenu将显示在包含工具条的位置(在工具条的左端)。我不知道为什么会这样。我希望有人可以向我解释这种行为。



这个问题是当显示上下文菜单时用户无法看到控件的内容。它就在控制之上。



我尝试过:



在使用此控件的表单上,我在启动时将容器的工具条设置为新的(空白)上下文菜单:

, the default context menu will display at the control and mContextMenu will display at the location on the containing toolstrip (at the left end of the toolstrip). I do not know why this is. I wish someone could explain this behavior to me.

The problem with this is that the user cannot see the contents of the control when the context menu is displayed. It is right on top of the control.

What I have tried:

On the form that uses this control I have set the toolstrip of the container to a new (blank) context menu at startup:

tsMain.ContextMenuStrip = New ContextMenuStrip





另外,我创建了一个新的空白上下文菜单并将其分配给tsMain设计。



当我右键单击该项时,这些方法都不会阻止显示默认的上下文菜单条。



Also, I have created a new blank context menu and assigned this to tsMain in design.

Neither of these methods prevents the default context menu strip from display when I right-click on the item.

推荐答案

我认为问题是新的ContextMenuStrip尚未分配给ToolStripComboBox。一旦完成它将只是工作,MouseDown处理程序和定位代码将是不必要的。



ToolStripComboBox是System.Windows.Forms.ComboBox的一个相当简单的包装器,并没有公开所有底层功能,但有一个属性可以。找到ToolStripComboBox.ComboBox.ContextMenuStrip属性并在其中填充新菜单。一旦完成,老式的原生菜单就会消失。



Alan。
I think the problem is that the new ContextMenuStrip has not been assigned to the ToolStripComboBox. Once that is done it will just work and the MouseDown handler and positioning code will be unnecessary.

The ToolStripComboBox is a fairly simple wrapper around a System.Windows.Forms.ComboBox and it does not expose all of the underlying functionality but there is a property which does. Find the ToolStripComboBox.ComboBox.ContextMenuStrip property and stuff the new menu in there. Once that has been done the old fashioned native menu will be gone.

Alan.


这篇关于禁用默认上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆