有没有办法在我的Delphi应用程序中最小化我无法控制的外部应用程序?
例如notepad.exe,除了我想要最小化的应用程序只会有一个实例。
答案 0 :(得分:8)
您可以使用 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;
答案 1 :(得分:3)
我不是Delphi专家,但如果您可以调用win32 apis,则可以使用FindWindow和ShowWindow来最小化窗口,即使它不属于您的应用程序。
答案 2 :(得分:2)
感谢您,最后我使用了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;
答案 3 :(得分:0)
我猜FindWindow(PChar('notepad'),nil)应该是FindWindow(nil,PChar('notepad'))来按标题查找窗口。