{"id":1683,"date":"2014-02-24T07:00:00","date_gmt":"2014-02-24T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/02\/24\/logging-the-contents-of-every-message-box-dialog-via-automation\/"},"modified":"2014-02-24T07:00:00","modified_gmt":"2014-02-24T07:00:00","slug":"logging-the-contents-of-every-message-box-dialog-via-automation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140224-00\/?p=1683","title":{"rendered":"Logging the contents of every message box dialog via automation"},"content":{"rendered":"<p>\nToday&#8217;s Little Program logs the contents of every message box dialog,\nor anything that vaguely resembles a message box dialog.\n(Since there&#8217;s no way for sure to know whether a dialog box is\na message box or not.)\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.<font COLOR=\"blue\">Descendants<\/font>,\n   (sender, e) =&gt; {\n    var element = sender as AutomationElement;\n    <font COLOR=\"blue\">if (element.GetCurrentPropertyValue(\n     AutomationElement.ClassNameProperty) as string != \"#32770\") {\n     return;\n    }\n    var text = element.FindFirst(TreeScope.Children,\n     new PropertyCondition(AutomationElement.AutomationIdProperty, \"65535\"));\n    if (text != null) {\n     System.Console.WriteLine(text.Current.Name);\n    }<\/font>\n   });\n  System.Console.ReadLine();\n  Automation.RemoveAllEventHandlers();\n }\n}\n<\/pre>\n<p>\nThis is the same pattern as the program we wrote last week,\nbut with different guts when the window opens.\n<\/p>\n<p>\nThis time, we see if the class name is\n<code>#32770<\/code>,\nwhich UI Spy tells us is the class name for dialog boxes.\n(That this is the numerical value of\n<code>WC_DIALOG<\/code> is no coincidence.)\n<\/p>\n<p>\nIf we have a dialog,\nthen we look for a child element whose automation ID\nis <code>65535<\/code>,\nwhich UI Spy tells us is the automation ID for the text\ninside a message box dialog.\n(That\nthe traditional control ID for static controls is <code>-1<\/code>\nand 65535 is the\nthe numerical value of\n<code>(WORD)-1<\/code>,\n is no coincidence.)\n<\/p>\n<p>\nIf so, then we print the text.\n<\/p>\n<p>\nIf we were cleverer, we could also confirm that the only buttons\nare OK, Cancel, and so on.\nOtherwise, we can get faked out by other dialog boxes that contain\nstatic text.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program logs the contents of every message box dialog, or anything that vaguely resembles a message box dialog. (Since there&#8217;s no way for sure to know whether a dialog box is a message box or not.) using System.Windows.Automation; class Program { [System.STAThread] public static void Main(string[] args) { Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Descendants, (sender, [&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-1683","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 logs the contents of every message box dialog, or anything that vaguely resembles a message box dialog. (Since there&#8217;s no way for sure to know whether a dialog box is a message box or not.) using System.Windows.Automation; class Program { [System.STAThread] public static void Main(string[] args) { Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Descendants, (sender, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1683","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=1683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1683\/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=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}