■説明
ウィンドウ全体の座標を取得する関数
■宣言
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
第一引数:ウィンドウのハンドルを指定
第二引数:ウィンドウのtop/right/bottom/leftの値を格納するための構造体を指定します
例)
■サンプルコード
では、サンプルとして、デスクトップのサイズを大きさを出してみます。
デスクトップのハンドルを取得するには、 GetDesktopWindow関数を使用すれば取得できます。
→GetDesktopWindow関数について
ただし、ウィンドウのハンドルがわかっただけでは、うれしくもないので
他の関数に渡して活用してみます。
左上(x, y) = (0, 0)
右下(x, y) = (1920, 1080)
Width :1920
Heihgt:1080
今度は、電卓を起動して、電卓のウィンドウ座標を取得してみます。
左上(x, y) = (66, 87)
右下(x, y) = (368, 332)
Width :302
Heihgt:245
今度は、関数電卓モードに切り替えて実行してみます。
左上(x, y) = (66, 87)
右下(x, y) = (625, 384)
Width :559
Heihgt:297
ウィンドウ全体の座標を取得する関数
■宣言
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
第一引数:ウィンドウのハンドルを指定
第二引数:ウィンドウのtop/right/bottom/leftの値を格納するための構造体を指定します
例)
Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type戻り値:失敗した時0を返します
■サンプルコード
では、サンプルとして、デスクトップのサイズを大きさを出してみます。
デスクトップのハンドルを取得するには、 GetDesktopWindow関数を使用すれば取得できます。
→GetDesktopWindow関数について
Option Explicit
Declare Function GetDesktopWindow Lib "user32" () As Long
Sub SampleCode1()
Dim hwnd As Long
hwnd = GetDesktopWindow()
Debug.Print hwnd
End Sub
説明するまでもない簡単なコードです。ただし、ウィンドウのハンドルがわかっただけでは、うれしくもないので
他の関数に渡して活用してみます。
Option Explicit
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Sub SampleCode()
Dim hwnd As Long
hwnd = GetDesktopWindow()
Dim Re As RECT
Dim Result As Long
Result = GetWindowRect(hwnd, Re)
Debug.Print "左上(x, y) = (" & Re.Left & ", " & Re.Top & ")"
Debug.Print "右下(x, y) = (" & Re.Right & ", " & Re.Bottom & ")"
Debug.Print "Width :" & (Re.Right - Re.Left)
Debug.Print "Heihgt:" & (Re.Bottom - Re.Top)
End Sub
実行結果例:左上(x, y) = (0, 0)
右下(x, y) = (1920, 1080)
Width :1920
Heihgt:1080
今度は、電卓を起動して、電卓のウィンドウ座標を取得してみます。
Option Explicit
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Sub SampleCode()
Dim hwnd As Long
hwnd = FindWindow("SciCalc", "電卓")
Dim Re As RECT
Dim Result As Long
Result = GetWindowRect(hwnd, Re)
Debug.Print "左上(x, y) = (" & Re.Left & ", " & Re.Top & ")"
Debug.Print "右下(x, y) = (" & Re.Right & ", " & Re.Bottom & ")"
Debug.Print "Width :" & (Re.Right - Re.Left)
Debug.Print "Heihgt:" & (Re.Bottom - Re.Top)
End Sub
実行結果例:左上(x, y) = (66, 87)
右下(x, y) = (368, 332)
Width :302
Heihgt:245
今度は、関数電卓モードに切り替えて実行してみます。
左上(x, y) = (66, 87)
右下(x, y) = (625, 384)
Width :559
Heihgt:297
0 件のコメント:
コメントを投稿