NativeWindow 클래스

정의

창 핸들과 창 프로시저의 저급 캡슐화를 제공합니다.

public ref class NativeWindow : MarshalByRefObject
public ref class NativeWindow : MarshalByRefObject, System::Windows::Forms::IWin32Window
public class NativeWindow : MarshalByRefObject
public class NativeWindow : MarshalByRefObject, System.Windows.Forms.IWin32Window
type NativeWindow = class
    inherit MarshalByRefObject
type NativeWindow = class
    inherit MarshalByRefObject
    interface IWin32Window
Public Class NativeWindow
Inherits MarshalByRefObject
Public Class NativeWindow
Inherits MarshalByRefObject
Implements IWin32Window
상속
NativeWindow
구현

예제

다음 코드 예제는 특정 운영 체제 창 클래스 이름이 창 만들기는 창 프로시저에서 운영 체제 창 메시지를 가로채는 방법을 보여 줍니다. 이 예제에서 상속 되는 두 개의 클래스를 만듭니다 NativeWindow 이 작업을 수행 하는 합니다.

MyNativeWindowListener 클래스 생성자에 전달 되는 폼의 창 프로시저로 후크 및 재정의 WndProc 메서드를 가로채는 WM_ACTIVATEAPP 창 메시지입니다. 클래스의 사용법을 보여 줍니다.는 AssignHandleReleaseHandle 창 핸들을 식별 하는 방법의 NativeWindow 사용 됩니다. 핸들을 기반으로 할당 합니다 Control.HandleCreatedControl.HandleDestroyed 이벤트입니다. 경우는 WM_ACTIVATEAPP 창 메시지를 받을 클래스 호출을 form1.ApplicationActivated 메서드.

합니다 MyNativeWindow 클래스를 사용 하 여 새 창을 만듭니다 합니다 ClassNameBUTTON합니다. 클래스를 사용 하는 방법을 보여 줍니다 합니다 CreateHandle 메서드와 재정의 WndProc 수신 되는 창 메시지를 가로채는 메서드.

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
ref class MyNativeWindowListener;
ref class MyNativeWindow;

// Summary description for Form1.
ref class Form1: public System::Windows::Forms::Form
{
private:
   MyNativeWindowListener^ nwl;
   MyNativeWindow^ nw;

internal:
   void ApplicationActived( bool ApplicationActivated )
   {
      // The application has been activated or deactivated
      #if defined(DEBUG)
      System::Diagnostics::Debug::WriteLine( "Application Active = {0}", ApplicationActivated.ToString() );
      #endif
   }


public:
   Form1();
};

// NativeWindow class to listen to operating system messages.
ref class MyNativeWindowListener: public NativeWindow
{
private:

   // Constant value was found in the S"windows.h" header file.
   literal int WM_ACTIVATEAPP = 0x001C;
   Form1^ parent;

public:
   MyNativeWindowListener( Form1^ parent )
   {
      parent->HandleCreated += gcnew EventHandler( this, &MyNativeWindowListener::OnHandleCreated );
      parent->HandleDestroyed += gcnew EventHandler( this, &MyNativeWindowListener::OnHandleDestroyed );
      this->parent = parent;
   }

internal:

   // Listen for the control's window creation and then hook into it.
   void OnHandleCreated( Object^ sender, EventArgs^ /*e*/ )
   {
      // Window is now created, assign handle to NativeWindow.
      AssignHandle( (dynamic_cast<Form1^>(sender))->Handle );
   }

   void OnHandleDestroyed( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      // Window was destroyed, release hook.
      ReleaseHandle();
   }

protected:

   virtual void WndProc( Message %m ) override
   {
      // Listen for operating system messages
      switch ( m.Msg )
      {
         case WM_ACTIVATEAPP:

            // Notify the form that this message was received.
            // Application is activated or deactivated,
            // based upon the WParam parameter.
            parent->ApplicationActived( ((int)m.WParam != 0) );
            break;
      }
      NativeWindow::WndProc( m );
   }

};

// 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 );
   }
};

Form1::Form1()
{
   this->Size = System::Drawing::Size( 300, 300 );
   this->Text = "Form1";
   nwl = gcnew MyNativeWindowListener( this );
   nw = gcnew MyNativeWindow( this );
}

// The main entry point for the application.

