当前位置:编程学习 > C#/ASP.NET >>

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

上一个:SharpDevelop初试手记
下一个:Internet Explorer 编程简述(五)调用IE隐藏的命令(中文版)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,