Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

 

I've 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.

 

 

 

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

Posted
Why are you doing this? Use Me.ClientSize
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
  • *Gurus*
Posted

I think he's trying to get the window bounds of a window outside his application.

 

   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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Thank you very much for your help.

It is new to me. I never come across with this kind of Tag before.

 

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

 

Thank you very much for your help.

  • *Gurus*
Posted

The StructLayoutAttribute class lets the framework know how to lay your structure out in memory when it's 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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

I test your code just now. Although there is no error occur, but I can't 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 don't think it represent the size/location of the form.

 

Thank you for your reply.

Posted

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 don't 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.

Posted

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.

 

 

 

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

  • Leaders
Posted

i put a code together , using the correct method

eg:

   <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 i'm not sure if just changing to Integers will help cheng_sh out or not Divil.:-\

  • *Gurus*
Posted

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.

 

It's possible in the form_load event that the form's 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 I've had no problems.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...