[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace NativeWindowApplication
{
    // Summary description for Form1.
    public class Form1 : System.Windows.Forms.Form
    {
        private MyNativeWindowListener nwl;
        private MyNativeWindow nw;

        internal void ApplicationActivated(bool ApplicationActivated)
        {
            // The application has been activated or deactivated
            System.Diagnostics.Debug.WriteLine("Application Active = " + ApplicationActivated.ToString());
        }

        private Form1()
        {
            this.Size = new System.Drawing.Size(300, 300);
            this.Text = "Form1";

            nwl = new MyNativeWindowListener(this);
            nw = new MyNativeWindow(this);
        }

        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }

    // NativeWindow class to listen to operating system messages.
    internal class MyNativeWindowListener : NativeWindow
    {
        // Constant value was found in the "windows.h" header file.
        private const int WM_ACTIVATEAPP = 0x001C;

        private Form1 parent;

        public MyNativeWindowListener(Form1 parent)
        {

            parent.HandleCreated += new EventHandler(this.OnHandleCreated);
            parent.HandleDestroyed += new EventHandler(this.OnHandleDestroyed);
            this.parent = parent;
        }

        // Listen for the control's window creation and then hook into it.
        internal void OnHandleCreated(object sender, EventArgs e)
        {
            // Window is now created, assign handle to NativeWindow.
            AssignHandle(((Form1)sender).Handle);
        }
        internal void OnHandleDestroyed(object sender, EventArgs e)
        {
            // Window was destroyed, release hook.
            ReleaseHandle();
        }

        protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages

            switch (m.Msg)
            {
                case WM_ACTIVATEAPP:

                    // Notify the form that this message was received.
                    // Application is activated or deactivated, 
                    // based upon the WParam parameter.
                    parent.ApplicationActivated(((int)m.WParam != 0));

                    break;
            }
            base.WndProc(ref 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);
        }
    }
}
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private nwl As MyNativeWindowListener
    Private nw As MyNativeWindow

    Friend Sub ApplicationActivated(ByVal ApplicationActivated As Boolean)
        ' The application has been activated or deactivated
        System.Diagnostics.Debug.WriteLine("Application Active = " + ApplicationActivated.ToString())
    End Sub

    Private Sub New()
        MyBase.New()

        Me.Size = New System.Drawing.Size(300, 300)
        Me.Text = "Form1"

        nwl = New MyNativeWindowListener(Me)
        nw = New MyNativeWindow(Me)

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

' NativeWindow class to listen to operating system messages.
Friend Class MyNativeWindowListener
    Inherits NativeWindow

    ' Constant value was found in the "windows.h" header file.
    Private Const WM_ACTIVATEAPP As Integer = &H1C

    Private parent As Form1

    Public Sub New(ByVal parent As Form1)

        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        Me.parent = parent
    End Sub

    ' Listen for the control's window creation and hook into it.    
    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Window is now created, assign handle to NativeWindow.
        AssignHandle(CType(sender, Form).Handle)
    End Sub

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ' Window was destroyed, release hook.
        ReleaseHandle()
    End Sub

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for operating system messages

        Select Case (m.Msg)
            Case WM_ACTIVATEAPP

                ' Notify the form that this message was received.
                ' Application is activated or deactivated, 
                ' based upon the WParam parameter.
                parent.ApplicationActivated(m.WParam.ToInt32() <> 0)

        End Select

        MyBase.WndProc(m)
    End Sub
End Class

' 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

설명

이 클래스는 창 클래스 만들기 및 등록에 자동으로 관리합니다.

창의 창 핸들을 사용 하 여 연결 된 경우 가비지 수집의 대상이 아닙니다. 적절 한 가비지 수집을 위해 처리 해야 하거나 제거할 사용 하 여 수동으로 DestroyHandle 사용 하 여 해제 또는 ReleaseHandle합니다.

참고

ReleaseHandle 메서드 WM_NCDESTROY 메시지를 처리할 때 호출 됩니다. 즉, 어떤 경우에서 필요가 없습니다를 직접 호출 하도록 하는 경우가 있습니다 ReleaseHandle, 있지만 이렇게 하는 것이 좋습니다.

합니다 NativeWindow 클래스는 다음 속성 및 처리를 관리 하는 메서드를 제공: HandleCreateHandle, AssignHandleDestroyHandle, 및 ReleaseHandle합니다.

생성자

NativeWindow()

NativeWindow 클래스의 인스턴스를 초기화합니다.

속성

Handle

이 창의 핸들을 가져옵니다.

메서드

AssignHandle(IntPtr)

이 창에 핸들을 할당합니다.

CreateHandle(CreateParams)

지정된 생성 매개 변수로 창 및 해당 핸들을 만듭니다.

CreateObjRef(Type)

원격 개체와 통신하는 데 사용되는 프록시 생성에 필요한 모든 관련 정보가 들어 있는 개체를 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
DefWndProc(Message)

이 창과 연결된 기본 창 프로시저를 호출합니다.

DestroyHandle()

창 및 해당 핸들을 소멸시킵니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Finalize()

이 창과 연결된 리소스를 해제합니다.

FromHandle(IntPtr)

지정된 핸들과 연결된 창을 검색합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 현재의 수명 서비스 개체를 검색합니다.

(다음에서 상속됨 MarshalByRefObject)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
InitializeLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 수명 서비스 개체를 가져옵니다.

(다음에서 상속됨 MarshalByRefObject)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
MemberwiseClone(Boolean)

현재 MarshalByRefObject 개체의 단순 복사본을 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
OnHandleChange()

창의 핸들이 변경될 때 호출되는 알림 메서드를 지정합니다.

OnThreadException(Exception)

파생된 클래스에서 재정의될 때 해당 메서드는 처리되지 않은 스레드 예외를 관리합니다.

ReleaseHandle()

이 창과 연결된 핸들을 해제합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)
WmDpiChangedAfterParent(Message)

창 핸들과 창 프로시저의 저급 캡슐화를 제공합니다.

WmDpiChangedBeforeParent(Message)

창 핸들과 창 프로시저의 저급 캡슐화를 제공합니다.

WndProc(Message)

이 창과 연결된 기본 창 프로시저를 호출합니다.

적용 대상

추가 정보