Results 1 to 3 of 3

Thread: Getting a window's coordinates

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    37

    Getting a window's coordinates

    I am trying to use GetWindowRect or GetWindowPlacement to find the coordinates of a window that I have the handle for, but every time I try to use GetWindowRect, I get this error:
    Code:
    An unhandled exception of type 'System.NullReferenceException' occurred in testapplication.exe
    
    Additional information: Object reference not set to an instance of an object.
    Here is the code I am using:

    VB Code:
    1. Dim r As New RECT
    2.     GetWindowRect(hwnd, r)
    3.     MsgBox(r.Left)

    hwnd is the window handle, the API calls are all declared public in a module. Here is my converted RECT structure:

    VB Code:
    1. Public Structure RECT
    2.         Dim Left As Integer
    3.         Dim Top As Integer
    4.         Dim Right As Integer
    5.         Dim Bottom As Integer
    6.     End Structure

    Anyone see why it would give me a null exception error? I know for sure that the window handle is valid because I can use SetForegroundWindow on the same variable and it brings the application to the front of the screen.

    Thanks for any help/ideas you can give.
    0000000100000010000000000000010100001001 reincarnated.

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    37

    Re: Getting a window's coordinates

    Nevermind. To make this work, change the ByVal to ByRef in the API declaration:
    VB Code:
    1. Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer
    0000000100000010000000000000010100001001 reincarnated.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Getting a window's coordinates

    Stuff that goes in = ByVal
    Stuff that comes out = ByRef.


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