如何捕获';发送';在C#中使用UI自动化的Outlook按钮事件?
我想使用UI自动化捕获outlook的“发送”按钮事件。 现在,我能够获得“焦点更改事件”,比如每当我最小化或最大化WINWORD窗口时,事件就会被引发,而不是我希望在单击发送按钮时获得事件如何捕获';发送';在C#中使用UI自动化的Outlook按钮事件?,c#,c#-4.0,outlook,ui-automation,microsoft-ui-automation,C#,C# 4.0,Outlook,Ui Automation,Microsoft Ui Automation,我想使用UI自动化捕获outlook的“发送”按钮事件。 现在,我能够获得“焦点更改事件”,比如每当我最小化或最大化WINWORD窗口时,事件就会被引发,而不是我希望在单击发送按钮时获得事件 private void SendButtonInvoke() { Process[] processes = Process.GetProcessesByName("WINWORD"); AutomationElement aeOutLook = null;
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.");
}
您必须为涉及的UIA模式指定EventHandler。(对于您的情况,很可能是InvokePattern):
您必须为涉及的UIA模式指定EventHandler。(对于您的情况,很可能是InvokePattern):
我编写并测试了下面的代码,它似乎适合我
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");
}
我应该注意,我使用的是.NET4.0自动化libs。我发现老的并不总是按照我想要的方式工作。我还使用Outlook 2013对此进行了测试,测试时Outlook和新的电子邮件消息都已打开。它无法处理等待它们出现的问题
请注意,这些事件并不总是适用于所有控件。某些自定义控件是这样创建的:调用事件不会以事件可以注册的方式报告给UI。话虽如此,根据我的测试,您应该能够在发送按钮上使用此方法
调用与鼠标单击:只是为了添加更多细节,标准控件在用户单击时引发调用事件。“Invoke”只是在可单击控件上触发的标准事件。只有在开发人员决定以某种方式拦截单击并将其重定向到其他位置时,单击才会触发相同的调用。当人们在那里构建自己的自定义控件时,我经常看到这一点
如果您不确定控件是否使用/触发invoke事件,则可以在单击控件时使用可访问事件观察程序来观察该控件。您可以在这里获得有关该工具的更多信息:我编写并测试了下面的代码,它似乎适合我
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");
}
我应该注意,我使用的是.NET4.0自动化libs。我发现老的并不总是按照我想要的方式工作。我还使用Outlook 2013对此进行了测试,测试时Outlook和新的电子邮件消息都已打开。它无法处理等待它们出现的问题
请注意,这些事件并不总是适用于所有控件。某些自定义控件是这样创建的:调用事件不会以事件可以注册的方式报告给UI。话虽如此,根据我的测试,您应该能够在发送按钮上使用此方法
调用与鼠标单击:只是为了添加更多细节,标准控件在用户单击时引发调用事件。“Invoke”只是在可单击控件上触发的标准事件。只有在开发人员决定以某种方式拦截单击并将其重定向到其他位置时,单击才会触发相同的调用。当人们在那里构建自己的自定义控件时,我经常看到这一点
如果您不确定控件是否使用/触发invoke事件,则可以在单击控件时使用可访问事件观察程序来观察该控件。您可以在此处获得有关该工具的更多信息:您是“调用”(通过模式)还是通过鼠标直接单击按钮?这可能会有所不同。通过鼠标直接点击按钮。在您的情况下,不可能坚持纯UI自动化。您将需要使用windows鼠标挂钩。你可以试试这个:看起来很有希望。另外,这里有一些问题已经涉及到您的主题:希望这有帮助;)当然我来看看。谢谢你你是“调用”(通过模式)还是通过鼠标直接点击按钮?这可能会有所不同。通过鼠标直接点击按钮。在您的情况下,不可能坚持纯UI自动化。您将需要使用windows鼠标挂钩。你可以试试这个:看起来很有希望。另外,这里有一些问题已经涉及到您的主题:希望这有帮助;)当然我会查出来的。谢谢。我已经试过这个代码,但在我的“发送”按钮上单击它不起作用。它可以找到“发送”按钮和Outlook邮件窗口,但在“发送”按钮上单击“我的处理程序不工作”。此外,请让我知道是否有一种方法,我可以捕捉到一个“对话框按钮点击”的事件我尝试了这个代码,但在我的发送按钮点击它不工作。它可以找到“发送”按钮和Outlook邮件窗口,但在“发送”按钮上单击“我的处理程序不工作”。另外,请告诉我是否有办法捕捉“对话框按钮点击”事件