5

I need to get the exact meaning of IntPtr.Zero in below function. I used this function for convert word document text range position pixel value to WPF unit value.

Graphics g = Graphics.FromHwnd(IntPtr.Zero)

That normally said to pass the handle,but I didn't get it.

Sean
  • 60,939
  • 11
  • 97
  • 136
KIS
  • 129
  • 1
  • 10

2 Answers2

6

Graphics.FromHwnd method is used to create a Graphics object from the specified handler of a window.

When you pass IntPtr.Zero as parameter you are creating a Graphics from the windows desktop that allow you draw directly in the screen.

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
0

IntPtr is a platform specific way to represent a handle or pointer. On 32 bit systems a pointer is 32 bits wide, whilst on a 64 bit system it is 64 bits wide. This type exists to hide these differences from you.

IntPtr.Zero represents a handle or pointer whose values is 0 (essentially null). You can use it in places where the API expects a Windows API style handle, such as in the FromHwnd call in your example. In the Windows API a HWND is a window handle, where null indicates "no window". To pass this via C# you use IntPtr.Zero.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • 1
    *"In the Windows API a HWND is a window handle, where null indicates "no window"."* - There is no such convention in the Windows API. Whenever a `HWND` argument is optional, it carries specific semantics. Since `System.Graphics.Drawing` wraps GDI+, which is implemented in terms of GDI, I would assume, that this retrieves an object representing the entire screen, although this is not specifically documented. – IInspectable May 06 '16 at 15:23