NativeWindow.CreateHandle(CreateParams) Método

Definición

Crea una ventana y un identificador de dicha ventana con los parámetros de creación especificados.

public:
 virtual void CreateHandle(System::Windows::Forms::CreateParams ^ cp);
public virtual void CreateHandle (System.Windows.Forms.CreateParams cp);
abstract member CreateHandle : System.Windows.Forms.CreateParams -> unit
override this.CreateHandle : System.Windows.Forms.CreateParams -> unit
Public Overridable Sub CreateHandle (cp As CreateParams)

Parámetros

cp
CreateParams

CreateParams que especifica los parámetros de creación de esta ventana.

Excepciones

El sistema operativo agotó los recursos al intentar crear la ventana nativa.

La API nativa de Windows no pudo crear la ventana especificada.

El indicador de la ventana nativa actual ya está asignado; es decir, el valor de la propiedad Handle no es igual a Zero.

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear una ventana con un nombre de clase de ventana de sistema operativo específico. En el ejemplo se crea una clase que hereda de NativeWindow para lograrlo.

La MyNativeWindow clase crea una nueva ventana con el ClassName establecido en BUTTON. Esto crea una ventana de botón Win32. Se establece la ubicación y el tamaño del botón, junto con la especificación de estilos de ventana adicionales. La clase muestra cómo usar el CreateHandle método e invalidar el WndProc método para interceptar los mensajes de ventana que se reciben. Aunque en el ejemplo se busca el mensaje de WM_ACTIVATEAPP, esto se puede reemplazar en un programa real con mensajes de ventana específicos del tipo creado.

Nota:

Algunos tipos de control envían sus mensajes de ventana al elemento primario de la ventana en lugar de a la ventana. Consulte el SDK de Plataforma para Windows para obtener más información.

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

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

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

protected:

   // 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
            break;
      }
      NativeWindow::WndProc( m );
   }
};
// 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);
    }
}
' 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

Comentarios

El cp parámetro especifica los valores que se pasan al método Win32 CreateWindowEx nativo para crear una ventana y su identificador.

Cuando el ClassName campo no nulles , el identificador de ventana recién creado hereda de la clase especificada. Por ejemplo, si ClassName se establece BUTTONen , la ventana recién creada se basa en la clase de ventana Win32 BUTTON . La Param propiedad del ClassName objeto debe ser null o hacer referencia a una instancia de una clase que se declaró como una estructura.

Este código es un extracto del ejemplo que se muestra en la información general de la NativeWindow clase. Algunos códigos no se muestran para la brevedad. Consulte NativeWindow para obtener toda la lista de código.

Nota

El nombre de clase proporcionado se registra con el sistema operativo.

Se aplica a

Consulte también