c# - 将流程适配到容器Form中

标签 c# process

我想在我的 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/

相关文章:

c# - 为什么.Net 框架不使用 Guard 类(或等效类)作为方法参数

c# - 如何在 WPF 中获取 "Selected MenuItem"

c# - 无法在 MVC 项目 Visual Studio 2015 中打开数据库

php - php5 中的代码入门

c - 多次从管道读取

java - 用于不同机器中进程之间通信的快速而肮脏的解决方案

C# WPF 设置焦点 TabItem

c# - 在 emptydata 模板 gridview 中使用客户端脚本代码

c# - 使用环境变量启动进程

c++ - 替换我的 C++ 程序中对 "system"的调用