Opening an exe(windows app) as modal while opening an office document from hard drive

417 views Asked by At

I have an requirement if the user open any office document from his/her hard drive it's should open an an exe(win form application) as a modal window to capture details about the document.

For that I have developed an console app which runs under the client machine, to monitor if any office document file is opening or not. Please find the below code

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);
    }
}

if you see in the OnUIAEvent event handler I am using Process.Start to open an exe.It's working as expected. But I want the exe should open as modal to the opened document.The below code is the form load of the exe.

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

Is it possible to open the windows application to open as modal to the opened document?

1

There are 1 answers

0
Antoine On

Unless the external application has been coded taken into account your requirement, it will be difficult. If you have access to the code (which you seem to have), you can include the Form in your console application (see here how to run a winform from console application?).

The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:

using System.Windows.Forms;

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

The important bit is the [STAThread] on your Main() method, required for full COM support.

You can override the Creator, and add parameters matching the document, or whatever you need to log/monitor.