有没有办法最小化我无法使用我的Delphi应用程序控制的外部应用程序?

例如notepad.exe,除了我想要最小化的应用程序只会有一个实例。

有帮助吗?

解决方案

您可以使用 FindWindow 查找应用程序句柄,使用 ShowWindow 来最小化它。

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;

其他提示

我不是Delphi专家,但如果您可以调用win32 apis,则可以使用FindWindow和ShowWindow来最小化窗口,即使它不属于您的应用程序。

多亏了这一点,最后我使用了修改版的 Neftali的代码,我已将其包含在下面,以防其他任何人在将来遇到相同的问题。

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

总是返回0,所以在寻找我找到这个的原因时功能,它会找到hwnd,并且有效。

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;

我猜FindWindow(PChar('notepad'),nil)应该是FindWindow(nil,PChar('notepad'))来按标题查找窗口。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top