在process.start()之前设置进程窗口大小和位置

我正在使用表单来启动新的Flash进程,使用Process.start()和MoveWindow()来resize并更改进程窗口位置。 问题是在调用MoveWindow()之前,您可以看到窗口的默认大小和位置一瞬间。 我想知道在实际进程开始之前是否有办法设置窗口的位置和大小。

Process flash = new Process(); flash.StartInfo.WindowStyle = ProcessWindowStyle.Normal; flash.StartInfo.FileName = "C:\\RED\\bin\\android.swf"; flash.Start(); Thread.Sleep(300); mainForm.MoveWindow(flash.MainWindowHandle, posX, 0, 1920, 1080, true); 

使用CreateProcess 。 我有同样的需要用位置创建另一个进程。

我已经定义了PInvoke,如下所示。

 public class Kernel32 { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STARTUPINFO { public uint cb; public string lpReserved; public string lpDesktop; public string lpTitle; public uint dwX; public uint dwY; public uint dwXSize; public uint dwYSize; public uint dwXCountChars; public uint dwYCountChars; public uint dwFillAttribute; public uint dwFlags; public short wShowWindow; public short cbReserved2; public IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public int dwProcessId; public int dwThreadId; } [StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public int bInheritHandle; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STARTUPINFOEX { public STARTUPINFO StartupInfo; public IntPtr lpAttributeList; } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); } 

并使用如下所示。

 const uint NORMAL_PRIORITY_CLASS = 0x0020; const uint CREATE_UNICODE_ENVIRONMENT = 0x0400; const uint STARTF_USESHOWWINDOW = 0x0001; var pInfo = new Kernel32.PROCESS_INFORMATION(); var sInfo = new Kernel32.STARTUPINFO(); sInfo.dwX = (uint)hostingApp.X; // X Position sInfo.dwY = (uint)hostingApp.Y; // Y Position sInfo.dwXSize = (uint)hostingApp.Width; // Width sInfo.dwYSize = (uint)hostingApp.Height; // Height sInfo.dwFlags = STARTF_USESHOWWINDOW; var pSec = new Kernel32.SECURITY_ATTRIBUTES(); var tSec = new Kernel32.SECURITY_ATTRIBUTES(); pSec.nLength = System.Runtime.InteropServices.Marshal.SizeOf(pSec); tSec.nLength = System.Runtime.InteropServices.Marshal.SizeOf(tSec); var create_result = Kernel32.CreateProcess( fileName, // lpApplicationName " " + arguments, // lpCommandLine ref pSec, // lpProcessAttributes ref tSec, // lpThreadAttributes false, // bInheritHandles NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT, // dwCreationFlags IntPtr.Zero, // lpEnvironment null, // lpCurrentDirectory ref sInfo, // lpStartupInfo out pInfo); // lpProcessInformation var process = Process.GetProcessById(pInfo.dwProcessId); 

希望这可以帮助。

查看ProcessStartInfo.WindowStyle 。 您应该可以将其设置为Hidden直到您移动并调整其大小,然后将其恢复正常。

看到这里 :

隐藏的窗口样式。 窗口可以是可见的或隐藏的。 系统通过不绘制来显示隐藏的窗口。 如果窗口被隐藏,则会被有效禁用。 隐藏窗口可以处理来自系统或其他窗口的消息,但它无法处理来自用户或显示输出的输入。 通常,应用程序可以在自定义窗口外观时隐藏新窗口,然后使窗口样式为“正常”。 要使用ProcessWindowStyle.Hidden,ProcessStartInfo.UseShellExecute属性必须为false。

有关如何在流程开始后更改样式,请参阅以下问题:

在运行时切换Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden