<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Win32 Interop</title>
        <link>http://geekswithblogs.net/CalinTatar/category/10091.aspx</link>
        <description>Win32 Interop</description>
        <language>en-US</language>
        <copyright>CalinTatar</copyright>
        <managingEditor>tatar_calin@yahoo.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Switching from Win32 to UI Automation</title>
            <link>http://geekswithblogs.net/CalinTatar/archive/2009/05/16/switching-from-win32-to-ui-automation.aspx</link>
            <description>&lt;p&gt;&lt;font size="2"&gt;If you are about to write another p/invoke (Win32 calls) to access existing Windows, Controls,...you should consider switching to UI Automation framework, which provides programmatic access to most user interface (UI) elements on the desktop, by writing managed code.  Some time ago I wrote an article about UI Automation, have a look at:  &lt;font face="Arial"&gt;&lt;a href="http://www.codeproject.com/KB/WPF/WPF_UI_Automation.aspx"&gt;http://www.codeproject.com/KB/WPF/WPF_UI_Automation.aspx&lt;/a&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;In this post, I'll try to compare two ways of implementing Windows automation applications, by using unmanaged code (Win32 interoperability) and by using the UI Automation framework (managed code).&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;OK, let's take a simple example. Suppose we want to write a piece of code that gets the active window and some details about this window.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;Instead of writing Win32 calls for &lt;strong&gt;GetWindow&lt;/strong&gt;, &lt;font face="Arial"&gt;&lt;strong&gt;GetWindowRect&lt;/strong&gt;, different structures, etc, with UI Automation we can write simple managed code to accomplish the same task.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;Basically, we just need to get &lt;strong&gt;AutomationElement.FocusedElement&lt;/strong&gt; and this will provide us useful informations about the focused control which might be a simple control(Button, TextBox, etc) or a Window. Let's assume that the returned FocusedControl is a Textbox, a not a Window; we still can get the parent Window of that control, by getting first the ProcessId property. Based on the process Id, we can create an instance of Process class and access &lt;strong&gt;process.MainWindowHandle&lt;/strong&gt;, to get the Window handle. Now, based on the Window handle, we can call the static method AutomationElement.FromHandle() and you'll get the AutomationElement for the window. To get the window bounds, just call &lt;strong&gt;windowElement.Current.BoundingRectangle&lt;/strong&gt; and you'll obtain a &lt;strong&gt;Rect&lt;/strong&gt; object. After that you may use the bounds to capture the window, etc.&lt;/font&gt;&lt;/p&gt;
&lt;font size="2"&gt;&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AutomationElement&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; controlElement = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AutomationElement&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.FocusedElement;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (controlElement != &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;null&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &amp;amp;&amp;amp; controlElement.Current.ControlType != &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;ControlType&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Window)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Process&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; process = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Process&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.GetProcessById(controlElement.Current.ProcessId);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AutomationElement&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; windowElement = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AutomationElement&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.FromHandle(process.MainWindowHandle);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (windowElement != &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;null&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Rect&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; r = windowElement.Current.BoundingRectangle;&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;/blockquote&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Another example would be to get the user control based on the cursor position. So we want to find a managed solution for &lt;strong&gt;WindowFromPoint &lt;/strong&gt;Win32 function. We can accomplish this without writing Interop code, by calling and passing a Point to &lt;strong&gt;AutomationElement.FromPoint();&lt;/strong&gt;  &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt; &lt;img src="http://geekswithblogs.net/CalinTatar/aggbug/132151.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>CalinTatar</dc:creator>
            <guid>http://geekswithblogs.net/CalinTatar/archive/2009/05/16/switching-from-win32-to-ui-automation.aspx</guid>
            <pubDate>Sat, 16 May 2009 01:40:03 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/CalinTatar/comments/132151.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/CalinTatar/archive/2009/05/16/switching-from-win32-to-ui-automation.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/CalinTatar/comments/commentRss/132151.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
