java 如何获取活动窗口的大小

2024-11-19 14:37:33
推荐回答(1个)
回答1:

'VB获得活动窗口标题、位置、大小
'运行后,在form上print当前活动窗口信息
Private Declare Function GetActiveWindow Lib "user32" () 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 GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Sub Form_Load()
Me.AutoRedraw = True
Timer1.Interval = 2000
Timer1.Enabled = True
End Sub

Sub Timer1_Timer()
Dim p As RECT
Dim h As Long
Dim str1 As String
h = GetActiveWindow '获得活动窗口句柄
str1 = String(255, 0)
GetWindowText h, str1, 255 '获得标题
GetWindowRect h, p '获得窗口位置、大小
Print "窗口标题:" & Left(str1, InStr(str1, Chr(0)) - 1) & " 窗口位置:Left=" & p.Left & " Top=" & p.Top & " 大小是:" & p.Right - p.Left & "X" & p.Bottom - p.Top
End Sub