如何在C#中使用UI自动化捕获Outlook的“发送"按钮事件? [英] How to Capture 'Send' button event for Outlook using UI Automation in C#?
问题描述
我想使用UI自动化捕获Outlook的发送"按钮事件. 现在,我能够获得焦点更改事件",就像每当iam最小化或最大化WINWORD窗口时,都会引发该事件,而不是我希望在单击发送"按钮时获得该事件.
I want to capture 'Send' button event of outlook using UI Automation. Right now i am able to get 'Focus Change Event' like whenever iam minimizing or maximizing the WINWORD window the the event is raised instead of that i want to get the event on Send button click.
private void SendButtonInvoke()
{
Process[] processes = Process.GetProcessesByName("WINWORD");
AutomationElement aeOutLook = null;
foreach (var item in processes)
{
aeOutLook = AutomationElement.FromHandle(item.MainWindowHandle);
}
//AutomationElement outlookelm = AutomationElement.FromHandle(processName.MainWindowHandle);
AutomationElement buttonAddInstance = aeOutLook.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Send"));
if (buttonAddInstance == null)
{
MessageBox.Show("Add button instance not found");
}
else
{
AutomationPropertyChangedEventHandler ButtonEvent =
new AutomationPropertyChangedEventHandler(ButtonChecked_EventHandler);
//Attaching the EventHandler
Automation.AddAutomationPropertyChangedEventHandler(buttonAddInstance, TreeScope.Children,
ButtonEvent, AutomationElement.NameProperty);
}
}
private void ButtonChecked_EventHandler(object sender, AutomationEventArgs e)
{
AutomationElement ar = sender as AutomationElement;
MessageBox.Show("Button Clicked Sucessfully.");
}
推荐答案
我编写并测试了以下代码,它似乎对我有用.
I wrote and tested the code below and it seems to work for me.
private void AddEmailSendEvent()
{
// Find the new email window
PropertyCondition newEmailWindowCondition = new PropertyCondition(AutomationElement.NameProperty, "Untitled - Message (HTML) ");
AutomationElement NewEmailWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, newEmailWindowCondition);
// Find the Send Button
PropertyCondition sendEmailButtonCondition = new PropertyCondition(AutomationElement.NameProperty, "Send");
AutomationElement sendButton = NewEmailWindow.FindFirst(TreeScope.Descendants, sendEmailButtonCondition);
// If supported, add the invoke event
if (sendButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, sendButton, TreeScope.Element, handler);
}
private void handler(object sender, AutomationEventArgs e)
{
// Do whatever is needed, for testing this just adds a message to my forms Main UI
AddMessage("Invoke event occured");
}
我应该注意,我正在使用.Net 4.0自动化库.我发现年长的人并不总是按照我想要的方式工作.我还使用Outlook 2013对此进行了测试,并且当我对此进行测试时,Outlook和新电子邮件均已打开.等待它们出现并不处理.
I should note that I'm using the .Net 4.0 automation libs. I've found the older ones don't always work the way I want them. I also tested this with Outlook 2013, and both outlook and the new email message were already open when I tested this. It doesn't handle waiting for them to appear.
请注意,这些事件并不总是适用于所有控件.某些自定义控件的制作方式是,调用事件不会以事件可以注册的方式报告给UI.话虽如此,从我的测试中,您应该可以在发送"按钮上使用此方法.
Just so your aware, these events don't always work for all controls. Some custom controls are made in such a way the invoke events are not reported to the UI in a way the event can register. With that said, from my testing you should be able to use this method on the send button.
调用vs鼠标单击:只是添加了更多细节,标准控件导致用户单击时触发invoke事件. 调用"只是在可点击控件上触发的标准事件.单击不会触发相同的调用的唯一时间是,如果开发人员决定以某种方式拦截该单击并将其重定向到其他位置.当人们在那里建立自己的自定义控件时,我已经看到了很多.
Invoking vs mouse clicks: Just to add a little more detail, the standard control causes the invoke event to fire when a user clicks it. "Invoke" is just the standard event fired on clickable controls. The only time a click wouldn't fire the same invoke is if the developer decided to intercept the click somehow and redirect it elsewhere. I've seen this a lot when people build there own custom controls.
如果不确定控件是否使用/触发了invoke事件,则可以在单击控件时使用Accessible Event Watcher来监视该控件.您可以在此处获取有关该工具的更多信息:
If your not sure about whether a control using/firing the invoke event or not you can get use the Accessible Event Watcher to watch a control as you click it. You can get more information on the tool here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd317979(v=vs.85).aspx
这篇关于如何在C#中使用UI自动化捕获Outlook的“发送"按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!