delphi - 如何指定外部控制台程序的窗口位置?

标签 delphi console-application

在我的 Win32 VCL 应用程序中,我使用 ShellExecute 来启动许多较小的 Delphi 控制台应用程序。有没有办法控制这些控制台窗口的位置?我想以屏幕为中心启动它们。

最佳答案

您可以使用CreateProcess并在其 STARTUPINFO 中指定窗口大小和位置结构参数。在下面的示例函数中,您可以指定控制台窗口的大小,然后根据指定的大小以当前桌面为中心。该函数返回进程句柄,如果成功,则返回0:

function RunConsoleApplication(const ACommandLine: string; AWidth,
  AHeight: Integer): THandle;
var
  CommandLine: string;
  StartupInfo: TStartupInfo;
  ProcessInformation: TProcessInformation;
begin
  Result := 0;
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  FillChar(ProcessInformation, SizeOf(TProcessInformation), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USEPOSITION or 
    STARTF_USESIZE;
  StartupInfo.wShowWindow := SW_SHOWNORMAL;
  StartupInfo.dwXSize := AWidth;
  StartupInfo.dwYSize := AHeight;
  StartupInfo.dwX := (Screen.DesktopWidth - StartupInfo.dwXSize) div 2;
  StartupInfo.dwY := (Screen.DesktopHeight - StartupInfo.dwYSize) div 2;
  CommandLine := ACommandLine;
  UniqueString(CommandLine);
  if CreateProcess(nil, PChar(CommandLine), nil, nil, False,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInformation)
  then
    Result := ProcessInformation.hProcess;
end;

关于delphi - 如何指定外部控制台程序的窗口位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138836/

相关文章:

java - 从 ArrayList 打印带有空格的字符串不正确

c# - 在控制台应用程序中使用Razor的最佳方法是什么

delphi - Delphi 2007 中的字节数组 PAnsiChar

delphi - Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2

delphi - 如何在调试中禁用事件日志?

delphi - 以字符串获取中间/开头/结尾阿拉伯字符

Google App Engine shell 中的 Python raw_input

delphi - 如何获取TVirtualStringTree的热点节点坐标?

asp.net - 无特殊字符加解密

c# - 如何使用 NDesk.Options 强制执行所需的命令行选项?