【发布时间】:2013-02-14 06:24:10
【问题描述】:
我正在尝试编写一个程序,只需单击一下即可打开多个文档,并为每个单独的文档窗口指定大小和位置。在我尝试打开第二个 Word 或 Excel 文档之前,我使用一个基本程序来测试打开和定位操作取得了不错的成功。
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
static void Main(string[] args)
{
Process resize = new Process();
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\TEST1.txt";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 10, 10, 500, 500, true);
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSWTEST1.docx";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 20, 20, 500, 500, true);
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSXTEST1.xlsx";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 30, 30, 500, 500, true);
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\TEST2.txt";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 40, 40, 500, 500, true);
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSWTEST2.docx";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 50, 50, 500, 500, true);
resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSXTEST2.xlsx";
resize.Start();
resize.WaitForInputIdle();
MoveWindow(resize.MainWindowHandle, 60, 60, 500, 500, true);
}
}
}
程序尝试使用记事本打开两个 .txt 文件,使用 MSWord 打开两个 .docx 文件,使用 MSExcel 打开两个 .xlsx 文件。无论我以何种顺序打开程序中的文档,都会在打开第二个 Word 或 Excel 文件后立即在 WaitForInputIdle 行上引发 InvalidOperationException。任何解决此错误的帮助将不胜感激。
【问题讨论】:
-
如果您不重复使用
resize,是否会出现同样的问题,即每个都有不同的Process? -
为什么要在 SAME 进程中 Start() 多个文档(调整大小)?
-
@Austin - 是的,无论我使用 1 个进程还是 6 个进程,都会出现同样的问题。
-
或者你可以通过静态:
Process proc = Process.Start(Execut, args);。这样您就知道您将始终拥有一个新窗口。编辑:没有用办公文件测试它,只有 chrome。