Knowledge Base

How To Minimize All Windows From Visual Basic

Article ID: 194914

Article Last Modified on 7/14/2004


APPLIES TO


This article was previously published under Q194914

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.

Keywords: kbhowto kbapi KB194914