Setting process window size and location before process.start()

后端 未结 2 1153
清歌不尽
清歌不尽 2021-01-22 13:44

I am using a form to start new flash processes using Process.start() and MoveWindow() to resize and change the process window location. The problem is before MoveWindow() is cal

2条回答
  •  无人共我
    2021-01-22 14:02

    Use CreateProcess. I had a same need for creating another process with the position.

    I have defined the PInvoke like below.

    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);
    }
    

    And use it like below.

    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);
    

    Hope this helps.

提交回复
热议问题