Delphi - 从 SW_HIDE 状态显示控制台应用程序

Delphi - show a console app from SW_HIDE state

我在 Delphi 中有一个控制台应用程序,我是这样从其他应用程序开始的:

FillChar(ExecInfo, SizeOf(ExecInfo), 0);
With ExecInfo Do Begin
  cbSize :=       SizeOf(ExecInfo);
  fMask :=        SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;
  Wnd :=          GetActiveWindow();
  lpVerb :=       PChar('runas');
  lpFile :=       PChar(FsCurrentPath + 'Install\Install_Elevated.exe');
  lpDirectory :=  PChar(FNew.sBinDir);
  lpParameters := PChar(sl.DelimitedText);
  nShow :=        SW_HIDE
End;
ShellExecuteEx(@ExecInfo);

在某些情况下,我想让它显示自己(进入 SW_SHOWNORMAL 状态)。我该怎么做?

这样不显示:

ShowWindow(GetConsoleWindow, SW_SHOW);

即使不是这样:

BringWindowToTop(GetConsoleWindow);
SetActiveWindow(GetConsoleWindow);
SetForegroundWindow(GetConsoleWindow);
ShowWindow(GetConsoleWindow, SW_SHOW)

但它是这样显示的:

MessageBox(GetConsoleWindow, PChar(IntToStr(GetConsoleWindow)), PChar(''), MB_SETFOREGROUND);
ShowWindow(GetConsoleWindow, SW_SHOW);

但是我当然不想要这个消息框。

有什么问题?

shell 将您通过 SHELLEXECUTEINFO 提供的信息通过 CreateProcess() 传递给控制台应用程序,当您首次尝试显示控制台时,它会接受该信息 window .

ShowWindow() 的文档说:

nCmdShow [in]
Type: int

Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values...

因此,您第一次调用 ShowWindow 时,传递给 ShellExecuteEx()SW_HIDE 生效。在后续调用中,您指定的参数将生效。