NativeWindow Class
Provides a low-level encapsulation of a window handle and a window procedure.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration <SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _ <SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _ Public Class NativeWindow _ Inherits MarshalByRefObject _ Implements IWin32Window 'Usage Dim instance As NativeWindow
This class automatically manages window class creation and registration.
A window is not eligible for garbage collection when it is associated with a window handle. To ensure proper garbage collection, handles must either be destroyed manually using DestroyHandle or released using ReleaseHandle.
Note: |
|---|
The ReleaseHandle method is called when the WM_NCDESTROY message is processed. This means there are cases in which when you do not need to manually call ReleaseHandle, but it is good practice to do so. |
The NativeWindow class provides the following properties and methods to manage handles: Handle, CreateHandle, AssignHandle, DestroyHandle, and ReleaseHandle.
The following code example demonstrates intercepting operating system window messages in a window procedure, and creating a window with a specific operating system window class name. The example creates two classes that inherit from NativeWindow that accomplish this.
The MyNativeWindowListener class hooks into the window procedure of the form passed into the constructor, and overrides the WndProc method to intercept the WM_ACTIVATEAPP window message. The class demonstrates the usage of the AssignHandle and ReleaseHandle methods to identify the window handle the NativeWindow will use. The handle is assign based upon the Control.HandleCreated and Control.HandleDestroyed events. When the WM_ACTIVATEAPP window message is received, the class calls the form1 ApplicationActivated method.
The MyNativeWindow class creates a new window with the ClassName set to BUTTON. The class demonstrates using the CreateHandle method and overriding the WndProc method to intercept window messages that are received.
Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.Runtime.InteropServices <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Public Class Form1 Inherits System.Windows.Forms.Form Private nwl As MyNativeWindowListener Private nw As MyNativeWindow Friend Sub ApplicationActivated(ByVal ApplicationActivated As Boolean) ' The application has been activated or deactivated System.Diagnostics.Debug.WriteLine("Application Active = " + ApplicationActivated.ToString()) End Sub Private Sub New() MyBase.New() Me.Size = New System.Drawing.Size(300, 300) Me.Text = "Form1" nwl = New MyNativeWindowListener(Me) nw = New MyNativeWindow(Me) End Sub Public Shared Sub Main() Application.Run(New Form1()) End Sub End Class ' NativeWindow class to listen to operating system messages. Friend Class MyNativeWindowListener Inherits NativeWindow ' Constant value was found in the "windows.h" header file. Private Const WM_ACTIVATEAPP As Integer = &H1C Private parent As Form1 Public Sub New(ByVal parent As Form1) AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed Me.parent = parent End Sub ' Listen for the control's window creation and hook into it. Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs) ' Window is now created, assign handle to NativeWindow. AssignHandle(CType(sender, Form).Handle) End Sub Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs) ' Window was destroyed, release hook. ReleaseHandle() End Sub <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Sub WndProc(ByRef m As Message) ' Listen for operating system messages Select Case (m.Msg) Case WM_ACTIVATEAPP ' Notify the form that this message was received. ' Application is activated or deactivated, ' based upon the WParam parameter. parent.ApplicationActivated(m.WParam.ToInt32() <> 0) End Select MyBase.WndProc(m) End Sub End Class ' MyNativeWindow class to create a window given a class name. <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Friend Class MyNativeWindow Inherits NativeWindow ' Constant values were found in the "windows.h" header file. Private Const WS_CHILD As Integer = &H40000000, _ WS_VISIBLE As Integer = &H10000000, _ WM_ACTIVATEAPP As Integer = &H1C Private windowHandle As Integer Public Sub New(ByVal parent As Form) Dim cp As CreateParams = New CreateParams() ' Fill in the CreateParams details. cp.Caption = "Click here" cp.ClassName = "Button" ' Set the position on the form cp.X = 100 cp.Y = 100 cp.Height = 100 cp.Width = 100 ' Specify the form as the parent. cp.Parent = parent.Handle ' Create as a child of the specified parent cp.Style = WS_CHILD Or WS_VISIBLE ' Create the actual window Me.CreateHandle(cp) End Sub ' Listen to when the handle changes to keep the variable in sync <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Sub OnHandleChange() windowHandle = Me.Handle.ToInt32() End Sub <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Sub WndProc(ByRef m As Message) ' Listen for messages that are sent to the button window. Some messages are sent ' to the parent window instead of the button's window. Select Case (m.Msg) Case WM_ACTIVATEAPP ' Do something here in response to messages End Select MyBase.WndProc(m) End Sub End Class
- SecurityPermission
for inheriting classes to call unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
- SecurityPermission
for the immediate caller to call unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: