.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Hook技术屏蔽鼠标右键的菜单(如realplayer的)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

我们以做一个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




相关文章

相关软件