NativeWindow.CreateHandle(CreateParams) 方法

定義

使用指定的建立參數建立視窗和它的控制代碼。

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)

參數

cp
CreateParams

CreateParams,指定這個視窗的建立參數。

例外狀況

嘗試建立原生視窗時,作業系統耗盡資源。

原生 Windows API 無法建立指定的視窗。

已指派目前原生視窗的控制代碼。換言之,Handle 屬性不等於 Zero

範例

下列程式碼範例示範如何建立具有特定作業系統視窗類別名稱的視窗。 此範例會建立繼承自 NativeWindow 的類別來完成此作業。

類別 MyNativeWindow 會建立新的視窗,並將 設定為 ClassNameBUTTON 。 這會建立 Win32 按鈕視窗。 設定按鈕的位置和大小,以及指定其他視窗樣式。 類別示範如何使用 CreateHandle 方法,並覆寫 WndProc 方法來攔截收到的視窗訊息。 雖然此範例會尋找WM_ACTI加值稅EAPP訊息,但在實際程式中可以取代為所建立類型特定的視窗訊息。

注意

有些控制項類型會將其視窗訊息傳送至視窗父代,而不是視窗。 如需詳細資訊,請參閱Windows 平臺 SDK。

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

備註

參數 cp 會指定傳遞至原生 Win32 CreateWindowEx 方法的值,以建立視窗及其控制碼。

ClassName當欄位不是 null 時,新建立的視窗控制碼會繼承自指定的類別。 例如,如果 ClassName 設定為 BUTTON ,則新建立的視窗是以 Win32 BUTTON 視窗類別為基礎。 物件的 Param 屬性 ClassName 必須是 null 或參考宣告為 結構之類別的實例。

此程式碼是類別概觀中所顯示範例的 NativeWindow 摘錄。 為了簡潔起見,不會顯示某些程式碼。 如需整個程式代碼清單,請參閱 NativeWindow

注意

提供的類別名稱會向作業系統註冊。

適用於

另請參閱