GFMoveMinimizedWindow/Form1.frm

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3075
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4695
   LinkTopic       =   "Form1"
   ScaleHeight     =   3075
   ScaleWidth      =   4695
   StartUpPosition =   3 'Windows‑Standard
   Begin VB.CommandButton Command2
      Caption         =   "Move Min. Window"
      Height          =   375
      Left            =   1740
      TabIndex        =   1
      Top             =   60
      Width           =   1635
   End
   Begin VB.CommandButton Command1
      Caption         =   "Show Window 2"
      Height          =   375
      Left            =   60
      TabIndex        =   0
      Top             =   60
      Width           =   1635
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'(c)2003, 2004 by Louis. Use to 'move' a minimized window.
'
'Downloaded from www.louis‑coder.com.
'This sample shows how one could move a VB window that is minimized
'(Form.Move doesn't work when being minimized).
'
'GFMoveMinimizedWindow
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
'GFMoveMinimizedWindow
Private Type POINTAPI
    x As Long
    y As Long
End Type
'GFMoveMinimizedWindow
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
'GFMoveMinimizedWindow
Private Type WINDOWPLACEMENT
    Length As Long
    flags As Long
    showCmd As Long
    ptMinPosition As POINTAPI
    ptMaxPosition As POINTAPI
    rcNormalPosition As RECT
End Type
'GFMoveMinimizedWindow
Private Const WPF_SETMINPOSITION = &H1
Private Const SW_SHOWNA = 8

Private Sub Command1_Click()
    'on error resume next
    Form2.Show
End Sub

Private Sub Command2_Click()
    'on error resume next
    Form2.WindowState = vbMinimized
    Form2.Refresh
    Debug.Print GFMoveMinimizedWindow(Form2.hwnd, 50, 25)
    Form2.WindowState = vbNormal
    Form2.Refresh
End Sub

Private Function GFMoveMinimizedWindow(ByVal WindowHandle As LongByVal WindowXPosNew As LongByVal WindowYPosNew As Long) As Boolean
    'on error resume next 'window will appear at the given position when restored (size stays unchanged); returns True if successful, False in case of an error
    Dim WINDOWPLACEMENTVar As WINDOWPLACEMENT
    Dim WindowHeightUnchanged As Long
    Dim WindowWidthUnchanged As Long
    'verify
    If IsIconic(WindowHandle) = 0& Then
        GFMoveMinimizedWindow = False 'error
        Exit Function
    End If
    'preset
    WINDOWPLACEMENTVar.Length = Len(WINDOWPLACEMENTVar)
    'begin
    Call GetWindowPlacement(WindowHandle, WINDOWPLACEMENTVar)
    '
    WindowWidthUnchanged = WINDOWPLACEMENTVar.rcNormalPosition.Right ‑ WINDOWPLACEMENTVar.rcNormalPosition.Left 'VB's size determining (.Width‑property) would fail (tested)
    WindowHeightUnchanged = WINDOWPLACEMENTVar.rcNormalPosition.Bottom ‑ WINDOWPLACEMENTVar.rcNormalPosition.Top
    '
    WINDOWPLACEMENTVar.flags = WPF_SETMINPOSITION
    WINDOWPLACEMENTVar.showCmd = SW_SHOWNA
    '
    WINDOWPLACEMENTVar.rcNormalPosition.Left = WindowXPosNew
    WINDOWPLACEMENTVar.rcNormalPosition.Top = WindowYPosNew
    WINDOWPLACEMENTVar.rcNormalPosition.Right = WindowXPosNew + WindowWidthUnchanged
    WINDOWPLACEMENTVar.rcNormalPosition.Bottom = WindowYPosNew + WindowHeightUnchanged
    '
    GFMoveMinimizedWindow = CBool(SetWindowPlacement(WindowHandle, WINDOWPLACEMENTVar))
    Exit Function
End Function


[END OF FILE]