VB怎么获取当前活动窗口的坐标?
发布网友
发布时间:2023-09-24 03:06
我来回答
共1个回答
热心网友
时间:2024-10-20 22:27
'在窗体上画一个Timer控件,设置其Interval属性为100
'把代码复制到Form1的代码窗口中
'获得活动窗口句柄的API
Private
Declare
Function
GetForegroundWindow
Lib
"user32"
()
As
Long
'获得窗口区域结构的API
Private
Declare
Function
GetWindowRect
Lib
"user32"
(ByVal
hwnd
As
Long,
wndRect
As
RECT)
As
Long
Private
Type
RECT
'自定义区域结构类型
x
As
Long
y
As
Long
Right
As
Long
Bottom
As
Long
End
Type
Dim
ActiveWindowRECT
As
RECT
'用於保存活动窗口区域的结构
Dim
ActiveWindowHwnd
As
Long
'活动窗口的句柄
Private
Sub
Timer1_Timer()
ActiveWindowHwnd
=
GetForegroundWindow
'获得活动窗口的句柄
GetWindowRect
ActiveWindowHwnd,
ActiveWindowRECT
'获得活动窗口的区域并保存在
ActiveWindowRECT
变量中
'在窗口上输出信息
Me.Cls
Print
"活动窗口的坐标是("
&
ActiveWindowRECT.x
&
","
&
ActiveWindowRECT.y
&
")"
End
Sub