gpt4 book ai didi

Delphi:shellexecute 和 sw_hide

转载 作者:行者123 更新时间:2023-12-02 02:52:03 29 4
gpt4 key购买 nike

我尝试运行隐藏的应用程序,但应用程序表单仍然可见。

ShellExecute(Handle, nil, 'app.exe', nil, nil, SW_HIDE);

如何在Delphi中运行隐藏的应用程序?

最佳答案

我建议改用CreateProcess,因为它返回新启动的应用程序的进程ID,您可以使用它来获取窗口的句柄。这是我一直在使用的一个功能,也许你可以去掉不必要的片段并根据你的需要进行调整?

// record to store window information
TWndInfo = record
pid: DWord;
WndHandle: HWND;
width, height: Integer;
end;
PWndInfo = ^TWndInfo;
{$HINTS OFF}
{ .: ExecNewProcess :. }
function ExecNewProcess(const ProgramName: String;
const StartHidden, WaitForInput: Boolean; out WndInfo: TWndInfo): Boolean;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
R: TRect;
SL: TStringList;

{$REGION 'EnumProcess'}
function EnumProcess(hHwnd: HWND; lParam: Integer): Boolean; stdcall;
var
WndInfo: PWndInfo;
pid: DWORD;
begin
Result := True;

WndInfo := PWndInfo(lParam);
if (WndInfo = nil) or (hHwnd = 0) then
exit;

GetWindowThreadProcessId(hHwnd, pid);
if (pid = WndInfo.PID) then
begin
if (WndInfo.WndHandle = 0) and (IsWindowVisible(hHwnd)) then
WndInfo.WndHandle := hHwnd;
//Result := False;
end;
end;
{$ENDREGION}

begin
Result := False;

ZeroMemory(@StartInfo, SizeOf(TStartupInfo));
ZeroMemory(@ProcInfo, SizeOf(TProcessInformation));

StartInfo.cb := SizeOf(TStartupInfo);
StartInfo.dwFlags := STARTF_USESTDHANDLES;
if StartHidden then
begin
StartInfo.dwFlags := STARTF_USESHOWWINDOW or StartInfo.dwFlags;
StartInfo.wShowWindow := SW_SHOWMINNOACTIVE;
end;

Result := CreateProcess(PChar(ProgramName), nil, nil, nil, False, 0, nil,
nil, StartInfo, ProcInfo);
try
if Result then
begin
WndInfo.WndHandle := 0;
WndInfo.PID := ProcInfo.dwProcessId;

if WaitForInput then
WaitForInputIdle(ProcInfo.hProcess, INFINITE);

EnumWindows(@EnumProcess, Integer(@WndInfo));
if (WndInfo.WndHandle <> 0) then
begin
if (StartHidden) then
ShowWindow(WndInfo.WndHandle, SW_HIDE);
Windows.GetWindowRect(WndInfo.WndHandle, R);

WndInfo.Width := R.Right - R.Left;
WndInfo.Height := R.Bottom - R.Top;
end;
end;
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
{$HINTS ON}

关于Delphi:shellexecute 和 sw_hide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273588/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com