The magic of NativeWindow-- How does .Net Winform manage Win32 controls
2006-08-12 22:36
375 查看
1. Every .Net winform control inherits from System.Windows.Forms.Control class. In its constructor, you will see this key statement:
public Control()
This statement will assign the control's reference to System.Windows.Forms.Control.ControlNativeWindow class.
ControlNativeWindow inherits from NativeWindow class and is designed to manage the messages of the assigned control.
2. Whenever a handle is needed for that control, Control.CreateControl method is called. And it internally will invoke its associated ControlNativeWindow.CreateHandle method with prepared CreateParams as parameter.
ControlNativeWindow.CreateHandle method first invokes System.Windows.Forms.NativeWindow.WindowClass.Create static method, which internally calls RegisterClass win32 API to register the native control class. The key point here is that the WindowClass.Callback method is stored in WNDCLASS structure as the default WndProc of registed class. Then it invokes CreateWindowEx win32 API to create the underlying win32 control.
3. When the WindowClass.Callback is called the first time, it will immediately call SetWindowLong to native DefWindowProc and then call its parent NativeWindow.AssignHandle method to start the subclass again.(NativeWindow.AssignHandle again first stores the DefWindowProc address and invokes SetWindowLong to set native control's wndproc to NativeMethods.Callback or NativeWindow.DebuggableCallback). Finally, it calls NativeMethods.Callback to handle this message.
4. NativeMethods.Callback just calls ControlNativeWindow.WndProc(override NativeWindow.WndProc) method. ControlNativeWindow.WndProc invokes ControlNativeWindow.OnMessage method, which only calls its associated Control's WndProc method. The message control is passed to Control class now!!
5. Control.WndProc acts as a .Net message filter which triggers most of the .Net control events in Winform control model. After the message processing, the message is finally passed to DefWndProc.
public Control()
This statement will assign the control's reference to System.Windows.Forms.Control.ControlNativeWindow class.
ControlNativeWindow inherits from NativeWindow class and is designed to manage the messages of the assigned control.
2. Whenever a handle is needed for that control, Control.CreateControl method is called. And it internally will invoke its associated ControlNativeWindow.CreateHandle method with prepared CreateParams as parameter.
ControlNativeWindow.CreateHandle method first invokes System.Windows.Forms.NativeWindow.WindowClass.Create static method, which internally calls RegisterClass win32 API to register the native control class. The key point here is that the WindowClass.Callback method is stored in WNDCLASS structure as the default WndProc of registed class. Then it invokes CreateWindowEx win32 API to create the underlying win32 control.
3. When the WindowClass.Callback is called the first time, it will immediately call SetWindowLong to native DefWindowProc and then call its parent NativeWindow.AssignHandle method to start the subclass again.(NativeWindow.AssignHandle again first stores the DefWindowProc address and invokes SetWindowLong to set native control's wndproc to NativeMethods.Callback or NativeWindow.DebuggableCallback). Finally, it calls NativeMethods.Callback to handle this message.
4. NativeMethods.Callback just calls ControlNativeWindow.WndProc(override NativeWindow.WndProc) method. ControlNativeWindow.WndProc invokes ControlNativeWindow.OnMessage method, which only calls its associated Control's WndProc method. The message control is passed to Control class now!!
5. Control.WndProc acts as a .Net message filter which triggers most of the .Net control events in Winform control model. After the message processing, the message is finally passed to DefWndProc.
相关文章推荐
- How to make an SDL window the child of another window in Win32
- Code snippet of the day : Resizing ASP.NET Datagrid Controls in Edit mode at runtime
- How to configure the Virtual Path of ASP.NET Development Server to be “Root”
- how to judge the type of the client browser by .net
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- How to read the query string params of a ASP.NET raw URL?
- How to Pop Open an image window that resizes to fit the image in ASP.net 1.x and 2.0 Beta 1
- How to get the visible region of a window or CDC(转自http://www.programmersheaven.com)
- Git 管理多个分支的公共部分 How to manage the common part of multiple branches
- The New Controls of ASP.NET 2.0
- How does the Spring MVC determine the type of a given request parameter
- How to set the width of columns?about chart control for .net
- how to extend the base functionality of standard web controls - by Venugopal Mallarapu
- How to find child controls that are located in the template of a parent control
- How does the Gizmocall work - analysis of the mechanism
- How does the lead number of inverse circular function be verified?
- How to get the DOM of a WebBrowser control from a window handle (VB6)
- How to debug ASP.Net program without the privilege of administrator
- zhuanzai: AJAX: How to create a "Processing" modal window using UpdateProgress and ModalPopup ASP.net AJAX controls
- .NET Framework Solutions: In Search of the Lost Win32 API