NativeWindow.CreateHandle(CreateParams) Metodo

Definizione

Crea una finestra e il relativo handle con i parametri di creazione specificati.

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)

Parametri

cp
CreateParams

CreateParams che specifica i parametri di creazione per la finestra corrente.

Eccezioni

Nel tentativo di creare la finestra nativa, il sistema operativo ha esaurito le risorse.

L'API Windows nativa non è in grado di creare la finestra specificata.

L'handle della finestra nativa corrente è già stato assegnato; in altri termini, la proprietà Handle è diversa da Zero.

Esempio

Nell'esempio di codice seguente viene illustrata la creazione di una finestra con un nome specifico della classe della finestra del sistema operativo. Nell'esempio viene creata una classe che eredita da NativeWindow per eseguire questa operazione.

La MyNativeWindow classe crea una nuova finestra con il ClassName set su BUTTON. In questo modo viene creata una finestra del pulsante Win32. La posizione e le dimensioni del pulsante sono impostate, insieme alla specifica di stili di finestra aggiuntivi. La classe illustra come usare il metodo e eseguire l'override del CreateHandleWndProc metodo per intercettare i messaggi della finestra ricevuti. Anche se l'esempio cerca il messaggio WM_ACTIVATEAPP, questo può essere sostituito in un programma reale con messaggi di finestra specifici del tipo creato.

Nota

Alcuni tipi di controllo inviano i messaggi della finestra all'elemento padre della finestra anziché alla finestra. Per altre informazioni, vedere la SDK per piattaforma 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

Commenti

Il cp parametro specifica i valori passati al metodo Win32 CreateWindowEx nativo per creare una finestra e il relativo handle.

Quando il campo non nullè , l'handle ClassName di finestra appena creato eredita dalla classe specificata. Ad esempio, se ClassName è impostato su BUTTON, la finestra appena creata è basata sulla classe finestra Win32 BUTTON . La Param proprietà dell'oggetto deve essere null o fare riferimento a un'istanza ClassName di una classe dichiarata come struttura.

Questo codice è un estratto dell'esempio illustrato nella panoramica della NativeWindow classe. Alcuni codici non vengono visualizzati allo scopo di brevità. Vedere NativeWindow per l'intero elenco di codice.

Nota

Il nome della classe specificato è registrato con il sistema operativo.

Si applica a

Vedi anche