Results 1 to 8 of 8

Thread: [RESOLVED] Detecting Window Bounds of a Program?

  1. #1

    Thread Starter
    Member Clemeit's Avatar
    Join Date
    Sep 2012
    Posts
    32

    Resolved [RESOLVED] Detecting Window Bounds of a Program?

    So I can't find anything like this anywhere and was beginning to wonder if it was even possible.

    Similar to the way many screen recording programs allow you to select the window you would like to record, I need a program to find the bounds of say a NotePad program (or any other program) that I mouse over or select some other way. So I mouse over NotePad (or select it from a list of opened programs) and the program will highlight NotePad's bounds.

    Sorry for the poor explanation, hope it made sense.

    Is it possible? How?
    Last edited by Clemeit; Jan 21st, 2013 at 03:55 PM.

  2. #2
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Detecting Window Bounds of a Program?

    well from an old VB6 way I used to use Findwindow then you can get the windowrect

    http://www.pinvoke.net/default.aspx/user32.findwindow
    Window Rect
    http://www.pinvoke.net/default.aspx/....getwindowrect

    hope that gets you started,

  3. #3

    Thread Starter
    Member Clemeit's Avatar
    Join Date
    Sep 2012
    Posts
    32

    Re: Detecting Window Bounds of a Program?

    Is this the only way? I'm not super keen on using code that I don't recognize, still kinda new to the advanced features of VB. And from what I saw, those only show how to set it up, but didn't give examples on how to actually use it.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Detecting Window Bounds of a Program?

    If you want to do anything with programs that are not under the control of your program then, yes, it's the only way. It's not advanced features of VB but basic nuts and bolts Windows operating system code. I'm still not clear in my head what it is that you actually want to do. If a window is active why does it need highlighting? Or were you going for framing a hidden application (even more complicated!)?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Member Clemeit's Avatar
    Join Date
    Sep 2012
    Posts
    32

    Re: Detecting Window Bounds of a Program?

    Quote Originally Posted by dunfiddlin View Post
    I'm still not clear in my head what it is that you actually want to do. If a window is active why does it need highlighting? Or were you going for framing a hidden application (even more complicated!)?
    Ok so basically I want to be able to put a frame around an open program. This would however require the programs position on the screen, and its height and width. I don't know how to get a programs location or height and width with visual basic.

    More specifically, I would get the programs height / width / location on the screen, then use an automated mouse movement/clicker and automated SendKeys program to run a series of pre-programmed operations. This is for a gaming bot I made a while back, however rather than manually selecting the program windows width height and location on the screen, I wanted to know if it could be done automatically.

  6. #6

    Thread Starter
    Member Clemeit's Avatar
    Join Date
    Sep 2012
    Posts
    32

    Re: Detecting Window Bounds of a Program?

    So I guess I'm having a little more luck.
    I found a way to retrieve information about the process using the code I used for a 'Task Manager' program I made a while back:
    Code:
    For Each p As Process In Process.GetProcesses
           Try
                  If p.MainWindowTitle = TextBox1.Text Then                                 'TEXTBOX1 IS THE WINDOW NAME OF THE PROGRAM I SELECTED
                         MsgBox(p.ProcessName & ": " & p.StartInfo.WindowStyle)
                  End If
           Catch ex As Exception
           End Try
    Next
    So this code returns in a message box the WindowStyle of the program selected, however I have found nothing to do with p.StartInfo.WindowSize or something along those lines.

    Anyone else use Process.GetProcesses before and know anything more about getting information of a program?

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Detecting Window Bounds of a Program?

    I'm not aware of any managed calls you can make to get the size of the external window, I might be wrong but I think you must go the PInvoke way.
    Code:
      Private Declare Function GetWindowRect Lib "User32.dll" (handle As Integer, ByRef rect As RECT) As Integer
    
      <System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)>
      Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
      End Structure
    Once you have the above declared you can use the process class in a similar fashion you showed above.
    Code:
        For Each p As Process In Process.GetProcesses
          If p.MainWindowTitle = "Untitled - Notepad" Then
            Dim r As RECT
            GetWindowRect(p.MainWindowHandle.ToInt32, r)
            MessageBox.Show(String.Format("Notepad at position {0}, {1} Size: {2}x{3}", r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top))
          End If
        Next
    You should obviously add some error handling but this is the bare bone of it.

  8. #8

    Thread Starter
    Member Clemeit's Avatar
    Join Date
    Sep 2012
    Posts
    32

    Re: Detecting Window Bounds of a Program?

    Wow that did it! HUGE thanks.

    Been trying to do something like this for a very long time, guess it was right under my nose.

    Thanks a ton to everyone for the help!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width