We help IT Professionals succeed at work.

Minimizing all running programs ....

hagur
hagur asked
on
Can anyone show me how I can minimize all open programs by sending a message to them.

Just find everything that is currently running, looping through it and send a minimize message to each one ...
Comment
Watch Question

Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
Hello

  here's two links to do that, in two ways, check them and choose the best way for you

http://www.swissdelphicenter.ch/en/showcode.php?id=223

http://www.q3.nu/trucomania/truco.cgi?301&ing

Best regards
Mohammed Nasman
Here are 3 different ways you could do this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Keybd_Event( VK_LWIN, MapVirtualKey(VK_LWIN, 0), 0, 0);
  Keybd_Event( Ord('M'), MapVirtualKey(Ord('M'), 0), 0, 0);
  Keybd_Event( Ord('M'), MapVirtualKey(Ord('M'), 0), KEYEVENTF_KEYUP, 0);
  Keybd_Event( VK_LWIN, MapVirtualKey(VK_LWIN, 0), KEYEVENTF_KEYUP, 0);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  H : hWnd;
begin
  // Already been posted (above) by mnasman....
  H := Handle;
  while H > 0 do
  begin
    if IsWindowVisible(H) then
      PostMessage(H, WM_SYSCOMMAND, SC_MINIMIZE, 0);
    H := GetNextWindow(H, GW_HWNDNEXT);
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // Requires ShellAPI o be in the uses clause of the unit...
  ShellExecute(Handle, 'Open', 'Show Desktop.scf', '', '', SW_SHOWNORMAL);
end;

Cheers,

Alan
Umm... this?

PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MINIMIZE, 0);

Commented:
or:

var
  Form1: TForm1;
oShell : OleVariant;
implementation

{$R *.DFM}
uses ComObj;

procedure TForm1.FormCreate(Sender: TObject);
begin
oShell := CreateOleObject('Shell.Application');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
oShell.MinimizeAll;
end;

Author

Commented:
Thanks for all the comments,  I've tried two of them, the one mnasman suggested and one of those suggested by alanwhincup.  They both work well, but there is one thing I forgot to mention.

I don't want my program to minimize ... is it possible to change this function so my program wont get minimized?

procedure TForm1.Button2Click(Sender: TObject);
var
 H : hWnd;
begin
 H := Handle;
 while H > 0 do
 begin
   if IsWindowVisible(H) then
     PostMessage(H, WM_SYSCOMMAND, SC_MINIMIZE, 0);
   H := GetNextWindow(H, GW_HWNDNEXT);
 end;
end;

Also, other thing.  I noticed that some programs don't minimize, like Winamp for example.  Is it somekind of a special window?  I also noticed that it did not minimize even when I pressed the "show desktop" shortcut in Windows.

Is there any way around that?

Commented:
this won't send the message to your form ...

procedure TForm1.Button2Click(Sender: TObject);
var
H : hWnd;
begin
H := Handle;
while H > 0 do
begin
  if IsWindowVisible(H) and (H <> Handle) then
    PostMessage(H, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  H := GetNextWindow(H, GW_HWNDNEXT);
end;
end;

PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MINIMIZE, 0);
ShowWindow(Handle, SW_RESTORE);

Commented:
yeah, DragonSlayer's is very straightforward.  minimize all (including yours) then restore the calling form. but i guess hagur won't be contented with that ...

Author

Commented:
Hi,

piscean, I tried your method.  It doesn't seem to work properly .... maybe I'm doing something wrong.
Commented:
Oh, I've forgot! My code will still send SC_MINIMIZE to the main application's handle.  Here's the modified one. This should do the job ...

procedure TForm1.Button1Click(Sender: TObject);
var
  H: hWnd;
begin
  H := Handle;
  while H <> 0 do
    begin
      if not ((H = Handle) or (H = Application.Handle)) then
        if IsWindowVisible(H) then
          PostMessage(H, WM_SYSCOMMAND, SC_MINIMIZE, 0);
      H := GetNextWindow(H, GW_HWNDNEXT);
    end;
end;

Commented:
But I'm sure you'll get a headache on applications currently displaying modal borderless forms (About boxes ...). The form will be minimized (most of them disappeared) leaving the application's main window disabled. tsk, tsk, tsk...

Author

Commented:
Thanks piscean,

I'll try this soon and let you know how it goes.

Commented:
what is heppening if an application have a modal form opened when you minimize all aplication?

Commented:
In most cases, the modal form will minimize and the application's main form will remain disabled. But there are cases when the modal form will become invisible after its state became wsMinimized (try for example InterBase Workbench [www.interbaseworkbench.com]). And some that ignores the minimize message simple remains.

If you used Windows' "Minimize All Windows" command, all applications currently showing modal forms are not affected.
Please update and finalize this very old question.
Moondancer - EE Moderator