在process.start()之前设置进程窗口大小和位置
本文关键字:进程 设置 窗口大小 位置 process start | 更新日期: 2023-09-27 17:50:56
我使用一个表单来启动新的flash进程,使用process .start()和MoveWindow()来调整大小和更改进程窗口位置。问题是在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
,直到你移动并调整它的大小,然后将其设置回正常。
看到:
隐藏窗口样式。窗口可以是可见的,也可以是隐藏的。系统通过不绘制隐藏窗口来显示隐藏窗口。如果一个窗口被隐藏,它实际上是被禁用的。隐藏窗口可以处理来自系统或其他窗口的消息,但不能处理来自用户的输入或显示输出。通常,应用程序可能会在自定义窗口外观时隐藏新窗口,然后将窗口样式设置为Normal。使用ProcessWindowStyle。隐藏的是ProcessStartInfo。UseShellExecute属性必须为false。
如何在进程开始后更改样式,参见这个问题:
切换Process.StartInfo.WindowStyle = ProcessWindowStyle。运行时隐藏