您的位置:程序门 -> vb -> 基础类



紧急,如何扑捉屏幕上的鼠标所在位置的坐标和记录下来,并判断鼠标的左键还是右键动作记录下来


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


紧急,如何扑捉屏幕上的鼠标所在位置的坐标和记录下来,并判断鼠标的左键还是右键动作记录下来[已结贴,结贴人:yh1205]
发表于:2007-08-06 17:08:19 楼主
如何扑捉屏幕上的鼠标所在位置的坐标和记录下来,并判断鼠标的左键还是右键动作记录下来,代码详细点,在线等,能达到要求马上结贴,谢谢

mymail:       cyanhua@sina.com
发表于:2007-08-06 20:48:361楼 得分:0
鼠标是可以移动的,动中的坐标很多的,你要记录在哪里?全都记录下来会把记录文件撑爆的。
如果在某个地方给你显示出来,可否?

“并判断鼠标的左键还是右键动作记录下来”
是在发生了健的动作,就记录吗?记录在哪里?
记录格式如:“18:25:03.224   左键动了;18:25:08.018   右键动了;18:26:08.981   哈哈,这次左右键同时都动了;...”,可否?

主要是说清鼠标移动的坐标值界限是在程序窗体内,还是指整个屏幕?
发表于:2007-08-06 20:52:062楼 得分:0
做一个鼠标钩子即可完成很简单的
发表于:2007-08-06 20:58:583楼 得分:0
chenhui530(陈辉):
钩子现在不会~~  
发表于:2007-08-06 21:15:584楼 得分:50
我前段时间写了个你自己修改下吧

下面是窗体代码
option   explicit
private   declare   function   showwindow   lib   "user32 "   (byval   hwnd   as   long,   byval   ncmdshow   as   long)   as   long
private   declare   function   setparent   lib   "user32 "   (byval   hwndchild   as   long,   byval   hwndnewparent   as   long)   as   long
private   declare   function   findwindow   lib   "user32 "   alias   "findwindowa "   (byval   lpclassname   as   string,   byval   lpwindowname   as   string)   as   long
private   declare   function   movewindow   lib   "user32 "   (byval   hwnd   as   long,   byval   x   as   long,   byval   y   as   long,   byval   nwidth   as   long,   byval   nheight   as   long,   byval   brepaint   as   long)   as   long
private   declare   function   clienttoscreen   lib   "user32 "   (byval   hwnd   as   long,   lppoint   as   pointapi)   as   long
private   declare   function   findwindowex   lib   "user32 "   alias   "findwindowexa "   (byval   hwnd1   as   long,   byval   hwnd2   as   long,   byval   lpsz1   as   string,   byval   lpsz2   as   string)   as   long
private   declare   function   getclientrect   lib   "user32 "   (byval   hwnd   as   long,   lprect   as   rect)   as   long
private   declare   function   messagebox   lib   "user32 "   alias   "messageboxa "   (byval   hwnd   as   long,   byval   lptext   as   string,   byval   lpcaption   as   string,   byval   wtype   as   long)   as   long
private   type   pointapi
        x   as   long
        y   as   long
end   type
private   type   rect
        left   as   long
        top   as   long
        right   as   long
        bottom   as   long
end   type

private   sub   cmdexit_click()
        unload   me
end   sub

private   sub   cmdleave_click()
        dim   lngstart   as   long,   lngstartbutton   as   long,   objpoint   as   pointapi,   objrect   as   rect
        me.hide
        lngstart   =   findwindow( "shell_traywnd ",   vbnullstring)
        setparent   lngcmdhwnd,   lngstart
        lngstartbutton   =   findwindowex(lngstart,   0,   "button ",   vbnullstring)
        'clienttoscreen   lngstartbutton,   objpoint
        getclientrect   lngstartbutton,   objrect
        movewindow   lngcmdhwnd,   0,   0,   objrect.right   -   objrect.left,   objrect.bottom   -   objrect.top,   1
end   sub

public   sub   cmdsend_click()
'         messagebox   me.hwnd,   "我能响应事件!! ",   "哈哈!! ",   vbinformation
'         showwindow   me.hwnd,   5
'         setparent   lngcmdhwnd,   me.hwnd
'         cmdsend.move   1800,   2880,   cmdexit.width,   cmdexit.height
        popupmenu   me.mnuopen
end   sub

