VB怎样获得鼠标点击窗体时的坐标!
答案:2 悬赏:70 手机版
解决时间 2021-01-27 23:36
- 提问者网友:你独家记忆
- 2021-01-27 06:12
VB怎样获得鼠标点击窗体时的坐标?在一定的坐标范围内响应某事件要怎么弄?
最佳答案
- 五星知识达人网友:野慌
- 2021-01-27 07:07
用hook绝对没问题
不过窗体本身就有MouseMove
在里面你就可以知道你的鼠标的位置了嘛。
如果是你自己的直接用MouseMove
其他程序的窗体就只能用hook了~!!
不过窗体本身就有MouseMove
在里面你就可以知道你的鼠标的位置了嘛。
如果是你自己的直接用MouseMove
其他程序的窗体就只能用hook了~!!
全部回答
- 1楼网友:雪起风沙痕
- 2021-01-27 08:26
新出炉的底层鼠标钩子(low level mouse hook)
'==================窗体代码=================
private sub form_load()
hhook = setwindowshookex(wh_mouse_ll, addressof mousehookproc, app.hinstance, 0)
if hhook <> 0 then
msgbox "已钩住鼠标事件,请按本窗体右上方的x按钮关闭窗体,不要使用vb调试工具栏的“结束”按钮,以免vb崩溃"
end if
end sub
private sub form_unload(cancel as integer)
unhookwindowshookex hhook
end sub
'=============模块代码==================
public const wh_mouse = 7 '本地钩子
public const wh_mouse_ll = 14 '全局钩子
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 callnexthookex lib "user32" (byval hhook as long, byval ncode as long, byval wparam as long, lparam as any) as long
public declare function unhookwindowshookex lib "user32" (byval hhook as long) as long
public declare sub copymemory lib "kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)
public const wm_lbuttondown = &h201 '窗口中按下鼠标左键
public const wm_lbuttonup = &h202 '窗口中松开鼠标左键
public const wm_mousemove = &h200 '窗口中移动鼠标
public const wm_rbuttondown = &h204 '窗口中按下鼠标右键
public const wm_rbuttonup = &h205 '窗口中松开鼠标右键
public const wm_mousewheel = &h20a '鼠标滚轮
public const wm_nclbuttondown = &ha1 '窗口标题栏中按下鼠标左键
public const wm_nclbuttonup = &ha2 '窗口标题栏中左开鼠标左键
public const wm_ncmousemove = &ha0 '窗口标题栏中移动鼠标
public const wm_ncrbuttondown = &ha4 '窗口标题栏中按下鼠标右键
public const wm_ncrbuttonup = &ha5 '窗口标题栏中松开鼠标右键
public hhook as long
public type pointapi
x as long
y as long
end type
type msllhookstruct
pt as pointapi '相对于屏幕左上角的坐标x,y
mousedata as long '鼠标数据
flags as long '标记
time as long '时间戳
dwextrainfo as long '其他信息
end type
type mousehookstruct
pt as pointapi '相对于屏幕左上角的坐标x,y
hwnd as long '鼠标光标下窗口的句柄
whittestcode as long '鼠标光标在窗口中的位置,标题栏、左边框、右边框,下边框。。。
dwextrainfo as long '其他信息,通常为0
end type
dim omousehookstruct as msllhookstruct
public function mousehookproc(byval idhook as long, byval wparam as long, byval lparam as long) as long
copymemory omousehookstruct, byval lparam, len(omousehookstruct)
debug.print "当前鼠标位置-x:" & omousehookstruct.pt.x & "; y:" & omousehookstruct.pt.y
select case wparam
case wm_lbuttondown, wm_nclbuttondown
debug.print "左键按下"
case wm_lbuttonup, wm_nclbuttonup
debug.print "左键弹起"
case wm_rbuttondown, wm_ncrbuttondown
debug.print "右键按下"
case wm_rbuttonup, wm_ncrbuttonup
debug.print "右键弹起"
case wm_mousemove, wm_ncmousemove
debug.print "鼠标移动"
case wm_mousewheel
debug.print "鼠标滚轮"
end select
mousehookproc = callnexthookex(hhook, idhook, wparam, byval lparam)
end function
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