Export (0) Print
Expand All

NativeWindow.CreateHandle Method

Creates a window and its handle with the specified creation parameters.

[Visual Basic]
Public Overridable Sub CreateHandle( _
   ByVal cp As CreateParams _
)
[C#]
public virtual void CreateHandle(
 CreateParams cp
);
[C++]
public: virtual void CreateHandle(
 CreateParams* cp
);
[JScript]
public function CreateHandle(
   cp : CreateParams
);

Parameters

cp
A CreateParams that specifies the creation parameters for this window.

Exceptions

Exception Type Condition
Win32Exception The native Win32 API could not create the window.

Remarks

The cp parameter specifies the values that are passed to the native Win32 CreateWindowEx API method to create a window and its handle.

When the ClassName field is not a null reference (Nothing in Visual Basic), the newly created window handle inherits from the specified class. For example, if ClassName is set to BUTTON, the newly created window is based on the Win32 BUTTON window class. The param field of the ClassName object must either be a null reference (Nothing) or reference an instance of a class that was declared as a structure.

This code is an excerpt from the example shown in the NativeWindow class overview. Some code is not shown for the purpose of brevity. See NativeWindow for the whole code listing.

Note   The class name provided is registered with the operating system.

Example

[Visual Basic, C#, C++] The following example demonstrates creating a window with a specific operating system window class name. The example create a class that inherits from NativeWindow to accomplish this.

[Visual Basic, C#, C++] 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 can be replaced in a real program with window messages specific to the type created.

[Visual Basic, C#, C++] Note   Some control types send their window messages to the window parent instead of the window. Refer to the Windows Platform SDK for more information.
[Visual Basic] 
' MyNativeWindow class to create a window given a class name.
Public 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

[C#] 
// MyNativeWindow class to create a window given a class name.
public class MyNativeWindow: NativeWindow{

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

    public MyNativeWindow(Form parent){

        CreateParams cp = 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 | WS_VISIBLE;

        // Create the actual window
        this.CreateHandle(cp);
    }

    // Listen to when the handle changes to keep the variable in sync
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    protected override void OnHandleChange(){
        windowHandle  = (int)this.Handle;
    }

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    protected override void WndProc(ref Message m){
        // 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
                break;
        }
        base.WndProc(ref m);
    }       
}

[C++] 
// MyNativeWindow class to create a window given a class name.
__gc class MyNativeWindow : public NativeWindow {

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

    int windowHandle;

public:
    MyNativeWindow(Form* parent) {

        CreateParams* cp = new CreateParams();

        // Fill in the CreateParams details.
        cp->Caption = S"Click here";
        cp->ClassName = S"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);
    }

protected:
    // Listen to when the handle changes to keep the variable in sync
    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void OnHandleChange() {
        windowHandle  = (int)this->Handle;
    }

    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void WndProc(Message* m) {
        // 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
                break;
        }
        NativeWindow::WndProc(m);
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

NativeWindow Class | NativeWindow Members | System.Windows.Forms Namespace | CreateParams | NativeWindow | Handle | AssignHandle | DestroyHandle | ReleaseHandle | WndProc

Show:
© 2014 Microsoft