楼上的会先弹出系统菜单再弹出自己的菜单,所以要先屏蔽系统菜单,首先要拦截右键信息,新建一个模块
Option Explicit
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_CONTEXTMENU = &H7B '-------------右键菜单
Private prevWndProc As Long
Private Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_CONTEXTMENU
'这里处理自定义的事件,最好为空
Case Else
'回调系统函数处理
WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam)
End Select
End Function
Public Sub DisableAbility(TargetTextBox As TextBox)
prevWndProc = GetWindowLong(TargetTextBox.hwnd, GWL_WNDPROC)
SetWindowLong TargetTextBox.hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub
然后在你的form窗体里写入
Private Sub Form_Load()
DisableAbility Text1'屏蔽系统右键
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then PopupMenu rrr'假设自己的菜单是rrr,弹出自己的菜单
End Sub