Determine the size & position of an external program window

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Determine the size & position of an external program window

    I'm updating a program that I wrote a few months back and I need to be able to determine the size and position of other program windows. I'm sure this is possible, but I just can't figure out how. I've tried Google and all of the results that I came up with were dead ends.

    Basically I just need to see the location in screen coordinates and the size of the external window. How might I accomplish this?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I'm wondering if the GetWindowRect function in User32.DLL can help you.

    Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.


    The only thing I'm unsure about here is how to get the hWnd of the other windows. I've been looking around but if I put you on the same track as I am, I'm sure you'll find it sooner :)

    Here's the thread I found that got me thinking this is how you might do it.
    i've developed a simple application (.dll) in LABVIEW and i implorted that dll to a C# windows application(Winforms) . Like [DllImport(@".\sample.dll")] public static extern void MyFunc(ch...


    I took a quick glance through what's available in User32.DLL but didn't see anything outright, which doesn't mean it isn't there. I'll see if I can come back to this... but hopefully I've given you something to look into.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Ooooh!


      Provides access to local and remote processes and enables you to start and stop local system processes.


      The Process class has a Handle property, so you might be able to get it from there. If you get a chance to try this before I do, let me know how it works! :)

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        My apologies for taking so long to reply. I have managed to whip up some code that gathers a list of all running processes and allows me to get rectangle data for the active window. There are a few issues, for example the X and Y loations are correct, but since I have multiple monitors, for some reason my width and height are being displayed wrong. Each monitor is 1920x1200. I've got a window open that is 1696x1056, located at 2032x94, so my rectangle should be:

        {X=2032, Y=94, Width=1696, Height=1056}

        Intead it s returned as:

        {X=2032, Y=94, Width=3728, Height=1150}

        However, it seems to be as simple as subtracting the X from the Width and the Y from the Height to get the actual size of the window, so all I'd have to do is:

        Code:
        Rectangle rect = new Rectangle();
        GetWindowRect(hWnd, ref rect);
        rect.Width -= rect.X;
        rect.Height -= rect.Y;
        And I'll have the correct coordinates and size of an external window.

        Now I just have to figure out how to detect when I put MY program window over the external one. Hmmm...

        I'm finishing up a screenshot program that I wrote that allows the user to position a rectangle anywhere on the screen, size it however they want, then push a button to take a screenshot of that portion of the screen. It started off as a test of my abilities, but it's grown up quite a bit. What I want to do with what I just learned is allow the user to drag the screenshot rectangle over an external window and allow them to lock the rectangle to that window. Armed with this new knowledge, I think I can do it.

        Comment

        Working...