怎样实时显示鼠标位置?在线等待!! ( 积分: 50 )

  • 主题发起人 主题发起人 漠风
  • 开始时间 开始时间

漠风

Unregistered / Unconfirmed
GUEST, unregistred user!
用GetCursorPos(p)能获得屏幕鼠标位置,在FORM的MOUSEMOVE事件中加入如下代码:<br>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,<br> &nbsp;Y: Integer);<br>var p: TPoint;<br>begin<br> &nbsp;GetCursorPos(p);<br> &nbsp;Label1.Caption := inttostr(p.X);<br> &nbsp;Label2.Caption := inttostr(p.Y);<br>end;<br>但是问题来了,只要鼠标一移出form1,两人label就不显示了,如何解决这个问题?<br>我不想用timer控件。
 
用GetCursorPos(p)能获得屏幕鼠标位置,在FORM的MOUSEMOVE事件中加入如下代码:<br>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,<br> &nbsp;Y: Integer);<br>var p: TPoint;<br>begin<br> &nbsp;GetCursorPos(p);<br> &nbsp;Label1.Caption := inttostr(p.X);<br> &nbsp;Label2.Caption := inttostr(p.Y);<br>end;<br>但是问题来了,只要鼠标一移出form1,两人label就不显示了,如何解决这个问题?<br>我不想用timer控件。
 
timer1.Enabled:=false;<br> &nbsp;GetCursorPos(p);<br> &nbsp;if (p.X&gt;=CursorOper.Left) and (p.X&lt;=CursorOper.Left+CursorOper.Width)<br> &nbsp; &nbsp;and (p.Y&gt;=CursorOper.Top) and (p.Y&lt;=CursorOper.Top+CursorOper.Height) then<br> &nbsp;begin<br> &nbsp; &nbsp;CursorStatus.Panels[0].Text:='在本窗体内';<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;CursorStatus.Panels[0].Text:='在本窗体外';<br> &nbsp; &nbsp;hand:=WindowFromPoint(p);<br><br> &nbsp; &nbsp;if anEnabled.Checked then EnumChildWindows(hand, @WindowEnabled, 0);<br> &nbsp; &nbsp;if Not anEnabled.Checked then EnumChildWindows(hand, @WindowDisEnabled, 0);<br> &nbsp; &nbsp;if anShow.Checked then EnumChildWindows(hand, @WindowShow, 0);<br> &nbsp; &nbsp;if Not anShow.Checked then EnumChildWindows(hand, @WindowHide, 0);<br><br> &nbsp; &nbsp;getwindowtext(hand,@str,length(str));<br> &nbsp; &nbsp;getclassname(hand,@strclass,length(str));<br> &nbsp; &nbsp;if CheckHand=hand then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if i&gt;=15 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;AddANXinXi(inttostr(hand),str,strclass);<br> &nbsp; &nbsp; &nbsp; &nbsp;i:=0;<br> &nbsp; &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;i:=i+1;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;JuBing.Text:= inttostr(hand);<br> &nbsp; &nbsp;NeiRong.Text:=str;<br> &nbsp; &nbsp;LeiMing.Text:=strclass;<br> &nbsp; &nbsp;ShuBiao_X.Caption:=IntToStr(p.X);<br> &nbsp; &nbsp;ShuBiao_Y.Caption:=IntToStr(p.Y);<br> &nbsp; &nbsp;CheckHand:=hand;<br> &nbsp;end;<br> &nbsp;timer1.Enabled:=true;
 
因为你只有在窗体上移动的时候才触发事件
 
鼠标 hook
 
用timer组件简单多了!
 
setcapture(handle)<br>这样即使鼠标移出form 范围也能收到消息. 但是鼠标在的窗口上点过后就不行了
 
to sbzldlb:<br><br> &nbsp; &nbsp;你给的程序怎么调试不通呀?帮帮我!有好多变量未找到!
 
cjianwen的建议很好的呀。简单实用型。
 
你创建一个线程不就得了
 
干嘛放着简单的方法不去用
 
用timer实现就可以了,将timer的interval设置小一点:<br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> &nbsp;p:tpoint;<br>begin<br> &nbsp;getcursorpos(p);<br> &nbsp;caption := format('%d %d',[p.x, p.y]);<br>end;
 
用钩子啊;<br>var<br> &nbsp;HOOKHANDLE:THandle;<br> &nbsp;xy:TPoint;<br>function GETMOUSEHOOK(code:LongInt;Wparam:WPARAM;Lparam:LPARAM):LRESULT;stdcall;<br>begin<br> &nbsp;if code &lt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CallNextHookEx(HOOKHANDLE,code,Wparam,Lparam);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;if Peventmsg(Lparam).message=WM_MOUSEMOVE then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(xy);<br> &nbsp; &nbsp; &nbsp; &nbsp;with Form1 do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;edt1.Text:=IntToStr(xy.X);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;edt2.Text:=IntToStr(xy.Y);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;CallNextHookEx(HOOKHANDLE,code,Wparam,Lparam);<br> &nbsp; &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;SetWindowsHookEx(HOOKHANDLE,GETMOUSEHOOK,HInstance,0);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;UnhookWindowsHookEx(HOOKHANDLE);<br>end;
 
多人接受答案了。
 
后退
顶部