我有个不完善的办法(Set Text1 = TextBox): 在Textbox的MouseDown事件中判断鼠标按键是否为右键: If Button=vbRightButton Then Text1.Enabled=False End If 在MouseUp事件中: Text1.Enabled=True
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then Me.PopupMenu menu End If End Sub ---------------------------------------------------------------
Option Explicit
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyControl Then
If Len(Clipboard.GetText) > 0 Then
Clipboard.Clear
End If
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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
Public Function SubClass1_WndMessage(ByVal hWnd As OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wp As OLE_HANDLE, ByVal lp As Long) As Long
在Textbox的MouseDown事件中判断鼠标按键是否为右键:
If Button=vbRightButton Then
Text1.Enabled=False
End If
在MouseUp事件中:
Text1.Enabled=True
这样就可以了.即鼠标右键按下时临时禁用TextBox.
如果必须按题设中的要求,那可能要用HOOK拦截Menu的Initialize事件了,很麻烦,不过是治本的办法.别问我,我不会.
---------------------------------------------------------------
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
Me.PopupMenu menu
End If
End Sub
---------------------------------------------------------------
Option Explicit
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyControl Then
If Len(Clipboard.GetText) > 0 Then
Clipboard.Clear
End If
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC)
' 取得窗口函数的地址
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)
' 用SubClass1_WndMessage代替窗口函数处理消息
End If
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc)
' 恢复窗口的默认函数
' 弹出自定义菜单
End If
End Sub
'Module1:
Option Explicit
Public OldWindowProc As Long
' 保存默认的窗口函数的地址
Public Const WM_CONTEXTMENU = &H7B
' 当右击文本框时,产生这条消息
Public Const GWL_WNDPROC = (-4)
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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
Public Function SubClass1_WndMessage(ByVal hWnd As OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wp As OLE_HANDLE, ByVal lp As Long) As Long
If Msg = WM_CONTEXTMENU Then
SubClass1_WndMessage = True
Else
SubClass1_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理
End If
End Function