views:

1543

answers:

3

Is there a way to Minimize an external application that I don't have control over from with-in my Delphi application?

for example notepad.exe, except the application I want to minimize will only ever have one instance.

+3  A: 

I'm not a Delphi expert, but if you can invoke win32 apis, you can use FindWindow and ShowWindow to minimize a window, even if it does not belong to your app.

Juanma
I voted you up, as you put me on the right track, but in the end I used Neftali's code
Re0sless
+5  A: 

You can use FindWindow to find the application handle and ShowWindow to minimize it.

var  
  Indicador :Integer;
begin 
  // Find the window by Classname
  Indicador := FindWindow(PChar('notepad'), nil);
  // if finded
  if (Indicador <> 0) then begin
    // Minimize
    ShowWindow(Indicador,SW_MINIMIZE);
  end;
end;
Neftalí
A: 

Thanks for this, in the end i used a modifyed version of Neftali's code, I have included it below in case any one else has the same issues in the future.

FindWindow(PChar('notepad'), nil);

was always returning 0, so while looking for a reason why I found this function that would find the hwnd, and that worked a treat.

function FindWindowByTitle(WindowTitle: string): Hwnd;
    var
      NextHandle: Hwnd;
      NextTitle: array[0..260] of char;
begin
      // Get the first window
      NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);
      while NextHandle > 0 do
      begin
        // retrieve its text
        GetWindowText(NextHandle, NextTitle, 255);
        if Pos(WindowTitle, StrPas(NextTitle)) <> 0 then
        begin
          Result := NextHandle;
          Exit;
        end
        else
          // Get the next window
          NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
      end;
      Result := 0;
end;

procedure hideExWindow()
var Indicador:Hwnd;
begin
    // Find the window by Classname
    Indicador := FindWindowByTitle('MyApp'); 
    // if finded
    if (Indicador <> 0) then
    begin
        // Minimize
        ShowWindow(Indicador,SW_HIDE); //SW_MINIMIZE
    end;
end;
Re0sless
Note that this will not work on Windows Vista unless your application run with elevated privileges.
Lars Fosdal