{"id":1743,"date":"2014-02-17T07:00:00","date_gmt":"2014-02-17T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/02\/17\/writing-automation-to-wait-for-a-window-to-be-created-and-dismiss-it\/"},"modified":"2014-02-17T07:00:00","modified_gmt":"2014-02-17T07:00:00","slug":"writing-automation-to-wait-for-a-window-to-be-created-and-dismiss-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140217-00\/?p=1743","title":{"rendered":"Writing automation to wait for a window to be created (and dismiss it)"},"content":{"rendered":"<p>\nToday&#8217;s Little Program uses UI Automation to cancel the Run dialog\nwhenever it appears.\nWhy?\nWell, it&#8217;s not really useful in and of itself,\nbut it at least provides an example of using UI Automation\nto wait for an event to occur and then respond to it.\n<\/p>\n<pre>\nusing System.Windows.Automation;\nclass Program\n{\n [System.STAThread]\n public static void Main(string[] args)\n {\n  Automation.AddAutomationEventHandler(\n   WindowPattern.WindowOpenedEvent,\n   AutomationElement.RootElement,\n   TreeScope.Children,\n   (sender, e) =&gt; {\n    var element = sender as AutomationElement;\n    if (element.Current.Name != \"Run\") return;\n    var cancelButton = element.FindFirst(TreeScope.Children,\n     new PropertyCondition(AutomationElement.AutomationIdProperty, \"2\"));\n    if (cancelButton != null) {\n     var invokePattern = cancelButton.GetCurrentPattern(InvokePattern.Pattern)\n                         as InvokePattern;\n     invokePattern.Invoke();\n     System.Console.WriteLine(\"Run dialog canceled!\");\n    }\n   });\n  System.Console.ReadLine();\n  Automation.RemoveAllEventHandlers();\n }\n}\n<\/pre>\n<p>\nOkay, let&#8217;s see what&#8217;s going on here.\n<\/p>\n<p>\nThe program registers a delegate with UI automation\nwhich is called for any <code>Window&shy;Opened<\/code>\nevent\nthat is an immediate child (<code>Tree&shy;Scope.Children<\/code>)\nof the root (<code>Automation&shy;Element.Root&shy;Element<\/code>).\nThis will catch changes to top-level unowned windows,\nbut not bother firing for changes that occur inside\ntop-level windows or for owned windows.\n<\/p>\n<p>\nInside our handler,\nwe check if the window&#8217;s title is\n<i>Run<\/i>.\nIf not, then we ignore the event.\n(This will get faked out by any other window that calls itself <i>Run<\/i>.)\n<\/p>\n<p>\nOnce we think we have a <i>Run<\/i> dialog,\nwe look for the Cancel button,\nwhich we have determined by using UI Spy to have the automation ID\n<code>\"2\"<\/code>.\n(That this is the numeric value of\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms645505(v=vs.85).aspx\">\n<code>IDCANCEL<\/code><\/a>\nis hardly a coincidence.)\n<\/p>\n<p>\nIf we find the Cancel button, we obtain its Invoke pattern so we can\nInvoke it,\nwhich for buttons means pressing it.\n<\/p>\n<p>\nTake this program out for a spin.\nRun the program and then hit <kbd>Win<\/kbd>+<kbd>R<\/kbd>\nto open the Run dialog.\nOops, the program cancels it!\n<\/p>\n<p>\nHa-ha!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program uses UI Automation to cancel the Run dialog whenever it appears. Why? Well, it&#8217;s not really useful in and of itself, but it at least provides an example of using UI Automation to wait for an event to occur and then respond to it. using System.Windows.Automation; class Program { [System.STAThread] public static [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-1743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program uses UI Automation to cancel the Run dialog whenever it appears. Why? Well, it&#8217;s not really useful in and of itself, but it at least provides an example of using UI Automation to wait for an event to occur and then respond to it. using System.Windows.Automation; class Program { [System.STAThread] public static [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=1743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}