改变内置的ContextMenu [英] alter built-in ContextMenu

查看:82
本文介绍了改变内置的ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个继承自ComboBox的控件,我想在该组合框的ContextMenu中添加一个

条目。我没有问题,如果我从头开始创建一个全新的ContextMenu,但我更愿意只需将我的MenuItem添加到现有的内置ContextMenu上(剪切,复制,

粘贴等):


_RemoveMRUItemMenu = New MenuItem

_RemoveMRUItemMenu.Text ="& ;删除项目

_RemoveMRUItemMenu.Enabled = False


MyBase.ContextMenu.MenuItems.Add(_RemoveMRUItemMen u)


AddHandler _RemoveMRUItemMenu.Click,AddressOf

RemoveCurrentMRUItem

这不起作用; MyBase.ContextMenu返回Nothing,即使

*是*现有的ContextMenu(内置的)。


是编写代码的唯一方法手动执行通常的TextBox

上下文菜单命令?或者我可以写一个继承自

的ContextMenu类似TextBoxContextMenu的东西吗?


谢谢,


g 。

I''m writing a control inheriting from ComboBox, and I''d like to add an
entry to the ContextMenu for that combo box. I have no problem if I
create an entirely new ContextMenu from scratch, but I''d prefer just to
tack my MenuItem on to the existing, built-in ContextMenu (Cut, Copy,
Paste, etc.):

_RemoveMRUItemMenu = New MenuItem
_RemoveMRUItemMenu.Text = "&Remove Item"
_RemoveMRUItemMenu.Enabled = False

MyBase.ContextMenu.MenuItems.Add(_RemoveMRUItemMen u)

AddHandler _RemoveMRUItemMenu.Click, AddressOf
RemoveCurrentMRUItem
This doesn''t work; MyBase.ContextMenu returns Nothing, even though
there *is* an existing ContextMenu (the built-in one).

Is the only way to write code to manually perform the usual TextBox
context menu commands? Or can I write a ContextMenu inheriting from
something like "TextBoxContextMenu"?

Thanks,

g.

推荐答案

您好格雷厄姆,


感谢您的帖子。


不,通常,没有简单的方法来完成这项工作。组合框/文本框的默认上下文

菜单是一个win32内置上下文菜单,它是

而不是.Net ContextMenu类。摆脱它的唯一方法是为Control.ContextMenu属性提供一个

..Net ContextMenu,然后.Net

ContextMenu将替换默认的上下文菜单。


我不确定你为什么不想使用新的.Net ContextMenu来取代默认的上下文菜单。实际上,我们可以在没有

的新.Net ContextMenu中实现默认的

上下文菜单剪切/复制/粘贴操作。下面是一个小小的VB.net示例:
http:// www .startvbdotnet.com / controls / contextmenu.aspx


如果您有任何进一步的顾虑,请随时告诉我,我会工作
$ b $和你在一起谢谢


祝你好运,

Jeffrey Tan

微软在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有任何保证,也没有授予任何权利。

Hi Graham,

Thanks for your post.

No, normally, there is no easy way to get this done. The default context
menu for the combobox/textbox is a win32 build-in context menu, which is
not a .Net ContextMenu class. The only way to get rid of it is supply a
..Net ContextMenu for the Control.ContextMenu property, then the .Net
ContextMenu will replace the default context menu.

I am not sure of why you do not want to use a new .Net ContextMenu to
substitute the default context menu. Actually, we can implement the default
context menu cut/copy/paste operations in the new .Net ContextMenu without
any problem. Below is a little VB.net sample:
http://www.startvbdotnet.com/controls/contextmenu.aspx

If you have any further concern, please feel free to tell me, I will work
with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


非常棒,除了不太容易实现的ComboBox

类,其中缺少Copy(),Paste(),...方法。


任何想法?

Brilliant, except not quite so easy to implement for the ComboBox
class, which lacks Copy(), Paste(), ... methods.

Any ideas?


嗨格雷厄姆,


