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

后端 未结 2 1151
清歌不尽
清歌不尽 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.

    0 讨论(0)
  • 2021-01-22 14:03

    Look at the ProcessStartInfo.WindowStyle. You should be able to set it to start as Hidden until you've moved and resized it and then set it back to normal.

    See here:

    The hidden window style. A window can be either visible or hidden. The system displays a hidden window by not drawing it. If a window is hidden, it is effectively disabled. A hidden window can process messages from the system or from other windows, but it cannot process input from the user or display output. Frequently, an application may keep a new window hidden while it customizes the window's appearance, and then make the window style Normal. To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false.

    For how to change the style after the process has started, see this question:

    Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime

    0 讨论(0)
提交回复
热议问题