C# 在process.start()之前设置进程窗口大小和位置
我正在使用一个表单来启动新的flash进程,使用Process.start()和MoveWindow()来调整和更改进程窗口的位置。问题是在调用MoveWindow()之前,您可以在一瞬间看到默认大小和位置的窗口。我想知道是否有办法在实际进程开始之前设置窗口的位置和大小C# 在process.start()之前设置进程窗口大小和位置,c#,process,C#,Process,我正在使用一个表单来启动新的flash进程,使用Process.start()和MoveWindow()来调整和更改进程窗口的位置。问题是在调用MoveWindow()之前,您可以在一瞬间看到默认大小和位置的窗口。我想知道是否有办法在实际进程开始之前设置窗口的位置和大小 Process flash = new Process(); flash.StartInfo.WindowStyle = ProcessWindowStyle.Normal; flash.StartInfo.FileName =
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);
看这张照片。您应该能够将其设置为开始时为隐藏
,直到移动并调整其大小,然后将其设置回正常状态
见:
隐藏的窗口样式。窗口可以是可见的,也可以是隐藏的。系统通过不绘制隐藏窗口来显示隐藏窗口。如果窗口被隐藏,它将被有效禁用。隐藏窗口可以处理来自系统或其他窗口的消息,但不能处理来自用户的输入或显示输出。通常,应用程序会在自定义窗口外观时隐藏新窗口,然后使窗口样式正常。要使用ProcessWindowsStyle.Hidden,ProcessStartInfo.UseShellExecute属性必须为false
有关如何在流程开始后更改样式,请参见以下问题:
使用。我同样需要为这个职位创建另一个流程
我定义了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);
然后像下面那样使用它
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);
希望这有帮助。可以从某个地方获取初始窗口大小。否则,唯一的选择就是像你那样做。提示:尝试显示最小化窗口并在一起恢复/移动。您的提示部分起作用,我使用2个屏幕,希望在每个屏幕上播放一个flash。使用最小化窗口并将其还原会导致窗口始终显示在主屏幕中。我尝试使用ProcessWindowsStyle.Hidden,但问题是隐藏窗口没有句柄(或实际窗口),因此我无法调整其大小。我尝试使用此示例的变体,但启动的应用程序(记事本)未侦听STARTUPINFO结构中设置的XY尺寸。如果我没有设置dwFlags值,记事本就会正常显示。如果我确实将dwFlags值设置为STARTF_USESHOWWINDOW,记事本将根本不会出现,也不在任务栏中(尽管我能够打印其进程id)。MSDN文档说,应用程序第一次调用CreateWindow时将使用这些尺寸,但记事本忽略了我的尺寸设置。