NativeWindow.CreateHandle(CreateParams) Method

Definition

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

public virtual void CreateHandle (System.Windows.Forms.CreateParams cp);

Parameters

cp
CreateParams

A CreateParams that specifies the creation parameters for this window.

Exceptions

The operating system ran out of resources when trying to create the native window.

The native Windows API could not create the specified window.

The handle of the current native window is already assigned; in explanation, the Handle property is not equal to Zero.

Examples

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

Note

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.
internal 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
    protected override void OnHandleChange()
    {
        windowHandle = (int)this.Handle;
    }

    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);
    }
}

Remarks

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

When the ClassName field is not null, 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 property of the ClassName object must either be null 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.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also