使用 Delphi 最小化外部应用程序

发布于 2024-07-05 18:26:33 字数 88 浏览 6 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-07-12 18:26:33

您可以使用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;

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;
长伴 2024-07-12 18:26:33

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

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.

沉睡月亮 2024-07-12 18:26:33

谢谢你,最后我使用了 Neftali's 的修改版本代码,我将其包含在下面,以防其他人将来遇到相同的问题。

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;

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;
梦醒时光 2024-07-12 18:26:33

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文