Hook Technology Map Menu Map (such as RealPlayer)

xiaoxiao2021-03-06  56

Let's take a player for a RealPlayer control as an example.

First put a realg2 control on the Form interface. Then set the attribute value of CTLControls to imageWindow, all

This way we can see a RealPlay player interface on the Form interface.

Then we add a contextMenu menu on the Form interface. I will add a few menu items.

(I have the name of ContextMenu in this is ContextMenu2. You are based on your own situation.

You can change the name. But the name under the bottom should also change accordingly)

Now we add a template in the project

Add the following code inside:

Module Module1

Public FRM1 AS New Form1 ()

Declare function getcurrentthreadid lib "kernel32" alias "getcurrentthreadid" () AS integer

Declare function setWindowshookex lib "user32" alias "setwindowshookexa" (Byval LPFN As INTEGER, 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 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)

Public HNEXTHOKPROC 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

HNEXTHOKPROC = SETWINDOWSHOKEX (HookType.Wh_Mouse, Addressof mymouseproc, 0, getcurrentthreadid ())

END FUNCTION

Public function mymouseproc (byval ncode as integer, byval wparam 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 TEN 'judgment mouse right

MyMouseProc = 1P = frm1.pointtoclient (frm1.mouseposition)

FRM1.CONTEXTMENU2.SHOW (FRM1, P) 'menu display

END IF

END FUNCTION

Sub main ()

Application.run (frm1)

End Sub

End module

Then we want to turn it in the properties of the project - starting the object to Moudel1

Figure:

Finally, we add the following code in Form's LOAD and CLOSED events:

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load

Call setook ()

End Sub

Private sub form1_closed (Byval e as system.eventargs) handles mybase.closed

Call unhook ()

End Sub

转载请注明原文地址:https://www.9cbs.com/read-116524.html

New Post(0)