GetWindowRect Error

cheng_sh

Active member
Joined
Feb 18, 2003
Messages
25
Location
KL
I am learning on how to use API function: GetWindowRect to get the location and size of an external program. But the following error occur when I execute it.

Can anybody kindly tell me why this error occur and how to solve it?

Ive simplified my code (see code below) and test on a normal form instead of getting the location and size of an external program. When u execute it, you should get the same error as below.


Thank you.

Error:
An unhandled exception of type System.NullReferenceException occurred in MeasureLink.exe

Additional information: Object reference not set to an instance of an object.



Code:
Public Declare Sub GetWindowRect Lib "user32" (ByVal hWnd As IntPtr, ByVal lpRect As RECT)
  Public Structure RECT
    Public Left As Long
    Public Top As Long
    Public Right As Long
    Public Bottom As Long
  End Structure

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim m_rect As RECT
    GetWindowRect(Me.Handle, m_rect)

  End Sub
 
I think hes trying to get the window bounds of a window outside his application.

Code:
    Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Integer
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

Those are the correct declares.
 
Thank you very much for your help.
It is new to me. I never come across with this kind of Tag before.

If you dont mind, can you pls kindly explain to me when should I add this type of Tag.

Thank you very much for your help.
 
The StructLayoutAttribute class lets the framework know how to lay your structure out in memory when its marshaled to an unmanaged application. The vast majority of times you can just specify Sequential, but some people prefer to enter the offsets manually.

You also had to change the ByVal to ByRef, because you want to pass a reference to your existing structure instead of just a copy.
 
I test your code just now. Although there is no error occur, but I cant get the size of the form.

The m_rect.Left, m_rect.Right, m_rect.Bottom and m_rect.Top return me some funny number and variable. So, I dont think it represent the size/location of the form.

Thank you for your reply.
 
I declare m_rect as RECT

<StructLayout(LayoutKind.Sequential)> _
Public Structure Rect
Public left As Long
Public top As Long
Public right As Long
Public bottom As Long
End Structure Rect

Dim m_rect As Rect


When I check its property left, top, right and bottom,
m_rect.left and m_rect.Top always return me big and random number. See example below.

m_rect.left = 1855425872304
m_rect.Top = 566935683204

While m_rect.right and m_rect.bottom always return me 0 value

m_rect.right = 0
m_rect.Top = 0

The form that I create has size width = 300 and Height = 300. I dont know what kind of unit RECT return to me or How can I convert it to get the size of the form.

Thank you for your reply.
 
Sorry for the delay. I create a form called frmMain with size (300,300). During Form load, I check the location and position of the form.


Thank you for your reply.



Code:
Imports System.Runtime.InteropServices
Public Class frmMain
  Inherits System.Windows.Forms.Form
  Public Declare Sub GetWindowRect Lib "user32" (ByVal hWnd As IntPtr, ByRef lpRect As RECT)
  <StructLayout(LayoutKind.Sequential)> _
  Public Structure RECT
    Public Left As Long
    Public Top As Long
    Public Right As Long
    Public Bottom As Long
  End Structure

 Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim m_rect As RECT
    Dim m_size As Size
    GetWindowRect(Me.Handle, m_rect)
    m_size = Me.ClientSize()
  End Sub
 
You havent used the code I posted. Your rect structure has longs in it instead of integers which it should have.
 
i put a code together , using the correct method
eg:
Code:
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
/// and the correct method of GetWindowRect
it returned all zeros , yet when i use the method in vb6 it works fine, so im not sure if just changing to Integers will help cheng_sh out or not Divil.:-\
 
The last code he posted works fine, if you just replace the Longs with Integers in the RECT structure. I tested it before I posted before, and I just tested it again.

Its possible in the form_load event that the forms window handle has not yet been created. It would be a good idea to make sure the form is visible before attempting to get its size and position, but Ive had no problems.
 
Back
Top