private   sub   cmdsend_keydown(keycode   as   integer,   shift   as   integer)
        if   keycode   =   27   then
                setparent   lngcmdhwnd,   me.hwnd
                cmdsend.move   1800,   2880,   cmdexit.width,   cmdexit.height
                me.show
        end   if
end   sub

private   sub   form_load()
        lngcmdhwnd   =   me.cmdsend.hwnd
        hhook   =   setwindowshookex(wh_mouse_dll,   addressof   mouseproc,   app.hinstance,   0)
end   sub

private   sub   form_unload(cancel   as   integer)
        unhookwindowshookex   hhook
end   sub

private   sub   mnucalc_click()
        shell   "calc ",   vbnormalfocus
end   sub

private   sub   mnunote_click()
        shell   "notepad.exe ",   vbnormalfocus
end   sub
发表于:2007-08-06 21:16:285楼 得分:0
下面是模块代码
option   explicit


public   const   wh_mouse   =   7
public   const   wh_mouse_dll   =   14
private   declare   function   callnexthookex   lib   "user32 "   (byval   hhook   as   long,   byval   ncode   as   long,   byval   wparam   as   long,   lparam   as   any)   as   long
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
declare   function   unhookwindowshookex   lib   "user32 "   (byval   hhook   as   long)   as   long
private   declare   function   getcursorpos   lib   "user32 "   (lppoint   as   pointapi)   as   long
private   declare   function   windowfrompoint   lib   "user32 "   (byval   xpoint   as   long,   byval   ypoint   as   long)   as   long
private   declare   function   getclassname   lib   "user32 "   alias   "getclassnamea "   (byval   hwnd   as   long,   byval   lpclassname   as   string,   byval   nmaxcount   as   long)   as   long
private   declare   function   iswindowenabled   lib   "user32 "   (byval   hwnd   as   long)   as   long
private   declare   function   showwindow   lib   "user32 "   (byval   hwnd   as   long,   byval   ncmdshow   as   long)   as   long
public   declare   function   enablewindow   lib   "user32 "   (byval   hwnd   as   long,   byval   fenable   as   long)   as   long
private   declare   function   enumchildwindows   lib   "user32 "   (byval   hwndparent   as   long,   byval   lpenumfunc   as   long,   byval   lparam   as   long)   as   long
private   declare   function   getparent   lib   "user32 "   (byval   hwnd   as   long)   as   long
private   declare   function   getwindowtext   lib   "user32 "   alias   "getwindowtexta "   (byval   hwnd   as   long,   byval   lpstring   as   string,   byval   cch   as   long)   as   long
private   declare   function   getwindowtextlength   lib   "user32 "   alias   "getwindowtextlengtha "   (byval   hwnd   as   long)   as   long
private   declare   function   sendmessage   lib   "user32 "   alias   "sendmessagea "   (byval   hwnd   as   long,   byval   wmsg   as   long,   byval   wparam   as   long,   lparam   as   any)   as   long
private   const   lb_findstring   =   &h18f

private   type   pointapi
                x   as   long
                y   as   long
end   type
private   type   mousehookstruct
                pt   as   pointapi
                hwnd   as   long
                whittestcode   as   long
                dwextrainfo   as   long
end   type
private   declare   sub   copymemory   lib   "kernel32 "   alias   "rtlmovememory "   (destination   as   any,   source   as   any,   byval   length   as   long)
public   hhook   as   long
private   objmousemsg   as   mousehookstruct
public   lngcmdhwnd   as   long

public   function   mouseproc(byval   idhook   as   long,   byval   wparam   as   long,   byval   lparam   as   long)   as   long
        dim   p   as   pointapi,   strclassname   as   string   *   260,   lnghwnd   as   long,   lngret   as   long
        if   idhook   <   0   then
                mouseproc   =   callnexthookex(hhook,   idhook,   wparam,   byval   lparam)
        else
                copymemory   objmousemsg,   byval   lparam,   lenb(objmousemsg)
                lnghwnd   =   windowfrompoint(objmousemsg.pt.x,   objmousemsg.pt.y)
               
                lngret   =   getclassname(lnghwnd,   strclassname,   260)
                if   (left(strclassname,   lngret)   =   "thundercommandbutton "   or   left(strclassname,   lngret)   =   "thunderrt6commandbutton ")   then
                        if   wparam   =   514   and   lnghwnd   =   lngcmdhwnd   then
                                frmmain.cmdsend_click
                        end   if
                end   if
                mouseproc   =   callnexthookex(hhook,   idhook,   wparam,   byval   lparam)
        end   if
