I am trying to use CreateProcess to start Notepad at a specific window size to avoid the flashing that occurs when it appears somewhere for a split second before I can resize it and move it.
CreateProcess starts Notepad but CreateProcess and Notepad both ignore the window dimensions in the STARTUPINFO structure. Notepad appears in the normal place at the normal size where it was last closed.
No combination of dwFlags
that I tried worked. Notepad either does not appear at all or ignores my settings and appears in a place and size determined by the operating system.
Why is CreateProcess ignoring the values I set in STARTUPINFO? Am I missing something?
(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);
}