次の方法で共有


NativeWindow.OnHandleChange メソッド

ウィンドウのハンドルが変更されたときに呼び出される通知メソッドを指定します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

'宣言
Protected Overridable Sub OnHandleChange
'使用

Me.OnHandleChange
protected virtual void OnHandleChange ()
protected:
virtual void OnHandleChange ()
protected void OnHandleChange ()
protected function OnHandleChange ()

解説

このメソッドは、Handle プロパティの値が変更された場合に呼び出されます。

継承時の注意 ウィンドウ ハンドルに対する変更を追跡するには、このメソッドをオーバーライドします。

使用例

特定のオペレーティング システム ウィンドウ クラス名を使用してウィンドウを作成するコード例を次に示します。この例では、これを達成するために、NativeWindow から継承するクラスを作成します。また、この例では、Handle が変更されたときに通知される OnHandleChange メソッドをオーバーライドしています。

MyNativeWindow クラスは、ClassNameBUTTON に設定された新しいウィンドウを作成します。これは Win32 ボタン ウィンドウを作成します。ボタンの位置とサイズは、追加のウィンドウ スタイルと併せて設定されます。このクラスは、CreateHandle メソッドを使用し、WndProc メソッドをオーバーライドして、受信されたウィンドウ メッセージを受け取る方法を示しています。この例では WM_ACTIVATEAPP メッセージを処理していますが、実際のプログラムでは、作成した型に対応するウィンドウ メッセージを使用できます。

このコードは、NativeWindow クラスの概要で紹介されている例からの抜粋です。簡略にするため、コードの一部は示されていません。コード全体については、NativeWindow を参照してください。

注意

コントロールの種類によっては、ウィンドウではなく、ウィンドウの親にウィンドウ メッセージを送信します。詳細については、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.
/** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)
 */
public class MyNativeWindow extends NativeWindow
{
    // Constant values were found in the "windows.h" header file.
    private int WS_CHILD = 0x40000000;
    private int WS_VISIBLE = 0x10000000;
    private int WM_ACTIVATEAPP = 0x1C;
    private int windowHandle;

    public MyNativeWindow(Form parent)
    {
        CreateParams cp = new CreateParams();

        // Fill in the CreateParams details.
        cp.set_Caption("Click here");
        cp.set_ClassName("Button");

        // Set the position on the form
        cp.set_X(100);
        cp.set_Y(100);
        cp.set_Height(100);
        cp.set_Width(100);

        // Specify the form as the parent.
        cp.set_Parent(parent.get_Handle());

        // Create as a child of the specified parent
        cp.set_Style(WS_CHILD | WS_VISIBLE);

        // Create the actual window
        this.CreateHandle(cp);
    } //MyNativeWindow

    // Listen to when the handle changes to keep the variable in sync
    protected void OnHandleChange()
    {
        windowHandle = this.get_Handle().ToInt32();
    } //OnHandleChange

    protected 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.
        if (m.get_Msg() == WM_ACTIVATEAPP) {
            // Do something here in response to messages
        }
        super.WndProc(m);
    } //WndProc
} //MyNativeWindow

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

NativeWindow クラス
NativeWindow メンバ
System.Windows.Forms 名前空間
Handle
AssignHandle
CreateHandle
DestroyHandle
ReleaseHandle
WndProc