● デスクトップの領域サイズを取得する ●

タスクバーのサイズも考慮されるよう実装してみた。意外と有用かもしれないが、使う機会は少なそう。

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long

Private Const SPI_GETWORKAREA = 48

'---------------------------------------------------------------------------
' 関数名: GetDesktopRect
' 機能  : デスクトップ領域サイズを取得する
' 引数  : (in) DesktopRect … RECT構造体
' 返り値: 正常:0以外、 エラー:0
'---------------------------------------------------------------------------
Public Function GetDesktopRect(ByRef DesktopRect As RECT) As Long

    GetDesktopRect = SystemParametersInfo(SPI_GETWORKAREA, 0, DesktopRect, 0)

End Function

これで終わってしまうのも空しいので、自分自身を縦方向に最大化するサンプルプログラムを挙げておく。以下は追加分。

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

'---------------------------------------------------------------------------
' 関数名: SetVerticalMaxmum
' 機能  : ウィンドウを縦方向に最大化する
' 引数  : (in) hWnd … メインウィンドウのハンドル
' 戻り値: 正常:0以外、 エラー:0
'---------------------------------------------------------------------------
Public Function SetVerticalMaxmum(ByVal hWnd As Long) As Long
    Dim ClientRect As RECT
    Dim DesktopRect As RECT

    '指定ウィンドウのサイズを取得
    Call GetWindowRect(hWnd, ClientRect)

    'デスクトップのサイズを取得する
    Call GetDesktopRect(DesktopRect)

    '縦方向に最大化する
    SetVerticalMaxmum = MoveWindow(hWnd, ClientRect.Left, DesktopRect.Top, _
                           ClientRect.Right - ClientRect.Left, _
                           DesktopRect.Bottom - DesktopRect.Top, True)

End Function

ということで以下のロジックを実行のこと。

Private Sub Command1_Click()

    Call SetVerticalMaxmum(Me.hWnd)

End Sub

戻る