'模块的代码:
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
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 MyMesg(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
MyMesg = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
'如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理
Exit Function
End If
MyMesg = True
End Function
窗体的代码:
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Exit Sub
OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC)
'取得窗口函数的地址
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf MyMesg)
'用MyMesg代替窗口函数处理消息
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, OldWindowProc)
'恢复窗口的默认函数
PopupMenu mymenu
'弹出自定义菜单
End Sub
第二中方法:对了还有更简单的一个方法
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Enabled = False
Text1.Enabled = True
PopupMenu mymenu
End Sub
搞定简单吧!(^_^)
尽量不要使用 AddressOf 来改变一个窗口的默认窗口函数,VB不擅长做这类的工作。