cheng_sh Posted May 29, 2003 Posted May 29, 2003 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 Quote
AndreRyan Posted May 29, 2003 Posted May 29, 2003 Why are you doing this? Use Me.ClientSize Quote .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* divil Posted May 29, 2003 *Gurus* Posted May 29, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
cheng_sh Posted May 29, 2003 Author Posted May 29, 2003 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. Quote
*Gurus* divil Posted May 29, 2003 *Gurus* Posted May 29, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
cheng_sh Posted May 29, 2003 Author Posted May 29, 2003 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. Quote
Administrators PlausiblyDamp Posted May 29, 2003 Administrators Posted May 29, 2003 Out of curiosity what kind of numbers where the 'funny' ones? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cheng_sh Posted May 29, 2003 Author Posted May 29, 2003 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. Quote
*Gurus* divil Posted May 29, 2003 *Gurus* Posted May 29, 2003 Can you post the whole code, including all your declares. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
cheng_sh Posted May 30, 2003 Author Posted May 30, 2003 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 Quote
*Gurus* divil Posted May 30, 2003 *Gurus* Posted May 30, 2003 You haven't used the code I posted. Your rect structure has longs in it instead of integers which it should have. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders dynamic_sysop Posted May 30, 2003 Leaders Posted May 30, 2003 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.:-\ Quote
*Gurus* divil Posted May 30, 2003 *Gurus* Posted May 30, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts