我想在我的 C# 应用程序中启动一个进程,我可以使用以下代码来实现:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "chrome.exe";
startInfo.WorkingDirectory = @"C:\Program Files\Google\Chrome\Application";
Process process = Process.Start(startInfo);
但是当我打开它时,它会在整个屏幕上打开,这意味着它覆盖了整个屏幕。我需要将其包含在我的表单范围内。假设表单的大小是 906, 495
那么这个应用程序应该在这个区域内打开。
我找不到我该怎么做。其次,我需要设置任何应用程序的大小。就像我使用过 Chrome,但它可能是任何其他进程。可能吗?
最佳答案
你可以这样做:
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "chrome.exe";
startInfo.WorkingDirectory = @"C:\Program Files\Google\Chrome\Application";
//Force chrome to run in a new process
startInfo.Arguments = @"--user-data-dir=C:\sometempdir";
Process process = Process.Start(startInfo);
process.WaitForInputIdle();
//Need to do a little more work make sure we get the Window handle properly
do
{
System.Threading.Thread.Sleep(100);
process.Refresh();
}
while (process.MainWindowHandle == IntPtr.Zero && !process.HasExited);
//Set these appropriately
int xPos = 0;
int yPos = 0;
MoveWindow(process.MainWindowHandle, xPos, yPos, 906, 495, true);
关于c# - 将流程适配到容器Form中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24334749/