Minimize external application using Delphi

Is there a way to minimize an external application that I cannot control because of in my Delphi application?

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

+5
source share
4 answers

You can use FindWindow to find the application descriptor 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;
+8
source

Delphi, win32 apis, FindWindow ShowWindow , .

+3

Thanks for that, in the end I used a modified version of the Neftali code , I included it below if someone else has problems in the future,

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

always returned 0, therefore, looking for the reason why I found this function that will find hwnd, and it worked.

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;
+2
source

I think FindWindow (PChar ('notepad'), nil) should be FindWindow (nil, PChar ('notepad')) to find the window by name.

0
source

All Articles