● タイトルバーのサイズを取得する ●

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
'ウインドウの属性の一部を取得する
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
'システムメトリック値を取得する
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

'ウインドウの位置、サイズを取得する
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

'ウインドウが最小化されているかチェックする
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long

Private Const SM_CYCAPTION = 4
Private Const SM_CYBORDER = 6
Private Const SM_CXDLGFRAME = 7
Private Const SM_CYDLGFRAME = 8
Private Const SM_CXFRAME = 32
Private Const SM_CYFRAME = 33
Private Const SM_CYSMCAPTION = 51
Private Const SM_CXSIZEFRAME = SM_CXFRAME
Private Const SM_CYSIZEFRAME = SM_CYFRAME
Private Const SM_CXFIXEDFRAME = SM_CXDLGFRAME
Private Const SM_CYFIXEDFRAME = SM_CYDLGFRAME

Private Const GWL_STYLE = (-16)   'ウインドウスタイル
Private Const GWL_EXSTYLE = (-20) 'ウインドウ拡張スタイル

Private Const WS_THICKFRAME = &H40000
Private Const WS_EX_TOOLWINDOW = &H80

'---------------------------------------------------------------
' 関数名: GetTitleBarRect
' 機  能: ウインドウのタイトルバーのサイズを取得する
' 引  数: (in) hWnd … ウインドウハンドル
'         (in/out) udtRECT … タイトルバーサイズ
' 返り値: なし
'---------------------------------------------------------------
Public Sub GetTitleBarRect(ByVal hWnd As Long, ByRef udtRECT As RECT)
    Dim wStyle As Long    'ウインドウスタイル
    Dim wExStyle As Long  '拡張ウインドウスタイル
    Dim xPos As Long      'ウインドウ枠 X
    Dim yPos As Long      'ウインドウ枠 Y
    Dim tHeight As Long   'タイトルバー高さ

    'ウインドウスタイル取得
    wStyle = GetWindowLong(hWnd, GWL_STYLE)     '通常
    wExStyle = GetWindowLong(hWnd, GWL_EXSTYLE) '拡張

    'サイズ変更ができて、アイコン化されていない
    If (wStyle And WS_THICKFRAME) And (Not IsIconic(hWnd)) Then
        xPos = GetSystemMetrics(SM_CXSIZEFRAME)
        yPos = GetSystemMetrics(SM_CYSIZEFRAME)
    Else
        xPos = GetSystemMetrics(SM_CXFIXEDFRAME)
        yPos = GetSystemMetrics(SM_CYFIXEDFRAME)
    End If

    '標準タイトルバー高さ取得
    If wExStyle And WS_EX_TOOLWINDOW Then
        tHeight = GetSystemMetrics(SM_CYSMCAPTION)  'ツールウインドウ
    Else
        tHeight = GetSystemMetrics(SM_CYCAPTION)    '通常ウインドウ
    End If

    'ウインドウサイズ取得
    Call GetWindowRect(hWnd, udtRECT)

    'タイトルバーのサイズを設定
    With udtRECT
        .Right = .Right - .Left - xPos
        .Left = xPos
        .Top = yPos
        .Bottom = .Top + tHeight - GetSystemMetrics(SM_CYBORDER)
    End With
End Sub

戻る