Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I find the position / location of a window given a hWnd without NativeMethods?
    primarykey
    data
    text
    <p>I'm currently working with WatiN, and finding it to be a great web browsing automation tool. However, as of the last release, it's screen capturing functionality seems to be lacking. I've come up with a workable solution for capturing screenshots from the screen (independently generating code similar to <a href="https://stackoverflow.com/questions/922546/c-screen-capture-virtual-desktop">this StackOverflow question</a>) in addition to some <a href="http://www.charlespetzold.com/pwcs/ReadingPixelsFromTheScreen.html" rel="nofollow noreferrer">code by Charles Petzold</a>. Unfortunately, there is a missing component: <em>Where is the actual window</em>?</p> <p>WatiN conveniently provides the browser's <code>hWnd</code> to you, so we can (with this simplified example) get set to copy an image from the screen, like so:</p> <pre><code>// browser is either an WatiN.Core.IE or a WatiN.Core.FireFox... IntPtr hWnd = browser.hWnd; string filename = "my_file.bmp"; using (Graphics browser = Graphics.FromHwnd(browser.hWnd) ) using (Bitmap screenshot = new Bitmap((int)browser.VisibleClipBounds.Width, (int)browser.VisibleClipBounds.Height, browser)) using (Graphics screenGraphics = Graphics.FromImage(screenshot)) { int hWndX = 0; // Upper left of graphics? Nope, int hWndY = 0; // this is upper left of the entire desktop! screenGraphics.CopyFromScreen(hWndX, hWndY, 0, 0, new Size((int)browser.VisibileClipBounds.Width, (int)browser.VisibileClipBounds.Height)); screenshot.Save(filename, ImageFormat.Bmp); } </code></pre> <p>Success! We get screenshots, but there's that problem: <code>hWndX</code> and <code>hWndY</code> always point to the upper left most corner of the screen, not the location of the window we want to copy from. </p> <p>I then looked into <code>Control.FromHandle</code>, however this seems to only work with forms you created; this method returns a null pointer if you pass the <code>hWnd</code> into it.</p> <p>Then, further reading lead me to switch my search criteria...I had been searching for 'location of window' when most people really want the 'position' of the window. This lead to <a href="https://stackoverflow.com/questions/1364440/how-to-get-and-set-window-position-of-another-application-in-c">another SO question</a> that talked about this, but their answer was to use native methods.</p> <p>So, <strong>Is there a native C# way of finding the position of a window, only given the hWnd (preferably with only .NET 2.0 era libraries)?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload