Article ID: 194914 - Last Review: July 14, 2004 - Revision: 3.2

How To Minimize All Windows From Visual Basic

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q194914

On This Page

Expand all | Collapse all

SUMMARY

Occasionally you would like to programmatically minimize all visible windows. Using the keybd_event API, this can easily be accomplished.

MORE INFORMATION

The trick is to mimic the keyboard events required to bring the Taskbar popup menu and send it the letter "M" to select the "Minimize All Windows" option. This is accomplished with three calls to the keybd_event API.

The second argument for the keybd_event call is the hardware scan code, and, in this case, you could use the value 91. However, because applications should not use the scan code, it has been left as 0.

Step-by-Step Example

  1. Start a new Standard EXE project. Form1 is created by default.
  2. Place a CommandButton onto Form1.
  3. Copy and paste the following code into Form1's Code Window.
          Private Declare Sub keybd_event Lib "user32" ( _
             ByVal bVk As Byte, _
             ByVal bScan As Byte, _
             ByVal dwFlags As Long, _
             ByVal dwExtraInfo As Long)
    
          Const KEYEVENTF_KEYUP = &H2
          Const VK_LWIN = &H5B
    
          Private Sub Command1_Click()
             ' 77 is the character code for the letter 'M'
             Call keybd_event(VK_LWIN, 0, 0, 0)
             Call keybd_event(77, 0, 0, 0)
             Call keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)
          End Sub
    
    						
  4. Press the F5 key to run the application and Click on Command1. All visible windows will minimize.

REFERENCES

For more information, please Search on keybd_event in either the Win32 Programmer's Reference or The Microsoft Developer Network (MSDN) Library CD-ROM.

APPLIES TO
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Keywords: 
kbhowto kbapi KB194914