感谢您的反馈。


对于Combobox,它由Edit控件和下拉列表合成。所以我们

可以先在ComboBox中找到Edit控件,然后在这个Edit控件上复制/剪切/粘贴

操作。


如果你使用Reflector来查看TextBox.Cut / Copy / Paste

方法的源代码,你会发现,他们只是p /调用SendMessage API来发送

WM_COPY,WM_CUT和WM_PASTE到TextBox(编辑)控件。所以我们可以做同样的事情。

。以下是示例代码段:


声明函数FindWindowEx Lib" user32"别名_

" FindWindowExA" (ByVal hWnd1 As IntPtr,_

ByVal hWnd2 As IntPtr,ByVal lpsz1 As String,_

ByVal lpsz2 As String)As IntPtr

< ;的DllImport(QUOT; USER32")> _

公共共享函数SendMessage(ByVal hWnd As IntPtr,ByVal msg As

Integer,ByVal wParam As Integer,ByVal lParam As Integer)As IntPtr

结束函数


Const WM_COPY As Integer = 769

Const WM_CUT As Integer = 768

Const WM_PASTE As Integer = 770

Private Sub MenuItem1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)Handles MenuItem1.Click

Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,

IntPtr.Zero," Edit",String.Empty)

如果hEdit.Equals(IntPtr.Zero)那么

MessageBox.Show(Marshal.GetLastWin32Error())

否则

SendMessage(hEdit,WM_COPY,0,0)

结束如果


End Sub


Private Sub MenuItem2_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MenuItem2.Click

Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,

IntPtr.Zero,Edit",String.Empty

如果hEdit.Equals(IntPtr.Zero)那么

MessageBox.Show(Marshal.GetLastWin32Error())

Else

SendMessage(hEdit,WM_CUT,0,0)

结束如果


结束Sub $ />

Private Sub MenuItem3_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)Handles MenuItem3.Click

Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,

IntPtr.Zero," Edit",String.Empty)

如果是hEdit.Equals(IntPtr。零)然后

MessageBox.Show(Marshal.GetLastWin32Error())

否则

SendMessage(hEdit,WM_PASTE,0,0)

结束如果

结束子


这对我很有帮助。希望它有所帮助


祝你好运,

Jeffrey Tan

微软在线合作伙伴支持

获取安全保障! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。

Hi Graham,

Thanks for your feedback.

For Combobox, it is composited by an Edit control and a dropdownlist. So we
can first find the Edit control in the ComboBox, then do the copy/cut/paste
operations on this Edit control.

If you use Reflector to view the source code of TextBox.Cut/Copy/Paste
methods, you will see that, they simply p/invokes SendMessage API to send
WM_COPY,WM_CUT and WM_PASTE to the TextBox(Edit) control. So we can do the
same thing. Below is the sample code snippet:

Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As IntPtr, _
ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As IntPtr
<DllImport("user32")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function

Const WM_COPY As Integer = 769
Const WM_CUT As Integer = 768
Const WM_PASTE As Integer = 770
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,
IntPtr.Zero, "Edit", String.Empty)
If hEdit.Equals(IntPtr.Zero) Then
MessageBox.Show(Marshal.GetLastWin32Error())
Else
SendMessage(hEdit, WM_COPY, 0, 0)
End If

End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,
IntPtr.Zero, "Edit", String.Empty)
If hEdit.Equals(IntPtr.Zero) Then
MessageBox.Show(Marshal.GetLastWin32Error())
Else
SendMessage(hEdit, WM_CUT, 0, 0)
End If

End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click
Dim hEdit As IntPtr = FindWindowEx(Me.ComboBox1.Handle,
IntPtr.Zero, "Edit", String.Empty)
If hEdit.Equals(IntPtr.Zero) Then
MessageBox.Show(Marshal.GetLastWin32Error())
Else
SendMessage(hEdit, WM_PASTE, 0, 0)
End If
End Sub

It works well on my side. Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


这篇关于改变内置的ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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