Imports System.Runtime.InteropServices   'これが無いと Marshal が宣言されていないとエラーになる 

Module Windows

    'サイズ変更したフォームのサイズと位置を設定・取得
    'http://oshiete1.goo.ne.jp/qa2080663.html

    Public l_WINDOWPLACEMENT As WINDOWPLACEMENT
    Public l_blnRet As Boolean
    Public l_ptr As IntPtr = Form1.Handle
    Public Position_ini_filename As String = Application.StartupPath() & "\read_Hamlog_posi.ini"

    Public Enum Flags_WPF
        WPF_SETMINPOSITION = &H1
        WPF_RESTORETOMAXIMIZED = &H2
    End Enum

    Public Enum showCmd_SW
        SW_HIDE = 0
        SW_SHOWNORMAL = 1
        SW_SHOWMINIMIZED = 2
        SW_SHOWMAXIMIZED = 3
        SW_SHOWNOACTIVATE = 4
        SW_SHOW = 5
        SW_MINIMIZE = 6
        SW_SHOWMINNOACTIVE = 7
        SW_SHOWNA = 8
        SW_RESTORE = 9
    End Enum

    Public Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Public Structure WINDOWPLACEMENT
        Dim Length As Integer
        Dim Flags As Flags_WPF
        Dim showCmd As showCmd_SW
        Dim ptMinPosition As Point
        Dim ptMaxPosition As Point
        Dim rcNormalPosition As RECT
    End Structure

    Public Declare Auto Function GetWindowPlacement Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByRef lpwndpl As WINDOWPLACEMENT) As Boolean

    Public Declare Auto Function SetWindowPlacement Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByRef lpwndpl As WINDOWPLACEMENT) As Boolean

    Public Sub PutIniPosition_read_Hamlog()

        'サイズ変更したフォームのサイズと位置を取得

        '構造体のサイズ情報を格納
        l_WINDOWPLACEMENT.Length = Marshal.SizeOf(GetType(WINDOWPLACEMENT))
        '取得実行
        l_blnRet = GetWindowPlacement(l_ptr, l_WINDOWPLACEMENT)

        With l_WINDOWPLACEMENT.rcNormalPosition
            PutIni("read_Hamlog""Left", .Left, Position_ini_filename)
            PutIni("read_Hamlog""top", .Top, Position_ini_filename)
            PutIni("read_Hamlog""Right", .Right, Position_ini_filename)
            PutIni("read_Hamlog""Bottom", .Bottom, Position_ini_filename)
        End With

    End Sub

    Public Sub GetIniPosition_read_Hamlog()

        'サイズ変更したフォームのサイズと位置を設定(再現)
        Position_left = GetIni("read_Hamlog""Left""", Position_ini_filename)
        Position_top = GetIni("read_Hamlog""Top""", Position_ini_filename)
        Position_right = GetIni("read_Hamlog""Right""", Position_ini_filename)
        Position_bottom = GetIni("read_Hamlog""Bottom""", Position_ini_filename)

        GetIniPosition_sub()

    End Sub

    Public Sub PutIniPosition_Form2()

        'サイズ変更したフォームのサイズと位置を取得

        '構造体のサイズ情報を格納
        l_WINDOWPLACEMENT.Length = Marshal.SizeOf(GetType(WINDOWPLACEMENT))
        '取得実行
        l_blnRet = GetWindowPlacement(l_ptr, l_WINDOWPLACEMENT)

        With l_WINDOWPLACEMENT.rcNormalPosition
            PutIni("Form2""Left", .Left, Position_ini_filename)
            PutIni("Form2""top", .Top, Position_ini_filename)
            PutIni("Form2""Right", .Right, Position_ini_filename)
            PutIni("Form2""Bottom", .Bottom, Position_ini_filename)
        End With

    End Sub

    Public Sub GetIniPosition_Form2()

        'サイズ変更したフォームのサイズと位置を設定(再現)
        Position_left = GetIni("Form2""Left""", Position_ini_filename)
        Position_top = GetIni("Form2""Top""", Position_ini_filename)
        Position_right = GetIni("Form2""Right""", Position_ini_filename)
        Position_bottom = GetIni("Form2""Bottom""", Position_ini_filename)

        GetIniPosition_sub()

    End Sub

    Public Sub GetIniPosition_sub()

        l_WINDOWPLACEMENT.Length = Marshal.SizeOf(GetType(WINDOWPLACEMENT))

        With l_WINDOWPLACEMENT
            With .rcNormalPosition
                .Left = Val(Position_left)   '左からの位置
                .Top = Val(Position_top)    '上からの位置
                .Right = Val(Position_right)  'サイズ 幅
                .Bottom = Val(Position_bottom) 'サイズ 高さ
            End With
        End With
        l_blnRet = SetWindowPlacement(l_ptr, l_WINDOWPLACEMENT)


    End Sub


End Module