我正在尝试编写一个程序,只需单击一下即可打开多个文档,并为每个单独的文档窗口指定大小和位置。在我尝试打开第二个 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。任何解决此错误的帮助将不胜感激。