다음을 통해 공유


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

이 창에 대한 생성 매개 변수를 지정하는 A CreateParams 입니다.

예외

네이티브 창을 만들려고 할 때 운영 체제에 리소스가 부족했습니다.

네이티브 Windows API에서 지정된 창을 만들 수 없습니다.

현재 네이티브 창의 핸들이 이미 할당되어 있습니다. 설명에서 속성은 HandleZero지 않습니다.

예제

다음 코드 예제에서는 특정 운영 체제 창 클래스 이름을 사용 하는 창을 만드는 방법을 보여 줍니다. 이 예제에서는 이 작업을 수행하기 위해 상속되는 클래스를 NativeWindow 만듭니다.

클래스는 MyNativeWindow .로 설정된 BUTTON새 창을 ClassName 만듭니다. 그러면 Win32 단추 창이 만들어집니다. 단추의 위치 및 크기는 추가 창 스타일을 지정하는 방법과 함께 설정됩니다. 이 클래스는 메서드를 사용하고 CreateHandle 메서드를 재정의 WndProc 하여 수신된 창 메시지를 가로채는 방법을 보여 줍니다. 이 예제에서는 WM_ACTIVATEAPP 메시지를 찾지만 실제 프로그램에서 만든 형식과 관련된 창 메시지로 바꿀 수 있습니다.

메모

일부 컨트롤 형식은 창이 아닌 창 부모 창에 창 메시지를 보냅니다. 자세한 내용은 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 메서드에 전달되는 값을 지정합니다.

필드가 ClassNamenull으면 새로 만든 창 핸들이 지정된 클래스에서 상속됩니다. 예를 들어 설정된 BUTTON경우 ClassName 새로 만든 창은 Win32 BUTTON 창 클래스를 기반으로 합니다. 개체의 속성은 Param 구조체로 선언된 클래스의 인스턴스이거나 참조해야 합니다null.ClassName

이 코드는 클래스 개요에 표시된 예제에서 발췌한 NativeWindow 것입니다. 간결성을 위해 일부 코드가 표시되지 않습니다. 전체 코드 목록을 참조하세요 NativeWindow .

메모

제공된 클래스 이름은 운영 체제에 등록됩니다.

적용 대상

추가 정보