NativeWindow.CreateHandle(CreateParams) Méthode

Définition

Crée une fenêtre et son handle avec les paramètres de création spécifiés.

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)

Paramètres

cp
CreateParams

CreateParams qui spécifie les paramètres de création pour cette fenêtre.

Exceptions

Le système d'exploitation a manqué de ressources lors de la tentative de création de la fenêtre native.

L'API Windows native n'a pas pu créer la fenêtre spécifiée.

Le handle de la fenêtre native active est déjà assigné ; plus précisément, la propriété Handle n'est pas égale à Zero.

Exemples

L’exemple de code suivant illustre la création d’une fenêtre avec un nom de classe de fenêtre de système d’exploitation spécifique. L’exemple crée une classe qui hérite de NativeWindow pour accomplir cette opération.

La MyNativeWindow classe crée une fenêtre avec la ClassName valeur définie sur BUTTON. Cela crée une fenêtre de bouton Win32. L’emplacement et la taille du bouton sont définis, ainsi que la spécification de styles de fenêtre supplémentaires. La classe montre comment utiliser la CreateHandle méthode et remplacer la méthode pour intercepter les WndProc messages de fenêtre reçus. Bien que l’exemple recherche le message WM_ACTIVATEAPP, il peut être remplacé dans un programme réel par des messages de fenêtre spécifiques au type créé.

Notes

Certains types de contrôles envoient leurs messages de fenêtre au parent de la fenêtre au lieu de la fenêtre. Pour plus d’informations, consultez la SDK de plateforme Windows.

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

Remarques

Le cp paramètre spécifie les valeurs qui sont passées à la méthode Win32 CreateWindowEx native pour créer une fenêtre et son handle.

Lorsque le ClassName champ n’est pas null, le descripteur de fenêtre nouvellement créé hérite de la classe spécifiée. Par exemple, si ClassName est défini sur BUTTON, la fenêtre nouvellement créée est basée sur la classe de fenêtre Win32 BUTTON . La Param propriété de l’objet ClassName doit être null ou référencer un instance d’une classe qui a été déclarée en tant que structure.

Ce code est un extrait de l’exemple présenté dans la vue d’ensemble de la NativeWindow classe. Certains codes ne sont pas affichés par souci de concision. Consultez NativeWindow pour obtenir la liste complète du code.

Notes

Le nom de classe fourni est inscrit auprès du système d’exploitation.

S’applique à

Voir aussi