使用Delphi最小化外部应用程序
时间:2020-03-06 14:43:14 来源:igfitidea点击:
有没有一种方法可以最小化我无法控制的Delphi应用程序中的外部应用程序?
例如notepad.exe,除了我要最小化的应用程序将永远只有一个实例。
解决方案
我不是Delphi专家,但是如果我们可以调用win32 api,则可以使用FindWindow和ShowWindow最小化一个窗口,即使它不属于应用程序也是如此。
我们可以使用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;
为此,最后我使用了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;

