次の方法で共有


NativeWindow.CreateHandle メソッド

作成パラメータを指定してウィンドウとそのハンドルを作成します。

Public Overridable Sub CreateHandle( _
   ByVal cp As CreateParams _)
[C#]
public virtual void CreateHandle(CreateParamscp);
[C++]
public: virtual void CreateHandle(CreateParams* cp);
[JScript]
public function CreateHandle(
   cp : CreateParams);

パラメータ

  • cp
    ウィンドウの作成パラメータを指定する CreateParams

例外

例外の種類 条件
Win32Exception ネイティブ Win32 API がウィンドウを作成できませんでした。

解説

cp パラメータは、ウィンドウとそのハンドルを作成するためにネイティブな Win32 CreateWindowEx API メソッドに渡される値を指定します。

ClassName フィールドが null 参照 (Visual Basic では Nothing) ではない場合、新しく作成されるウィンドウ ハンドルは、指定したクラスから継承します。たとえば、 ClassName を BUTTON に設定した場合、Win32 BUTTON ウィンドウ クラスに基づいた新しいウィンドウが作成されます。 ClassName オブジェクトの param フィールドは、 null 参照 (Nothing) または構造体として宣言されたクラスのインスタンスへの参照のいずれかである必要があります。

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

メモ   指定したクラス名は、オペレーティング システムで登録されます。

使用例

[Visual Basic, C#, C++] 特定のオペレーティング システム ウィンドウ クラス名を使用してウィンドウを作成する例を次に示します。この例では、これを達成するために、 NativeWindow の派生クラスを作成します。

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

[Visual Basic, C#, C++] メモ   コントロールの種類によっては、ウィンドウではなく、ウィンドウの親にウィンドウ メッセージを送信します。詳細については、Windows プラットフォーム SDK を参照してください。

 
' 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#] 
// 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++] 
// 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);
    }
};

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

必要条件

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

.NET Framework セキュリティ:

  • UIPermission (セーフ サブウィンドウでこのメソッドを呼び出すために必要なアクセス許可) UIPermissionWindow.SafeSubWindows (関連する列挙体)
  • UIPermission (トップレベル ウィンドウを作成するために必要なアクセス許可)。このアクセス許可は、ウィンドウ スタイルが子ではない場合、またはウィンドウに親がない場合にだけ要求されます。 UIPermissionWindow.SafeTopLevelWindows (関連する列挙体)

参照

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