我在 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 aSTARTUPINFO
structure. Otherwise, the first timeShowWindow
is called, the value should be the value obtained by theWinMain
function in itsnCmdShow
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/