Sunday, April 10, 2016

Minimize All Windows (Show Desktop) in Visual Foxpro



Below I am providing two ways to minimize all of the applications (top level windows) currently running.  Cut-N-Paste the code below into prg file(s) and run the different alternatives to see how it works.

******************FIRST WAY TO DO IT*******************

*!* Let's Minimize and UnMinimize All Top Level Windows
 
#DEFINE WM_COMMAND    0x111
DECLARE INTEGER FindWindow IN user32 STRING, STRING
DECLARE INTEGER SendMessage IN user32 INTEGER, INTEGER, INTEGER, INTEGER
SendMessage(FindWindow("Shell_TrayWnd", NULL), WM_COMMAND, 415, 0) &&Minimize All
=INKEY(2) &&Give windows a chance to catch up
MESSAGEBOX("Now we will undo the minimize")
SendMessage(FindWindow("Shell_TrayWnd", NULL), WM_COMMAND, 416, 0) &&Undo Minimize

******************SECOND WAY TO DO IT*******************

*!* Let's Minimize All Top Level Windows
 
DECLARE INTEGER CloseWindow IN user32 INTEGER
DECLARE INTEGER GetActiveWindow IN user32
DECLARE INTEGER GetWindow IN user32 INTEGER, INTEGER
DECLARE INTEGER GetWindowTextLength IN user32 INTEGER
DECLARE INTEGER IsIconic IN user32 INTEGER
DECLARE INTEGER IsWindow IN user32 INTEGER
DECLARE INTEGER IsWindowVisible IN user32 INTEGER

LOCAL nActive, nCounter, nHwnd, nIndex
LOCAL cWindowCaption

nCounter = 0
nHwnd = -1

nActive = GetActiveWindow()

DO WHILE nHwnd != GetWindow(nActive, 1) &&1 = FIRST WINDOW
    IF nHwnd != -1
        nHwnd = GetWindow(nHwnd, 2) &&2 = NEXT WINDOW
    ELSE
        nHwnd = GetWindow(nActive, 0) &&0 = LAST WINDOW
    ENDIF

    IF GetWindowTextLength(nHwnd) > 0 AND IsWindow(nHwnd) != 0 AND IsWindowVisible(nHwnd) != 0
        nCounter = nCounter + 1
        DIMENSION aryWindows(nCounter)
        aryWindows(nCounter) = nHwnd
    ENDIF
ENDDO

FOR nIndex=1 TO nCounter
    If IsIconic(aryWindows(nIndex)) = 0
        IF CloseWindow(aryWindows(nIndex)) = 0
            *!* Non-existant window or something went wrong
        ENDIF
    ENDIF
ENDFOR 

******************SCARY WAY TO DO IT*******************

*!* Use the following with EXTREME CAUTION
*!* Will minimize all windows including hidden ones
*!* Won't break anything but you will have a
*!* window mess on your hands  It is kind
*!* of informative so for the brave go ahead
*!* and uncomment it and run it
*!*    #DEFINE HWND_BROADCAST    0xffff
*!*    #DEFINE SC_MINIMIZE       0xF020
*!* #DEFINE WM_SYSCOMMAND   0x112
*!* DECLARE INTEGER SendMessage IN user32 INTEGER, INTEGER, INTEGER, INTEGER
*!* SendMessage (HWND_BROADCAST, WM_SYSCOMMAND, SC_MINIMIZE, 0)

No comments:

Post a Comment