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

标签 delphi console-application

我在 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 将您通过 CreateProcess() 使用 SHELLEXECUTEINFO 提供的信息传递给控制台应用程序,当您第一次尝试显示控制台窗口时,控制台应用程序会遵循该信息。

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 就会生效。在后续调用中,将改为您指定的参数生效。

关于Delphi - 从 SW_HIDE 状态显示控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39778261/

相关文章:

delphi - 如何捕获Fn?笔记本电脑上的按键?

delphi - 在 Delphi 应用程序中查看 Autocad 绘图

c - 使用 "console font-size"中的 "GetCurrentConsoleFont()"检索 "Windows XP"失败?

c# - .NET应用程序将作为控制台应用程序运行,但不能作为Windows窗体运行,调试工作

c# - 我可以在单元测试中写入控制台吗?如果是,为什么控制台窗口不打开?

performance - Delphi编译时间特别慢

delphi - 我无法在 delphi xe2 中使用 jpeg

delphi - 如何卸载JVCL?

C# 在运行时修改控制台字体、字体大小?

c# - 必须声明表变量@table