NativeWindow 클래스
창 핸들과 창 프로시저의 저급 캡슐화를 제공합니다.
네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)
구문
‘선언
Public Class NativeWindow
Inherits MarshalByRefObject
Implements IWin32Window
‘사용 방법
Dim instance As NativeWindow
public class NativeWindow : MarshalByRefObject, IWin32Window
public ref class NativeWindow : public MarshalByRefObject, IWin32Window
public class NativeWindow extends MarshalByRefObject implements IWin32Window
public class NativeWindow extends MarshalByRefObject implements IWin32Window
설명
이 클래스는 창 클래스 만들기 및 등록을 자동으로 관리합니다.
창이 창 핸들과 연결되면 가비지 수집 권한을 상실합니다. 가비지 수집을 적절히 수행하려면 DestroyHandle을 사용하여 수동으로 핸들을 소멸하거나 ReleaseHandle을 사용하여 핸들을 해제해야 합니다.
NativeWindow 클래스는 핸들을 관리하기 위해 Handle, CreateHandle , AssignHandle , DestroyHandle 및 ReleaseHandle 등의 프로시저와 메서드를 제공합니다.
예제
다음 코드 예제에서는 창 프로시저에서 운영 체제 창 메시지를 가로채고 특정 운영 체제 창 클래스 이름으로 창을 만드는 방법을 보여 줍니다. 다음 예제에서는 이를 위해 NativeWindow로부터 상속받는 두 개의 클래스를 만듭니다.
MyNativeWindowListener
클래스는 생성자에 전달되는 폼의 창 프로시저에 후크하고 WndProc 메서드를 재정의하여 WM_ACTIVATEAPP
창 메시지를 가로챕니다. 이 클래스는 AssignHandle 및 ReleaseHandle 메서드를 사용하여 NativeWindow에서 사용할 창 핸들을 식별하는 방법을 보여 줍니다. 핸들은 Control.HandleCreated 및 Control.HandleDestroyed 이벤트를 기반으로 할당됩니다. WM_ACTIVATEAPP
창 메시지를 받으면 클래스에서 form1``ApplicationActivated
메서드를 호출합니다.
MyNativeWindow
클래스는 ClassName이 BUTTON
으로 설정된 새 창을 만듭니다. 이 클래스는 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
System::Diagnostics::Debug::WriteLine( "Application Active = {0}", ApplicationActivated.ToString() );
}
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 );
}
package NativeWindowApplication;
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.Runtime.InteropServices.*;
import System.Security.Permissions.*;
// Summary description for Form1.
public class Form1 extends System.Windows.Forms.Form
{
private MyNativeWindowListener nwl;
private MyNativeWindow nw;
void ApplicationActived(boolean applicationActivated)
{
// The application has been activated or deactivated
System.Diagnostics.Debug.WriteLine("Application Active = "
+ Convert.ToString(applicationActivated));
} //ApplicationActived
public Form1()
{
this.set_Size(new System.Drawing.Size(300, 300));
this.set_Text("Form1");
nwl = new MyNativeWindowListener(this);
nw = new MyNativeWindow(this);
} //Form1
// The main entry point for the application.
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
} //Form1
// NativeWindow class to listen to operating system messages.
/** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)
*/
public class MyNativeWindowListener extends NativeWindow
{
// Constant value was found in the "windows.h" header file.
private int WM_ACTIVATEAPP = 0x1C;
private Form1 parent;
public MyNativeWindowListener(Form1 parent)
{
parent.add_HandleCreated(new EventHandler(this.OnHandleCreated));
parent.add_HandleDestroyed(new EventHandler(this.OnHandleDestroyed));
this.parent = parent;
} //MyNativeWindowListener
// 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(((Form1)sender).get_Handle());
} //OnHandleCreated
void OnHandleDestroyed(Object sender, EventArgs e)
{
// Window was destroyed, release hook.
ReleaseHandle();
} //OnHandleDestroyed
protected void WndProc(Message m)
{
// Listen for operating system messages
if (m.get_Msg() == WM_ACTIVATEAPP) {
// Notify the form that this message was received.
// Application is activated or deactivated,
// based upon the WParam parameter.
parent.ApplicationActived(m.get_WParam().ToInt32() != 0);
}
super.WndProc(m);
} //WndProc
} //MyNativeWindowListener
// 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
.NET Framework 보안
- SecurityPermission 비관리 코드를 호출하기 위해 클래스를 상속하는 데 필요한 권한입니다. 연관된 열거형: SecurityPermissionFlag.UnmanagedCode
- SecurityPermission 직접 실행 호출자가 비관리 코드를 호출하는 데 필요한 권한입니다. 연관된 열거형: SecurityPermissionFlag.UnmanagedCode
상속 계층 구조
System.Object
System.MarshalByRefObject
System.Windows.Forms.NativeWindow
스레드로부터의 안전성
이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.
플랫폼
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 멤버
System.Windows.Forms 네임스페이스
IntPtr
Application 클래스
AxHost 클래스
Control
Form 클래스
IWin32Window 인터페이스
Message 구조체