如何获取光标所在的位置的句柄。 (200分) ( 积分: 200 )

  • 主题发起人 主题发起人 lsjkiki
  • 开始时间 开始时间
L

lsjkiki

Unregistered / Unconfirmed
GUEST, unregistred user!
光标所在”的位置的控件句柄。<br>就是光标闪烁的地方的句柄,一般是edit,如何得到句柄,或者类名,<br>不是鼠标所在的地方的句柄。<br>用这个函数的GetCursorPos(pt)&nbsp;别来;<br>望高手指点。。。[:)]
 
你是想知道光标停留的是哪个控件是么?
 
就是写一个程序&nbsp;来判断当前键盘光标&nbsp;所在的窗口和&nbsp;控件句柄&nbsp;类名。
 
[:(]在顶一下!
 
{<br>&nbsp;&nbsp;当光标被另外一个线程掌握时,就无法用GetCursor()应用接口获得光标句柄。<br>&nbsp;&nbsp;本文阐述如何在任何线程掌握光标时都能够获取光标句柄。<br>&nbsp;&nbsp;例如,想在屏幕截取程序中放置光标时该如何做。<br>}<br><br>function&nbsp;GetCursorHandle:&nbsp;HCURSOR;<br>var<br>&nbsp;&nbsp;hWindow:&nbsp;HWND;<br>&nbsp;&nbsp;pt:&nbsp;TPoint;<br>&nbsp;&nbsp;pIconInfo:&nbsp;TIconInfo;<br>&nbsp;&nbsp;dwThreadID,&nbsp;dwCurrentThreadID:&nbsp;DWORD;<br>begin<br>&nbsp;&nbsp;//&nbsp;检查哪个窗体掌握光标<br>&nbsp;&nbsp;GetCursorPos(pt);<br>&nbsp;&nbsp;hWindow&nbsp;:=&nbsp;WindowFromPoint(pt);<br><br>&nbsp;&nbsp;//&nbsp;获得光标所有者的线程ID<br>&nbsp;&nbsp;dwThreadID&nbsp;:=&nbsp;GetWindowThreadProcessId(hWindow,&nbsp;nil);<br><br>&nbsp;&nbsp;//&nbsp;获得当前线程的ID<br>&nbsp;&nbsp;dwCurrentThreadID&nbsp;:=&nbsp;GetCurrentThreadId;<br><br>&nbsp;&nbsp;//&nbsp;如果光标所有者的线程不是当前线程,就要把光标所有者的线程配属到当前线程。<br>&nbsp;&nbsp;//然后调用GetCursor()来获得正确的光标句柄(hCursor)。<br><br>&nbsp;&nbsp;if&nbsp;(dwCurrentThreadID&nbsp;&lt;&gt;&nbsp;dwThreadID)&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;AttachThreadInput(dwCurrentThreadID,&nbsp;dwThreadID,&nbsp;True)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;获得光标句柄<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;GetCursor;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AttachThreadInput(dwCurrentThreadID,&nbsp;dwThreadID,&nbsp;False);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end&nbsp;else<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;GetCursor;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;CurPosX,&nbsp;CurPoxY:&nbsp;Integer;<br>&nbsp;&nbsp;MyCursor:&nbsp;TIcon;<br>&nbsp;&nbsp;pIconInfo:&nbsp;TIconInfo;<br>begin<br>&nbsp;&nbsp;MyCursor&nbsp;:=&nbsp;TIcon.Create;<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;MyCursor.Handle&nbsp;:=&nbsp;GetCursorHandle;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;获得光标位置<br>&nbsp;&nbsp;&nbsp;&nbsp;GetIconInfo(MyCursor.Handle,&nbsp;pIconInfo);<br>&nbsp;&nbsp;&nbsp;&nbsp;CurPosX&nbsp;:=&nbsp;pIconInfo.xHotspot;<br>&nbsp;&nbsp;&nbsp;&nbsp;CurPoxY&nbsp;:=&nbsp;pIconInfo.yHotspot;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;在窗体上画出光标<br>&nbsp;&nbsp;&nbsp;&nbsp;Canvas.Draw(CurPoxY,&nbsp;CurPoxY,&nbsp;MyCursor);<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;MyCursor.ReleaseHandle;<br>&nbsp;&nbsp;&nbsp;&nbsp;MyCursor.Free;<br>&nbsp;&nbsp;end;<br>end;<br><br>//&nbsp;另外一种解决办法:<br><br>procedure&nbsp;TForm1.Timer1Timer(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;CI:&nbsp;TCursorInfo;<br>begin<br>&nbsp;&nbsp;CI.cbSize&nbsp;:=&nbsp;SizeOf(CI);<br>&nbsp;&nbsp;GetCursorInfo(CI);<br>&nbsp;&nbsp;Image1.Picture.Icon.Handle&nbsp;:=&nbsp;CI.hCursor;<br>end;
 
请认真看题目,用GetCursorPos(pt);<br>就是错的,我要的是键盘闪烁的光标的位置,和鼠标的光标没有任何关系。。<br>[:(]<br>在这样就要扣分了。。。哈哈~~!!!!!!!!!!<br>再顶,希望有高手出来解决!
 
GetCursorPos(pt)可以获取光标的位置!
 
帮顶,接分
 
请认真看题目,再顶上。
 
楼主的意思应该是取得被激活控件的句柄[:D]<br><br>可以这样用&nbsp;findwindowEX&nbsp;找到窗体句柄,然后遍历窗体上所有的控件,用&nbsp;IsWindowEnabled&nbsp;来判断该控件是否被激活就OK了,拿分来~[:D]<br><br>或者用消息来获取&nbsp;E&nbsp;N&nbsp;_&nbsp;S&nbsp;E&nbsp;T&nbsp;F&nbsp;O&nbsp;C&nbsp;U&nbsp;S编辑框获得输入焦点。
 
GetFocus();<br>The&nbsp;GetFocus&nbsp;function&nbsp;retrieves&nbsp;the&nbsp;handle&nbsp;of&nbsp;the&nbsp;window&nbsp;that&nbsp;has&nbsp;the&nbsp;keyboard&nbsp;focus,&nbsp;if&nbsp;the&nbsp;window&nbsp;is&nbsp;associated&nbsp;with&nbsp;the&nbsp;calling&nbsp;thread's&nbsp;message&nbsp;queue.&nbsp;<br><br>HWND&nbsp;GetFocus(VOID)<br>&nbsp;<br><br>Parameters<br><br>This&nbsp;function&nbsp;has&nbsp;no&nbsp;parameters.&nbsp;<br><br>Return&nbsp;Values<br><br>If&nbsp;the&nbsp;function&nbsp;succeeds,&nbsp;the&nbsp;return&nbsp;value&nbsp;is&nbsp;the&nbsp;handle&nbsp;of&nbsp;the&nbsp;window&nbsp;with&nbsp;the&nbsp;keyboard&nbsp;focus.&nbsp;If&nbsp;the&nbsp;calling&nbsp;thread's&nbsp;message&nbsp;queue&nbsp;does&nbsp;not&nbsp;have&nbsp;an&nbsp;associated&nbsp;window&nbsp;with&nbsp;the&nbsp;keyboard&nbsp;focus,&nbsp;the&nbsp;return&nbsp;value&nbsp;is&nbsp;NULL.&nbsp;<br><br>Remarks<br><br>Even&nbsp;if&nbsp;GetFocus&nbsp;returns&nbsp;NULL,&nbsp;another&nbsp;thread's&nbsp;queue&nbsp;may&nbsp;be&nbsp;associated&nbsp;with&nbsp;a&nbsp;window&nbsp;that&nbsp;has&nbsp;the&nbsp;keyboard&nbsp;focus.&nbsp;<br>Use&nbsp;the&nbsp;GetForegroundWindow&nbsp;function&nbsp;to&nbsp;retrieve&nbsp;the&nbsp;handle&nbsp;to&nbsp;the&nbsp;window&nbsp;with&nbsp;which&nbsp;the&nbsp;user&nbsp;is&nbsp;currently&nbsp;working.&nbsp;You&nbsp;can&nbsp;associate&nbsp;your&nbsp;thread's&nbsp;message&nbsp;queue&nbsp;with&nbsp;the&nbsp;windows&nbsp;owned&nbsp;by&nbsp;another&nbsp;thread&nbsp;by&nbsp;using&nbsp;the&nbsp;AttachThreadInput&nbsp;function.&nbsp;<br><br>下面这几个API应该能实现,不过没试过<br>GetForegroundWindow<br>GetWindowThreadProcessId<br>GetCurrentThreadId<br>AttachThreadInput<br>GetFocus
 
windowfrompoint()
 
先&nbsp;GetCursorPos&nbsp;得到光标的位置<br>再&nbsp;WindowFromPoint&nbsp;得到光标下窗口的句柄<br><br>------------------------<br>www.waibaoinfo.com
 
后退
顶部