Well, there is a common confusion for many who are working with WPF objects. Is 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 app, 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 HANDLE
s. 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
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
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.
HMENU
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 it communicate with window handle? Yes, this question comes into the mind of every programmer who starts working with WPF after having considerable amount of exposure in the 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</span /> ListBox();
HwndSource source = (HwndSource)HwndSource.FromVisual(lst);
IntPtr</span /> 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</span />);
IntPtr</span /> hWnd = windowHwnd.Handle;
So basically the WindowInteropHelper
will give you the HANDLE
for the outside window.
So does it mean every WPF object has a HANDLE
associated with it? Actually, the case is something different than what looks like. WPF window is made up of two parts.
- Window area which is made up of Operating System Window
- 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</span /> ListBox();
HwndSource lstHwnd = HwndSource.FromVisual(lst) as</span /> HwndSource;
WindowInteropHelper windowHwnd =new WindowInteropHelper(this</span />);
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 correspond to its own Handle whatever.
Thank you for reading.