Q: How to minimise all open windows

Can some kind soul please let me know how to minimise all open windows on the desktop.

(VB5/SP3 / WIN95/98)

Many thanks.
Who is Participating?
Hi there,

There is a way to achieve it using the API - the following code should work fine, although i've not had a chance to thoroughly test it...

First - in a module under generate declarations:

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public lMinimList() As Long

Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const GWL_STYLE = (-16)
Public Const WS_MINIMIZEBOX = &H20000

And then - assign the following code to a button!!!

'Declare local variables
Dim lDeskTop As Long
Dim lChildWindow As Long
Dim lRetVal As Long
Dim lStyle As Long
Dim bFirstWindow As Boolean
Dim iX As Integer
Dim iWindowCount As Integer

'Set initial variable states
bFirstWindow = True

'Get the hWnd for the desktop and then get the first child of the
lDeskTop = GetDesktopWindow()
lChildWindow = GetWindow(lDeskTop, GW_CHILD)

'Loop through all the children of the desktop
Do While lChildWindow <> 0
    'Get the style mask for the current child window
    lStyle = GetWindowLong(lChildWindow, GWL_STYLE)
    'If the window can be minimized then....
    If lStyle And WS_MINIMIZEBOX Then
        'Check the hWnd is for a window and that the window is enabled and visible; this prevents
        'hidden system windows from being minimized - doing so would cause major problems...
        If IsWindow(lChildWindow) Then
            If IsWindowEnabled(lChildWindow) Then
                If IsWindowVisible(lChildWindow) Then
                    'If the window was valid then resize the array to hold the hWnd as necessary
                    If bFirstWindow Then
                        bFirstWindow = False
                        ReDim lMinimList(0)
                        ReDim lMinimList(UBound(lMinimList) + 1)
                    End If
                    'Load the hWnd into the array - we'll minimize all the windows in the array
                    lMinimList(UBound(lMinimList)) = lChildWindow
                End If
            End If
        End If
    End If
    'Get the next child window
    lChildWindow = GetWindow(lChildWindow, GW_HWNDNEXT)


'Determine how many windows we need to minimize and store in a variable
iWindowCount = UBound(lMinimList)

'Loop through the array, using the SizeChange API to minimize each in turn
For iX = 0 To iWindowCount
    lRetVal = ShowWindow(lMinimList(iX), SW_SHOWMINNOACTIVE)
Next iX

Hope this helps


You could try the following code:

Dim frm as Form

For Each frm In Forms
   frm.WindowState = vbMinimized
Next frm

This program scan through all forms in the desktop and minimize them.
scvbAuthor Commented:
I may have misphrased my question ...

I want to minimise all open PROGRAM windows on the desktop. Your sample will work well if only my app's forms needed to be minned, but not other (external) apps.

Looking forward to hearing from you.



Sorry, I accidentally missed out the 'preserve' keyword in the redim statement about half way through the main routine.  Just change the line:

ReDim lMinimList(UBound(lMinimList) + 1)

to read

ReDim Preserve lMinimList(UBound(lMinimList) + 1)

and everything should be fine!!!

Sorry 'bout that!!


scvbAuthor Commented:
Thanks very much indeed. Yes, the Redim statement fixed it!!
Brilliant answer!

All the best.

Kind regards,
    Tony Jones
    Southern Cross Visual Basic Code and Links

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.