ユーザーが自分のハード ドライブから Office ドキュメントを開く場合、ドキュメントの詳細を取得するためにモーダル ウィンドウとして exe (win フォーム アプリケーション) を開く必要があります。
そのために、オフィス ドキュメント ファイルが開いているかどうかを監視するために、クライアント マシンで実行されるコンソール アプリを開発しました。以下のコードを見つけてください
static void Main(string[] args)
{
var UIAEventHandler = new AutomationEventHandler(OnUIAEvent);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, UIAEventHandler);
Console.ReadLine();
Automation.RemoveAllEventHandlers();
}
public static void OnUIAEvent(object src, AutomationEventArgs args)
{
AutomationElement element;
try
{
element = src as AutomationElement;
}
catch
{
return;
}
string name = "";
if (element == null)
name = "null";
else
{
name = element.GetCurrentPropertyValue(
AutomationElement.NameProperty) as string;
}
if (name.Length == 0) name = "< NoName >";
string guid = Guid.NewGuid().ToString("N");
string str = string.Format("{0} : {1}", name, args.EventId.Id);
if ((element.Current.ClassName.Equals("XLMAIN", StringComparison.InvariantCultureIgnoreCase) == true && name.Contains(".xlsx")) || (element.Current.ClassName.Equals("OpusApp", StringComparison.InvariantCultureIgnoreCase) == true && name.Contains(".docx")))
{
Process.Start(@"E:\experiment\TestingWindowsService\UserInfomation\bin\Debug\UserInfomation.exe", element.Current.Name);
//Automation.AddAutomationEventHandler(
// WindowPattern.WindowClosedEvent,
// element, TreeScope.Element, (s, e) => UIAEventHandler1(s, e, guid, name));
Console.WriteLine(guid + " : " + name);
// Environment.Exit(1234);
}
}
exeを開くためOnUIAEvent
に使用しているイベントハンドラーに表示されている場合は、期待どおりに機能しています。Process.Start
しかし、私はexeが開いたドキュメントのモーダルとして開く必要があることを望んでいます。以下のコードは、exeのフォームロードです。
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.CenterToScreen();
}
Windows アプリケーションを開いて、開いているドキュメントのモーダルとして開くことはできますか?