我有一个要求,如果用户从他/她的硬盘驱动器打开任何办公文档,它应该打开一个 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);
}
}
如果您在OnUIAEvent
我Process.Start
用来打开 exe 的事件处理程序中看到。它按预期工作。但我希望 exe 应该以模式打开到打开的文档。下面的代码是 exe 的表单加载。
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.CenterToScreen();
}
是否可以打开 Windows 应用程序以打开已打开文档的模式?