vb 获取鼠标在窗口内的屏幕坐标

2024-12-01 00:23:12
推荐回答(4个)
回答1:

看到你的问题补充就不知道你想要写成什么样子的了。
给你两段代码把。

一、获取鼠标在屏幕上的坐标
'坐标显示在Text1,Text2.
'建一个Timer1,Text1,Text2.
'代码如下。
'============
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim p As POINTAPI
Private Sub Form_Load()
Timer1.Interval = 10
End Sub

Private Sub Timer1_Timer()
GetCursorPos p
Text1.Text = p.x
Text2.Text = p.y
End Sub

二、获取鼠标在窗体内的坐标
'坐标显示在标题上
'运行此程序需要再窗体添加控件Timer
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type

Private Sub Form_Load()
Timer1.Interval = 10 '设置获取鼠标坐标间隔
End Sub

Private Sub Timer1_Timer()
Dim P As POINTAPI
GetCursorPos P '获取鼠标在屏幕中的位置
ScreenToClient Me.hwnd, P '转换为本窗体的坐标
Dim t As Boolean
t = P.x >= 0 And P.y >= 0 And P.x < Me.Width / Screen.TwipsPerPixelX And P.y <= Me.Height / Screen.TwipsPerPixelY
If t Then Me.Caption = "x=" & P.x & "y=" & P.y '按像素显示坐标
'If t Then Me.Caption = "x=" & P.x * Screen.TwipsPerPixelX & "y=" & P.y * Screen.TwipsPerPixelY '按缇显示坐标
End Sub

回答2:

'运行此程序需要再窗体添加控件timer
option
explicit
private
declare
function
getcursorpos
lib
"user32"
(lppoint
as
pointapi)
as
long
private
declare
function
screentoclient
lib
"user32"
(byval
hwnd
as
long,
lppoint
as
pointapi)
as
long
private
type
pointapi
x
as
long
y
as
long
end
type
private
sub
form_load()
timer1.interval
=
10
'设置获取鼠标坐标间隔
end
sub
private
sub
timer1_timer()
dim
p
as
pointapi
getcursorpos
p
'获取鼠标在屏幕中的位置
screentoclient
me.hwnd,
p
'转换为本窗体的坐标
dim
t
as
boolean
t
=
p.x
>=
0
and
p.y
>=
0
and
p.x
<
me.width
/
screen.twipsperpixelx
and
p.y
<=
me.height
/
screen.twipsperpixely
if
t
then
me.caption
=
"x="
&
p.x
&
"y="
&
p.y
'按像素显示坐标
'if
t
then
me.caption
=
"x="
&
p.x
*
screen.twipsperpixelx
&
"y="
&
p.y
*
screen.twipsperpixely
'按缇显示坐标
end
sub

回答3:

讲一下很久以前的设计思路
1、首先取得全屏的大小。
2、不可能获取任意窗口,因为不同的系统和软件的限制。
3、找出看得见的窗口的句柄,求出窗口大小。
4、综上所述建立一个限制算法。
5、其它的由你了。

回答4:

不太清楚哦,希望有人帮解答,祝:早日找到答案!