Click here to Skip to main content
Click here to Skip to main content

Win32 Handle (HWND) & WPF Objects - A Note

, 5 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Win32 Handle (HWND) & WPF Objects - A Note

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

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.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur
Architect
India India
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

Comments and Discussions

 
Generalcommunicate with child objects PinmemberAngelo Munoz6-Dec-10 3:45 
GeneralRe: communicate with child objects PinmvpAbhishek Sur6-Dec-10 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 5 Dec 2010
Article Copyright 2010 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid