VB 用全域性鉤子,記錄滑鼠點選次數

2021-05-26 05:41:38 字數 3499 閱讀 9273

'首先用建立乙個標準exe程式

'把窗體名稱,設定為frmmain

'在窗體上建立一組文字框陣列,名稱為txtmsg

'txtmsg(0) -- 顯示滑鼠左鍵按下的次數

'txtmsg(1) -- 顯示滑鼠中鍵按下的次數

'txtmsg(2) -- 顯示滑鼠右鍵按下的次數

'txtmsg(3) -- 顯示滑鼠按下的總次數

'首先建立乙個module,並輸入以下**

public declare function setwindowshookex lib "user32" alias "setwindowshookexa" (byval idhook as long, byval lpfn as long, byval hmod as long, byval dwthreadid as long) as long

public declare function unhookwindowshookex lib "user32"

(byval hhook as long) as long

public declare function callnexthookex lib "user32"

(byval hhook as long, byval ncode as long, byval wparam as long, lparam as any) as long

public declare sub copymemory lib "kernel32" alias "rtlmovememory" (lpvdest as any, byval lpvsource as long, byval cbcopy as long)

public type mousemsgs

x as long            'x座標

y as long            'y座標

a as long

b as long

time as long         'window執行時間

end type

public const wh_mouse_ll = 14

'-----------------------------------------

'訊息public const hc_action = 0

'滑鼠訊息

public const wm_mousemove = &h200

public const w

m_lbuttondown = &h201

public const wm_lbuttonup = &h202

public const wm_lbuttondblclk = &h203

public const wm_rbuttondown = &h204

public const wm_rbuttonup = &h205

public const wm_rbuttondblclk = &h206

public const wm_mbuttondown = &h207

public const wm_mbuttonup = &h208

public const wm_mbuttondblclk = &h209

public const wm_mouseactivate = &h21

public const wm_mousefirst = &h200

public const wm_mouselast = &h209

public const wm_mousewheel = &h20a

public mousemsg as mousemsgs

public lhook as long '記錄hook的值,以便退出程式的時候銷毀hook

public lclick as long, mclick as long, rclick as long, tclick as long '用來統計滑鼠各個鍵的按下次數

'滑鼠鉤子

public function callmousehookproc(byval code as long, byval wparam as long, byval lparam as long)

as long

if code = hc_action then

copymemory mousemsg, lparam, lenb(mousemsg)

frmmain.caption = "x=" + str(mousemsg.x) + " y=" + str(mousemsg.y)

'在主視窗上顯示滑鼠的當前位置

select case wparam

'根據不同滑鼠動作進行處理,在這裡只處理了滑鼠按下的動作

case wm_lbuttondown

'左鍵按下

lclick = lclick + 1  '進行統計,並顯示在住窗體的文字框上

tclick = tclick + 1

frmmain.txtmsg(0).text = lclick

frmmain.txtmsg(3).text = tclick

callmousehookproc = 0

'這裡把返回值設定為0,保證滑鼠動作正常完成

case wm_mbuttondown '中鍵按下

mclick = mclick + 1

tclick = tclick + 1

frmmain.txtmsg(1).text = mclick

frmmain.txtmsg(3).text = tclick

callmousehookproc = 0

case wm_rbuttondown '右鍵按下

rclick = rclick + 1

tclick = tclick + 1

frmmain.txtmsg(2).text = rclick

frmmain.txtmsg(3).text = tclick

callmousehookproc = 0

end select

end if

if code <> 0 then

callmousehookproc = callnexthookex(0, code, wparam, lparam)

'使用callnexthookex,來保證滑鼠鉤子能夠被其它程式使用

end if

end function

'接下來,我們再給窗體裡面新增**

private sub addhook()

'安裝滑鼠鉤子

end sub

private sub delhook()

'卸除滑鼠鉤子

unhookwindowshookex lhook

end sub

private sub form_load() '窗體載入的時候,安裝滑鼠鉤子

addhook

end sub

private sub form_unload(cancel as integer)'窗體解除安裝的時候,卸除滑鼠鉤子

delhook

end sub

VB 全域性鍵盤 滑鼠鉤子

if code hc action then copymemory mousemsg,lparam,lenb mousemsg form1.txtmsg 1 text x str mousemsg.x y str mousemsg.y form1.txthwnd 1 format wparam,0 ...

VB 模擬滑鼠點選 Mouse Event

private declare submouse event lib user32 byval dwflags aslong byval dx as long byval dy as long byval cbuttons aslong byval dwextrainfo aslong const ...

VB 全域性Hook滑鼠訊息

vb 全域性hook滑鼠鉤子,獲取滑鼠單擊左鍵 滑鼠座標位置等。以下是滑鼠的按鍵訊息 鍵 按下釋放 按下 雙擊 左wm nclbuttondown ha1 wm nclbuttonup ha2 wm nclbuttondblclk ha3 中wm ncmbuttondown ha7 wm ncmbu...