Hook技術屏蔽鼠標右鍵的菜單(如realplayer的)
我們以做一個realplayer控件的播放器爲例子
首先在form界面放一個realG2控件。然後設定CtlControls的屬性值爲imagewindow,all
這樣我們就可以在form界面上看到一個realplay的播放器界面了。
然後我們再在form界面上加一個ContextMenu的菜單。自己隨便加幾個菜單項。
(我這裏面的contextMenu的名字爲contextMenu2。你們根據自己的情況
可以改變名字。但底下的名字也要相應改變)
現在我們在項目中添加一個模板
在裏面加入以下代碼:
Module Module1
Public frm1 As New form1()
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Enum HookType
WH_MOUSE = 7
End Enum
Public Sub UnHook()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook()
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_MOUSE, AddressOf MyMouseProc, 0, GetCurrentThreadId())
End Function
Public Function MyMouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyMouseProc = 0
Dim p As New Point()
If nCode < 0 Then
MyMouseProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = 516 Then '判斷鼠標右擊
MyMouseProc = 1
p = frm1.PointToClient(frm1.MousePosition)
frm1.ContextMenu2.Show(frm1, p)‘菜單顯示
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
然後我們要在項目的屬性中把-啓動對象改爲Moudel1
如圖:
最後我們在form的load和closed事件中加入如下代碼:
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call SetHook()
End Sub
Private Sub form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Call UnHook()
End Sub
首先在form界面放一個realG2控件。然後設定CtlControls的屬性值爲imagewindow,all
這樣我們就可以在form界面上看到一個realplay的播放器界面了。
然後我們再在form界面上加一個ContextMenu的菜單。自己隨便加幾個菜單項。
(我這裏面的contextMenu的名字爲contextMenu2。你們根據自己的情況
可以改變名字。但底下的名字也要相應改變)
現在我們在項目中添加一個模板
在裏面加入以下代碼:
Module Module1
Public frm1 As New form1()
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Enum HookType
WH_MOUSE = 7
End Enum
Public Sub UnHook()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook()
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_MOUSE, AddressOf MyMouseProc, 0, GetCurrentThreadId())
End Function
Public Function MyMouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyMouseProc = 0
Dim p As New Point()
If nCode < 0 Then
MyMouseProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = 516 Then '判斷鼠標右擊
MyMouseProc = 1
p = frm1.PointToClient(frm1.MousePosition)
frm1.ContextMenu2.Show(frm1, p)‘菜單顯示
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
然後我們要在項目的屬性中把-啓動對象改爲Moudel1
如圖:
最後我們在form的load和closed事件中加入如下代碼:
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call SetHook()
End Sub
Private Sub form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Call UnHook()
End Sub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.