戻る

フォームのサイズを取得する方法(API関数 GetWindowRect の使用例)
動作環境 Windows95/98 開発環境 Visual Basic 6.0 (+SP3)
説明 API関数 GetWindowRect を使用してフォームのサイズを取得します。
用意するもの:標準フォーム(Form1)、コマンドボタン(Command1)
注 このサンプルでは、Windows付属の"電卓"のウインドウサイズ(フォームのサイズ)を取得します。
  電卓を起動した上で、下記のコードをお試しください。

'変数の明示的な宣言を強制します。
Option Explicit

'API関数FindWindowの使用を宣言します。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

'API関数GetWindowRectの使用を宣言します。
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, _
lpRect As RECT) As Long

'RECT構造体を定義します。
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Sub Command1_Click()

'変数を定義します。
'API関数FindWindowの戻り値
Dim hWnd As Long
'API関数GetWindowRectの戻り値
Dim ret As Long
'RECT構造体
Dim rc As RECT

'電卓のウインドウハンドルを取得します。
hWnd = FindWindow(vbNullString, "電卓")

'電卓のウインドウハンドルからウインドウの矩形情報を取得します。
ret = GetWindowRect(hWnd, rc)

'ウインドウの幅、高さをフォームに表示します。
Print "ウインドウの幅 :" & rc.Bottom - rc.Top
Print "ウインドウの高さ:" & rc.Right - rc.Left

End Sub