2016-12-17 5 views
0

ユーザがハードドライブからオフィス文書を開くと、exe(winフォームアプリケーション)を開く必要があります。モーダルウィンドウを使用してドキュメントの詳細をキャプチャします。ハードドライブからオフィス文書を開くときにモーダルとしてexe(Windowsアプリ)を開く

私は、オフィス文書ファイルが開いているかどうかを監視するために、クライアントマシンで実行されるコンソールアプリケーションを開発しました。あなたは、私が予想通りexe.Itの作業を開くためにProcess.Startを使用していますOnUIAEventイベントハンドラで見た場合、以下のコード

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が開いているドキュメントのモーダルとして開く必要があります。以下のコードは、EXEのフォームロードです。

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.TopMost = true; 
    this.CenterToScreen(); 
} 

開いている文書に対してモーダルとして開くようにWindowsアプリケーションを開くことはできますか?

+1

UIオートメーションを使用すると、ウィンドウを無効にすることもできます。ユーザーがウィンドウを再び有効にすることはできません。あなたのアプリをモーダルにします。それを再度有効にすることを忘れないでください。 –

答えて

0

あなたの要件を考慮して外部アプリケーションがコード化されていない限り、それは難しいでしょう。 コードにアクセスできる場合は、コンソールアプリケーションにフォームを含めることができます(how to run a winform from console application?を参照)。

最も簡単な選択肢は、Windowsフォームプロジェクトを開始してから をコンソールアプリケーションに変更することです。また、ただのSystem.Windows.Forms.dllへ の参照を追加し、コーディングを開始:

using System.Windows.Forms; 

[STAThread] static void Main() { 
    Application.EnableVisualStyles(); 
    Application.Run(new Form()); // or whatever 
} 

重要なビットがフルCOMをサポートするために必要な、自分のメイン()メソッドに[STAThread]です。

作成者を無効にして、ドキュメントに一致するパラメータを追加するか、ログ/モニタする必要があるものを追加できます。

関連する問題