# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  How To Minimize All Open Windows

## RobDog888

This code example will minimize all your open windows simulating the "Show Desktop" quick launch button shortcut next to the Start windows menu button.


VB Code:
Option Explicit
'Copyright © 2005 by RobDog888 (VB/Office Guru). All Rights reserved.
'
'Distribution: You can freely use this code in your own
'              applications provided that this copyright
'              is left unchanged, but you may not reproduce
'              or publish this code on any web site, online
'              service, or distribute as source on any
'              media without express permission.
'
'Add a command button to your form and copy/paste this code
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
 Const VK_STARTKEY = &H5B
Const VK_M = 77
Const KEYEVENTF_KEYUP = &H2
 Private Sub Command1_Click()
    'WinKey down
    keybd_event VK_STARTKEY, 0, 0, 0
    'M key down
    keybd_event VK_M, 0, 0, 0
    'M key up
    keybd_event VK_M, 0, KEYEVENTF_KEYUP, 0
    'WinKey up
    keybd_event VK_STARTKEY, 0, KEYEVENTF_KEYUP, 0
End Sub

----------


## dglienna

You should have a way to contact you if you want someone to ask for permission to reproduce it.

----------


## RobDog888

Yes, that might be a good idea. For now they can just send me a PM as I am _always_ on the Forums.  :Smilie:

----------


## dglienna

Randy's lets you use about 6 different actions.

http://vbnet.mvps.org/code/screen/keybd_event.htm

but I see he doesn't include any contact info, either.

Gary Beenes site does it in less code, without any keypress

http://www.garybeene.com/code/visual%20basic158.htm

no disclaimers, either

----------


## iPrank

Alternate Ideas  :Big Grin: 

(*PS:* If anyone finds a way to call the *Shell32.IShellDispatch4.ToggleDesktop()* method from VB6, please let me know.  :wave: ) - Resolved

----------


## tony007

Thank u rob for sharing this nice code. Is there away not to minimize the form that has the button for this code?  I be happy if u show me how not to minimize the form iteself.Thanks

----------


## RobDog888

You mean minimize all windows but one?

----------


## tony007

> You mean minimize all windows but one?


yes. minimize all but not the one that holds the button and this code .

----------


## RobDog888

If its your own form/window then you can easily just add Me.WindowState = vbMaximized.

----------


## iPrank

Just a note,
Rob, when you minimize windows using th "Show Desktop" (any) method, the Form_Resize event doesn't fire.

So your idea,

VB Code:
MinimizeAll
Me.WindowState= vbNormal
looks like it is the best method without subclassing.  :Thumb:

----------


## tony007

> If its your own form/window then you can easily just add Me.WindowState = vbMaximized.


i do not want to maximize the form itself. I do not want it to get placed in task bar! I tried that method  it made the form itself bix and placed it in task bar!

----------


## iPrank

> i do not want to maximize the form itself. I do not want it to get placed in task bar!


By setting the form's ShowInTaskBar property to false ?  :Confused:

----------


## tony007

> By setting the form's ShowInTaskBar property to false ?


Is there a way not to maximize it just leave same size?Thanks

----------


## RobDog888

No it doesnt but you can subclass your form and trap the window message.

----------


## RobDog888

> Is there a way not to maximize it just leave same size?Thanks


 Just use the Me.WindowState = vbNormal but you will need to do like I just posted in order to trap the event.

----------


## tony007

> Just use the Me.WindowState = vbNormal but you will need to do like I just posted in order to trap the event.


Many thanks to u . It worked well . Is there a  way to put all the windows to some sort of system tray instead of task bar?



VB Code:
Private Sub Command1_Click()
    'WinKey down
    keybd_event VK_STARTKEY, 0, 0, 0
    'M key down
    keybd_event VK_M, 0, 0, 0
    'M key up
    keybd_event VK_M, 0, KEYEVENTF_KEYUP, 0
    'WinKey up
    keybd_event VK_STARTKEY, 0, KEYEVENTF_KEYUP, 0
    
[B]    'do not minimiz form itself
    Me.WindowState = vbMaximized
    Me.WindowState = vbNormal[/B]
End Sub

----------


## RobDog888

No AFAIK because you would have to manipulate all windows/programs which is difficult to do. Using some APIs I believe you can remove a window from the taskbar but it may complicate the use of the program or re-appear depending on how the app is designed .

----------


## Merri

Old thread, yes, but you don't need to add anything to your application to get the effect:

```
    Dim Application As Object
    Set Application = CreateObject("Shell.Application")
    Application.MinimizeAll
```

For more information, see MSDN


(I originally was posting a reply to this thread but thought this would be a better choice.)

----------


## RobDog888

Thanks Merri, always more then 1 way to skin a cat.

----------


## dilettante

Or the short version:



```
CreateObject("Shell.Application").MinimizeAll
```

The question is why anyone might want an application doing this spontaneously?  It's my desktop, not some program's to fiddle with.

----------


## Merri

At least I'm doing it in a resolution changing full screen app, and once it completes it calls UndoMinimizeAll. The desktop is back as it was  :Smilie:  The advantage is that when resolution goes very small, not all apps are able to handle it well, especially if they're maximized or if they position themselves to the right edge or bottom of the desktop, as music players can do.

I've also made a bit of custom desktop application and because it literally sets itself to the desktop I have to minimize all the windows to let the user know something happened. And even if it won't be in final version, if it ever gets that far, it is a programming convenience as I don't need to minimize the VB IDE all the time.

----------

