Q: How to minimise all open windows

Posted on 1998-07-02
Medium Priority
Last Modified: 2010-05-03
Can some kind soul please let me know how to minimise all open windows on the desktop.

(VB5/SP3 / WIN95/98)

Many thanks.
Question by:scvb
  • 2
  • 2

Expert Comment

ID: 1464883
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.

Author Comment

ID: 1464884
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.



Accepted Solution

peterwest earned 400 total points
ID: 1464885
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



Expert Comment

ID: 1464886

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!!



Author Comment

ID: 1464887
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


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question