NativeWindow 类

定义

提供窗口句柄和窗口过程的低级封装。

C#
public class NativeWindow : MarshalByRefObject
C#
public class NativeWindow : MarshalByRefObject, System.Windows.Forms.IWin32Window
继承
NativeWindow
实现

示例

下面的代码示例演示如何在窗口过程中截获操作系统窗口消息,并创建具有特定操作系统窗口类名称的窗口。 该示例创建两个继承自 NativeWindow 的类来实现此目的。

MyNativeWindowListener 挂钩到传递到构造函数中的窗体的窗口过程,并重写 WndProc 方法以截获 WM_ACTIVATEAPP 窗口消息。 类演示如何使用 AssignHandleReleaseHandle 方法来标识 将使用的窗口句柄 NativeWindow 。 句柄基于 Control.HandleCreatedControl.HandleDestroyed 事件进行分配。 WM_ACTIVATEAPP收到窗口消息时, 类将调用 form1.ApplicationActivated 方法。

MyNativeWindow 创建一个新窗口, ClassName 并将 设置为 BUTTON。 类演示如何使用 CreateHandle 方法并重写 WndProc 方法来截获收到的窗口消息。

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

注解

此类会自动管理窗口类的创建和注册。

当窗口与窗口句柄关联时,该窗口不符合垃圾回收的条件。 为了确保垃圾回收正确,必须使用 手动 DestroyHandle 销毁句柄或使用 释放 ReleaseHandle句柄。

备注

处理 ReleaseHandle WM_NCDESTROY消息时调用 方法。 这意味着在某些情况下,无需手动调用 ReleaseHandle,但最好这样做。

NativeWindow 提供以下属性和方法来管理句柄: HandleCreateHandleAssignHandleDestroyHandleReleaseHandle

构造函数

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)

调用与此窗口关联的默认窗口过程。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