Introduction to Win32 handle HWND and WPF Objects-Tutorial

No.of Views2225
Bookmarked0 times
Downloads 
Votes0
By  abhi2434   On  10 Dec 2010 07:12:57
Tag : WPF , General
In this tutorial i will explain how WPF object work and Win32 HWND handle as well.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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this tutorial i will explain how WPF object work and Win32 HWND handle as well.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

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 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.

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</