Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Handle Property
Collapse the table of content
Expand the table of content

NativeWindow::Handle Property

Gets the handle for this window.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

virtual property IntPtr Handle {
	IntPtr get () sealed;

Property Value

Type: System::IntPtr
If successful, an IntPtr representing the handle to the associated native Win32 window; otherwise, 0 if no handle is associated with the window.



Use this method when calling Windows API methods that require a handle for a window or control.

The following code example demonstrates creating a window with a specific operating system window class name. The example creates a class that inherits from NativeWindow to accomplish this. The example also demonstrates overriding the OnHandleChange method to be notified when the Handle changes.

The MyNativeWindow class creates a new window with the ClassName set to BUTTON. This creates a Win32 button window. The location and size of the button is set, along with specifying additional window styles. The class demonstrates how to use the CreateHandle method and override the WndProc method to intercept window messages that are received. Although the example looks for the WM_ACTIVATEAPP message, this could be replaced in a real program with window messages specific to the type created.


Some control types send their window messages to the window parent instead of the window. See the Windows Platform SDK for more information.

// MyNativeWindow class to create a window given a class name. 
ref class MyNativeWindow: public NativeWindow

   // Constant values were found in the S"windows.h" header file.
   literal int WS_CHILD = 0x40000000,WS_VISIBLE = 0x10000000,WM_ACTIVATEAPP = 0x001C;
   int windowHandle;

   MyNativeWindow( Form^ parent )
      CreateParams^ cp = gcnew 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 | WS_VISIBLE;

      // Create the actual window 
      this->CreateHandle( cp );


   // Listen to when the handle changes to keep the variable in sync 

   virtual void OnHandleChange() override
      windowHandle = (int)this->Handle;

   virtual void WndProc( Message % m ) override
      // Listen for messages that are sent to the button window. Some messages are sent 
      // to the parent window instead of the button's window. 
      switch ( m.Msg )
         case WM_ACTIVATEAPP:

            // Do something here in response to messages 
      NativeWindow::WndProc( m );

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft