次の方法で共有


NativeWindow クラス

ウィンドウ ハンドルとウィンドウ プロシージャの低水準のカプセル化を提供します。

この型のすべてのメンバの一覧については、NativeWindow メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.Windows.Forms.NativeWindow

Public Class NativeWindow
   Inherits MarshalByRefObject
[C#]
public class NativeWindow : MarshalByRefObject
[C++]
public __gc class NativeWindow : public MarshalByRefObject
[JScript]
public class NativeWindow extends MarshalByRefObject

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このクラスは、ウィンドウ クラスの作成と登録を自動的に管理します。

ウィンドウは、ウィンドウ ハンドルが関連付けられている場合は、ガベージ コレクションの対象にはなりません。適切なガベージ コレクションを行うには、ハンドルを DestroyHandle を使用して手動で破棄するか、 ReleaseHandle を使用して解放する必要があります。

NativeWindow クラスには、 HandleCreateHandleAssignHandleDestroyHandleReleaseHandle など、ハンドルを管理するためのプロシージャやメソッドがあります。

使用例

[Visual Basic, C#, C++] オペレーティング システムのウィンドウ メッセージをウィンドウ プロシージャで受け取り、特定のオペレーティング システム ウィンドウ クラス名を指定してウィンドウを作成する例を次に示します。この例では、これを達成するために、 NativeWindow の派生クラスを 2 つ作成しています。

[Visual Basic, C#, C++] MyNativeWindowListener クラスは、コンストラクタに渡されたフォームのウィンドウ プロシージャにフックし、 WndProc メソッドをオーバーライドして WM_ACTIVATEAPP ウィンドウ メッセージを受け取ります。このクラスでは、 NativeWindow が使用するウィンドウ ハンドルを識別するために AssignHandle メソッドと ReleaseHandle メソッドを使用する方法を示しています。このハンドルは、 Control.HandleCreated イベントと Control.HandleDestroyed イベントを基に割り当てられます。 WM_ACTIVATEAPP ウィンドウ メッセージが受信されると、このクラスは form1 ApplicationActivated メソッドを呼び出します。

[Visual Basic, C#, C++] MyNativeWindow クラスは、 ClassNameBUTTON に設定された新しいウィンドウを作成します。このクラスでは、 CreateHandle メソッドを使用し、 WndProc メソッドをオーバーライドして、受信されたウィンドウ メッセージを受け取る方法を示しています。

 
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

' Summary description for Form1.
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private nwl As MyNativeWindowListener
    Private nw As MyNativeWindow

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

    Public 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

End Class

' NativeWindow class to listen to operating system messages.
Public 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.ApplicationActived(m.WParam.ToInt32() <> 0)

        End Select

        MyBase.WndProc(m)
    End Sub
End Class

' MyNativeWindow class to create a window given a class name.
Public 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

[C#] 
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 ApplicationActived(bool ApplicationActivated){
            // The application has been activated or deactivated
            System.Diagnostics.Debug.WriteLine("Application Active = " + ApplicationActivated.ToString());
        }

        public 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.
    public 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();
        }
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        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.ApplicationActived(((int)m.WParam != 0));

                    break;                
            }
            base.WndProc(ref m);
        }        
    }

    // MyNativeWindow class to create a window given a class name.
    public 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
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        protected override void OnHandleChange(){
            windowHandle  = (int)this.Handle;
        }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        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);
        }       
    }
}

[C++] 

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;

__gc class MyNativeWindowListener;
__gc class MyNativeWindow;

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

    public private: 
        void ApplicationActived(bool ApplicationActivated) {
            // The application has been activated or deactivated
            System::Diagnostics::Debug::WriteLine(S"Application Active = {0}", ApplicationActivated.ToString());
        }

public:
    Form1();
};

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

    // Constant value was found in the S"windows.h" header file.
private:
    const static int  WM_ACTIVATEAPP = 0x001C;
    Form1*  parent;

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

    public private: 
        // 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:
    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void WndProc(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->ApplicationActived(((int)m->WParam != 0));

                break;
        }
        NativeWindow::WndProc(m);

    }
};

// MyNativeWindow class to create a window given a class name.
__gc class MyNativeWindow : public NativeWindow {

    // Constant values were found in the S"windows.h" header file.
private:
    const static int
        WS_CHILD = 0x40000000,
        WS_VISIBLE = 0x10000000,
        WM_ACTIVATEAPP = 0x001C;

    int windowHandle;

public:
    MyNativeWindow(Form* parent) {

        CreateParams* cp = new CreateParams();

        // Fill in the CreateParams details.
        cp->Caption = S"Click here";
        cp->ClassName = S"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
    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void OnHandleChange() {
        windowHandle  = (int)this->Handle;
    }

    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void WndProc(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;
        }
        NativeWindow::WndProc(m);
    }
};


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

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

.NET Framework セキュリティ:

参照

NativeWindow メンバ | System.Windows.Forms 名前空間