Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then Text1.Enabled = False Text1.Enabled = True PopupMenu PopupMenuName End If End Sub
http://www.mf100.com/document/2005-8/699.shtml
在上面给出的例子中的 Private Sub Text1_MouseUp(Button As Integer, Shift _ As Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWinProc) 'PopupMenu a 不要自己右键菜单时,就注释这行 End Sub 其他的那个例子里面有详细解释,不再赘述!
'Form1 Code Private Sub Text1_MouseDown(Button As Integer, Shift As _ Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub OldWinProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) ' 取得窗口函数的地址 SetWindowLong Text1.hWnd, GWL_WNDPROC, AddressOf WndProc ' 用SubClass_WndMessage代替窗口函数处理消息 End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift _ As Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWinProc) ' 恢复窗口的默认函数 'PopupMenu a ' 弹出自定义菜单 End Sub'Module1 Code Public Const GWL_WNDPROC = (-4) Public OldWinProc As Long ' 保存系统窗口函数的地址 Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,系统发送这条消息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 Public 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 WndProc(ByVal hWnd As _ OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wParam As OLE_HANDLE, _ ByVal lParam As Long) As Long If Msg <> WM_CONTEXTMENU Then SubClass_WndMessage = CallWindowProc(OldWinProc, _ hWnd, Msg, wParam, lParam) ' 如果消息不是WM_CONTEXTMENU,就调用系统的窗口处理函数 Exit Function End If SubClass_WndMessage = True End Function
If Button = vbRightButton Then
Text1.Enabled = False
Text1.Enabled = True
PopupMenu PopupMenuName
End If
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
If Button = 1 Then Exit Sub
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWinProc)
'PopupMenu a 不要自己右键菜单时,就注释这行
End Sub
其他的那个例子里面有详细解释,不再赘述!
Private Sub Text1_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub
OldWinProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC)
' 取得窗口函数的地址
SetWindowLong Text1.hWnd, GWL_WNDPROC, AddressOf WndProc
' 用SubClass_WndMessage代替窗口函数处理消息
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
If Button = 1 Then Exit Sub
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWinProc)
' 恢复窗口的默认函数
'PopupMenu a
' 弹出自定义菜单
End Sub'Module1 Code
Public Const GWL_WNDPROC = (-4)
Public OldWinProc As Long ' 保存系统窗口函数的地址
Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,系统发送这条消息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
Public 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 WndProc(ByVal hWnd As _
OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wParam As OLE_HANDLE, _
ByVal lParam As Long) As Long
If Msg <> WM_CONTEXTMENU Then
SubClass_WndMessage = CallWindowProc(OldWinProc, _
hWnd, Msg, wParam, lParam)
' 如果消息不是WM_CONTEXTMENU,就调用系统的窗口处理函数
Exit Function
End If
SubClass_WndMessage = True
End Function