我正在嘗試編寫一個程序,該程序只需單擊一次即可打開多個文檔,併爲每個單獨的文檔窗口指定大小和位置。在我嘗試打開第二個Word或Excel文檔之前,我已經獲得了一個基本程序的體面成功,用於測試開放和定位操作。用Process.Start打開多個文檔()
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);
}
}
}
該程序試圖打開使用記事本2個.txt文件,使用的MSWord 2個的.docx文件,並使用msexcel的2個的.xlsx文件。無論我在程序中打開文檔的順序如何,在打開第二個Word或Excel文件後立即在WaitForInputIdle行上引發InvalidOperationException。任何幫助解決這個錯誤將不勝感激。
如果您不重複使用'resize',即每個都有不同的'Process',是否會出現同樣的問題? – 2013-02-13 17:28:19
爲什麼要在同一個進程(調整大小)中啓動()多個文檔? – Paparazzi 2013-02-13 17:29:05
@奧斯汀 - 是的,我使用1個進程還是6個進程會出現同樣的問題。 – 2013-02-13 17:37:27