Win32 Handle (HWND) & WPF Objects - A Note

Well, there is a common confusion for many who are working with WPF objects, is does it truly interoperable with Windows environment, or more precisely, does it act the same way as it does with normal Win32 objects?
The problem here comes with the fact that each WPF window has only one window handle (HWND) and each other controls are actually placed as content to the window and does not have any entry on Kernel table(no HWND) except of course Popup class in WPF. In this post I will try to cover the basis of HWND (for those of you who don't know) and later go on with what are the changes of it with WPF environment.

If you know what HWND is or how it works, please skip the next section.

Overview of Win32 HANDLE

Win32 Handle is very important for any Win32 applications. For every Win32 apps, Kernel maintains a Table which has entries to identify a memory address. A HANDLE is actually a DWORD (32 bit integer) which maps the memory address on the table. So if you get a HANDLE, you can easily locate the memory address it points to.  Each object that you have in Windows (like windows, buttons, mouse pointers, icon, menu, bitmap etc) has entries in the table and the object is traced using both internally by windows or by programs using those HANDLEs. Even though it is just an unsigned integer value, you should not edit the value, otherwise the HANDLE could not be used to point the object anymore.

Based on the Type of HANDLE can be categorized into HWND, HINSTANCE, HMENU.


HWND  is a special HANDLE which points to a window object. HWND is said to be a pointer to a Window. To get any Window, its Child or Dialog box object, we need to use an HWND object. Communication between two windows is also done using HWND's.


HINSTANCE is a special HANDLE which points to a program instance. Kernel keeps a HANDLE to the instance of the program so that later on the program could be communicated from outside.


For every dropdown menu,  a HMENU handle will be associated with it. HMENU handle is used to alter content of the dropdown menu item.

WPF Objects and HWND

WPF objects are not HWND based. Then how does you it communicate with window handle ? Yes this question come into mind of every programmer who starts working with WPF after having considerable amount of exposure in Windows environment.

First of all, OS cannot render any object without having a reference to HWND to its kernel. Hence is the case with WPF. So WPF window actually holds a reference to one window handle. Even you can get Window Handle for any Visual inside the WPF window.

ListBox lst = new ListBox();
HwndSource source = (HwndSource)HwndSource.FromVisual(lst);
IntPtr hWnd = source.Handle;

So this is a very basic step to get HWND data for any visual. Similarly, for a Window you can get the Window Handle using :

WindowInteropHelper windowHwnd =new WindowInteropHelper(this); 
IntPtr hWnd = windowHwnd.Handle;

So basically the WindowInteropHelper will give you the HANDLE for the outside window.

So does it mean every WPF object have a HANDLE associated with it?
Actually, the case is something different than what looks like. WPF window is made up of two parts.
  1. Window area which is made up of Operating System Window
  2. Non - Window area which is inside a WPF window
Now, a WPF window being a ContentControl holds everything as its Content.  So you can say every pixel of the Content inside the Window class is held by the Outside window. Every Visual of WPF does not have its own HANDLE associated with it, rather it is a content for the outside window element.   To Examine :

ListBox lst = new ListBox();
HwndSource lstHwnd = HwndSource.FromVisual(lst) as HwndSource;
WindowInteropHelper windowHwnd =new WindowInteropHelper(this);

Debug.Assert(lstHwnd.Handle == windowHwnd.Handle);

The assertion outputs true. Hence you can say, every WPF control is owned by its parent window.

Except popup class every control is drawn over the screen using Vector Graphics and hence does not corresponds to its own Handle whatever.

Thank you for reading.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice


Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!