NativeWindow.CreateHandle(CreateParams) Metoda

Definicja

Tworzy okno i jego uchwyt z określonymi parametrami tworzenia.

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)

Parametry

cp
CreateParams

Element CreateParams określający parametry tworzenia dla tego okna.

Wyjątki

System operacyjny zabrakło zasobów podczas próby utworzenia okna natywnego.

Natywny interfejs API systemu Windows nie może utworzyć określonego okna.

Uchwyt bieżącego okna natywnego jest już przypisany; w wyjaśnieniu Handle właściwość nie jest równa Zero.

Przykłady

Poniższy przykład kodu przedstawia tworzenie okna z określoną nazwą klasy okna systemu operacyjnego. W przykładzie zostanie utworzona klasa dziedziczona z NativeWindow , aby to osiągnąć.

Klasa MyNativeWindow tworzy nowe okno z zestawem ClassName na BUTTONwartość . Spowoduje to utworzenie okna przycisku Win32. Ustawiono lokalizację i rozmiar przycisku wraz z określeniem dodatkowych stylów okna. Klasa pokazuje, jak użyć metody i zastąpić WndProc metodę CreateHandle w celu przechwycenia odebranych komunikatów okien. Mimo że przykład szuka komunikatu WM_ACTIVATEAPP, można go zastąpić w rzeczywistym programie komunikatami okna specyficznymi dla utworzonego typu.

Uwaga

Niektóre typy kontrolek wysyłają komunikaty okien do elementu nadrzędnego okna zamiast okna. Aby uzyskać więcej informacji, zobacz Zestaw SDK do platformy systemu 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

Uwagi

Parametr cp określa wartości przekazywane do natywnej metody Win32 CreateWindowEx w celu utworzenia okna i jego uchwytu.

ClassName Jeśli pole nie nulljest , nowo utworzony uchwyt okna dziedziczy z określonej klasy. Jeśli na przykład ClassName ustawiono wartość BUTTON, nowo utworzone okno jest oparte na klasie okna Win32 BUTTON . Właściwość ParamClassName obiektu musi być null lub odwoływać się do wystąpienia klasy, która została zadeklarowana jako struktura.

Ten kod jest fragmentem przykładu pokazanego w przeglądzie NativeWindow klasy. Niektóre kody nie są wyświetlane w celu zwięzłości. Zobacz, aby zapoznać się NativeWindow z całą listą kodu.

Uwaga

Podana nazwa klasy jest zarejestrowana w systemie operacyjnym.

Dotyczy

Zobacz też