2016-12-17 12 views
0

사용자가 하드 드라이브에서 Office 문서를 열면 exe (winform 응용 프로그램)를 열어야합니다. 모달 창을 사용하여 문서에 대한 세부 정보를 캡처합니다.하드 드라이브에서 Office 문서를 여는 동안 모달로 exe (Windows 응용 프로그램) 열기

나는 Office 문서 파일이 열려 있는지 여부를 모니터링하기 위해 클라이언트 컴퓨터에서 실행되는 콘솔 앱을 개발했습니다. 당신은 내가 예상대로 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]입니다.

작성자를 무시하고 문서와 일치하는 매개 변수를 추가하거나 로그/모니터링해야하는 항목을 추가 할 수 있습니다.