微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 CreateProcess 忽略 STARTUPINFO 窗口大小值?

如何解决为什么 CreateProcess 忽略 STARTUPINFO 窗口大小值?

我正在尝试使用 CreateProcess 以特定窗口大小启动记事本,以避免在我调整大小和移动它之前它出现在某处一瞬间发生的闪烁。

CreateProcess 启动记事本,但 CreateProcess 和记事本都忽略 STARTUPINFO 结构中的窗口尺寸。记事本以上次关闭时的正常大小出现在正常位置。

我尝试过的 dwFlags 组合都不起作用。记事本要么根本不出现,要么忽略我的设置并出现在由操作系统决定的位置和大小。

为什么 CreateProcess 忽略我在 STARTUPINFO 中设置的值?我错过了什么吗?

[TestMethod()]
  public void CreateProcesstest() {
    const uint norMAL_PRIORITY_CLASS = 0x0020;
    const uint STARTF_USESHOWWINDOW = 0x0001;

    // create structures needed by CreateProcess
    var pInfo = new Kernel32.PROCESS_informatION();
    var pSec = new Kernel32.Security_ATTRIBUTES();
    var tSec = new Kernel32.Security_ATTRIBUTES();
    pSec.nLength = Marshal.SizeOf(pSec);
    tSec.nLength = Marshal.SizeOf(tSec);

    // set the app and a file to open
    var app = Environment.GetEnvironmentvariable("windir") + @"\notepad.exe";
    var arguments = @" C:\somefile.txt";

    // the started app window does not use these values
    var sInfo = new Kernel32.STARTUPINFO();
    sInfo.dwX = 800;  // desired x-y position of the window
    sInfo.dwY = 400;
    sInfo.dwXSize = 200; // desired size of the window
    sInfo.dwYSize = 400;

    // no combination of these flags that I tried makes any difference
    // Notepad always appears as normal and ignores the size settings above
    sInfo.dwFlags = STARTF_USESHOWWINDOW;
    sInfo.wShowWindow = (short) Win32.SW_SHOW;

    // create the process
    var result = Kernel32.CreateProcess(app,arguments,ref pSec,ref tSec,false,norMAL_PRIORITY_CLASS,IntPtr.Zero,null,ref sInfo,out pInfo);
  }

解决方法

根据MSDN: STARTUPINFO 的 dwFlags 成员需要添加 STARTF_USEPOSITIONSTARTF_USESIZE

已编辑,
存储在注册表目录:Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad。

iWindowPosDX
iWindowPosDY
iWindowPosX
iWindowPosY

,

您的整个 STARTUPINFO 结构被忽略,因为您没有正确设置 cb 成员。我有点惊讶 CreateProcess 并没有完全失败 ERROR_INVALID_PARAMETER

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。