【问题标题】:Minimize a external application with Delphi使用 Delphi 最小化外部应用程序
【发布时间】:2010-09-13 02:19:25
【问题描述】:

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

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

【问题讨论】:

    标签: delphi window minimize


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我投了你一票,因为你让我走上了正轨,但最后我使用了 Neftali 的代码
      【解决方案3】:

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

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

      总是返回 0,所以在寻找我找到 this function 的原因时,它会找到 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;
      

      【讨论】:

      • 请注意,这在 Windows Vista 上不起作用,除非您的应用程序以提升的权限运行。
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多