end   function
发表于:2007-08-06 21:17:306楼 得分:0
其中pointapi这个结构是存放鼠标位置的
发表于:2007-08-06 22:01:527楼 得分:0
to   mmyyxx88()  

1.只要能在所有控件都能得到坐标就可以,也可能说就是全屏幕吧,移动时得到的坐标x,y在标签控件里显示一下就可以了,不用记录下来,我心里有素就行,但一定要这个功能

2.关于鼠标左键右键事件,最好能像picture.mousedown鼠标击了有个响应,我可以在里面写下面的类似代码
msgbox   "你在屏幕的 "   &   x   &   ", "   &   y   &   "按了 "   &   "鼠标左键 "(或右键)

3.我的程序是最大化状态下运行的,上面有图片框,也有标签,也有文本框,都希望在鼠标移动到控件上都有一个相对屏幕像素的一个坐。。。,我讲得挺罗索的,说到就是鼠标在全屏幕状态下移动的坐标

4.非常感谢你的回复
发表于:2007-08-06 22:03:308楼 得分:0
to   mmyyxx88()  

你说的就是我想要的,就是全屏幕啦,期待你的代码救我。我都不行了:(
发表于:2007-08-06 22:04:359楼 得分:0
to   chenhui530(陈辉)

谢谢你的回复,对api不是太熟悉,我仔仔研究一下
发表于:2007-08-06 22:08:3310楼 得分:0
上面代码很简单的
发表于:2007-08-06 22:22:1311楼 得分:0
如果你不懂api那么有个变通的方法
用一个api加timer也可以实现,把timer时间设置短些
然后用private   declare   function   getcursorpos   lib   "user32 "   alias   "getcursorpos "   (lppoint   as   pointapi)   as   long
获取坐标信息
发表于:2007-08-06 23:01:2512楼 得分:0
看看这个!
http://www.hexi5.com/bbs/dispbbs.asp?boardid=6&id=35&page=1
发表于:2007-08-06 23:03:3813楼 得分:0
现在太晚了,肯定会有你习惯的代码出现的,这里都是热心人。
发表于:2007-08-06 23:04:4014楼 得分:0
chenhui530(陈辉)=   good!
发表于:2007-08-07 10:05:2115楼 得分:0
学习。
发表于:2007-08-07 10:35:5316楼 得分:0
to   陈辉

谢谢,怎么一调试就退出程序
发表于:2007-08-07 10:36:2817楼 得分:0
to   mmyyxx88()  

你好,能帮我实现吗?
发表于:2007-08-07 10:46:3918楼 得分:50
private   type   pointapi
        x   as   long
        y   as   long
end   type
private   declare   function   getcursorpos   lib   "user32 "   (lppoint   as   pointapi)   as   long
dim   mypos   as   pointapi
private   sub   timer1_timer()
        getcursorpos   mypos                             '获取光标位置
        text1   =   mypos.x
        text2   =   mypos.y
end   sub
private   sub   picture1_mousedown(button   as   integer,   shift   as   integer,   x   as   single,   y   as   single)
        select   case   button
                case   vbleftbutton
                        debug.print   "你在屏幕 "   &   mypos.x   &   ", "   &   mypos.y   &   "   按鼠标左键 "
                case   vbrightbutton
                        debug.print   "你在屏幕 "   &   mypos.x   &   ", "   &   mypos.y   &   "按鼠标右键 "
        end   select
end   sub
发表于:2007-08-07 10:57:5719楼 得分:0
其实chenhui530(陈辉)的程序已经包含了你要求解决的问题。
api还是不能缺,上述代码经过运行。

控件timer1.interval   =   100(ms),作为定时读取位置坐标,你可以调整合适的时间。

左右键的问题,既然你的是全屏,我猜想你关心的应该是你窗体内的位置,窗体内的控件基本上都有mousedown事件,你可以每个控件都写一遍mousedown事件的处理代码。其实你最关心的应该是指定区域内的mousedown事件,比如一幅画面。这里用一个picture1给你举例。

对左右键问题有不满意,或者还有其他要求再提出来,但要更详细表述清楚。
发表于:2007-08-07 13:57:5120楼 得分:0
好的,谢谢各位,问题已解决


快速检索

最新资讯
热门点击