c# - A study on the NativeWindow - encapsulate window handle and procedure

2023-11-05

NativeWindow gives you a way to way to intercept the window messages (are you familiar with the Windows Message Pump?) 

Aslo, this class have provided with some default impl on manipulating handles and create child handle/control.s

 

NativeWindow for Message Intercepting and Handle Manipulation

 

We will first start from the basic example of the NativeWindow. This is an example that is taken from the MSDN NativeWindow Class page. 

 

The code is as below.

    // NativeWindow class
    // please check 
    //   http://msdn.microsoft.com/en-us/library/system.windows.forms.nativewindow.aspx
    // Summary description for Form1.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    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());
        }

    }


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

        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.ApplicationActivated(((int)m.WParam != 0));
                break;
            }
            base.WndProc(ref m);
        }

    }

     // MyNativeWindow class to create a window given a class name.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    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"; // the classname has some predefined enumerations so that it knows what type of Control/handle to create.

            // 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;

            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)
        {
            switch (m.Msg)
            {
                case WM_ACTIVATEAPP:
                    // DO someting here in response to messages
                    break;
            }
            base.WndProc(ref m);
        }

    }

 the first derived class to MyNativeWindowListener class intercept the calls to Form's. It assign/destroy the handle (the handle of the main form is assigned) so that the interceptin is done. it will listen to the WM_ACTIVATE call and active the main form.)

the second class MyNativeWindow, what it does is to cretae a child handle based on the main handled passed in, the main handle is the handle of the form's. and this shows how you can leverage the NativeWindow to manipulate the handles/Controls.
If you run this code, you will see the following as a result.


 
NativeWindow to provide message pump


There is a post - Message Pump in .Net windows Service where a method of using the NativeWindow to simulate the Messgae Pump is discussed. and here is the detailed code.

    public class MessgePumpManager
    {
        private readonly Thread messagePump;

        private AutoResetEvent messagePumpRunning = new AutoResetEvent(false);

        public MessgePumpManager()
        {
            // start message pump in its own thread
            messagePump = new Thread(RunMessagePump) { Name = "ManualMessagePump" };
            messagePump.Start();
            messagePumpRunning.WaitOne();
        }

        // the message pump thread
        private void RunMessagePump()
        {
            // Create control to handle windows messages 
            MessageHandler messageHandler = new MessageHandler();

            // Do something
            // like initialize a thirdparty library
            //DLL.Init(messageHandler.Handle);

            Console.WriteLine("Message Pump thrad Started");
            messagePumpRunning.WaitOne();
            Application.Run();
        }
    }

    internal class MessageHandler : NativeWindow
    {
        public event EventHandler<MessageData> MessageReceived;

        public MessageHandler()
        {
            CreateHandle(new CreateParams());
        }

        protected override void WndProc(ref Message msg)
        {            
            // filter message here for your purposes
            EventHandler<MessageData> handler = MessageReceived;
            MessageData msgData = new MessageData(msg);
            if (handler != null) handler(this, msgData);

            base.WndProc(ref msg);
        }
    }

    // Struct cannot be inherited.
    class MessageData : EventArgs
    {
        private Message _message;
        public MessageData(Message message)
        {
            _message = message;
        }
    }

 as you can see some third party or some component may rquires a windows message pump to work, what has shown above is that the message pump handle (denoted by the MessageHandler class - inherits from the NativeWindow class ) is passed to the Third party componet, which could be in native code and be able to consume the handle and reconstruct his own message pump out of it.
The current thread (the thread which enters MessagePumpManager will block until the third party component has done with the handle - a typical way of implementing the message pump). 


 

References

NativeWindow Class

Message Pump in .Net windows Service

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c# - A study on the NativeWindow - encapsulate window handle and procedure 的相关文章

  • 忽略控制器参数缺失导致的异常

    我有一个使用 MVC4 构建的面向互联网的网站 我偶尔会收到来自机器人或好奇的用户发送不完整 URL 请求的错误报告 例如 public class ProductController Controller HttpGet public v
  • 有关堆栈大小的警告消息

    I use Visual Studio 2010 with Code Analysis活性 在我的代码中 有一行在函数中分配一些内存 TCHAR someString 40000 代码分析抛出警告信息 警告 C6262 函数使用 40000
  • WTL 子窗口事件处理

    我正在开发窗口应用程序 因为我在左侧和右侧有 2 个子窗口 我想分别处理两个窗口的输入事件 如何实现 My code class EditorWindow public DxWindow public CSplitterWindow m v
  • 将 .net 上的 AWS 开发工具包与 localstack 结合使用(TransferUtility/S3 - 设置端点)

    我有本地堆栈 https github com localstack localstack https github com localstack localstack 正在运行 并且能够使用 aws s3 cli 将文件上传到其中 我想要
  • SFINAE 在返回类型中工作,但不作为模板参数[重复]

    这个问题在这里已经有答案了 我已经使用过 SFINAE 习语很多次了 而且我习惯了把我的std enable if lt gt 在模板参数中而不是在返回类型中 然而 我遇到了一些微不足道的情况 它不起作用 我不知道为什么 首先 这是我的主要
  • 异步方法调用和模拟

    为什么模拟用户上下文仅在异步方法调用之前可用 我编写了一些代码 实际上基于 Web API 来检查模拟用户上下文的行为 async Task
  • 如何使用鼠标指针和键盘快捷键捕获文本?

    我想使用 C 或 java 使用鼠标指针和键盘快捷键从打开的窗口捕获文本 喜欢babylon http babylon com 所以 我需要知道什么以及如何实施 我需要使用哪些库 或者我可以使用 winapi 吗 使用脚本语言创建您想要执行
  • 如何更改 gdb 中的值

    所以我有这个家庭作业代码 我必须使用 gdb 进行调试 我发现了问题 但不知道如何使用gdb来改变它 define ARRAYSIZE 12 for i ARRAYSIZE 2 i gt 0 i for j i j lt ARRAYSIZE
  • 字符集中字符的顺序

    是否通过标准保证字符的顺序 例如 我可以算出字符集表中 1 符号后面跟着 2 符号吗 或者它是特定于平台的 1999 年的 C 标准对字符集是这样规定的 基本源字符集和基本执行字符集都应具有以下成员 拉丁字母表中的 26 个大写字母 拉丁字
  • 创建新文件夹的“Shell 命名空间”方式是什么?

    显然 这对于 win32 api CreateDirectory 来说是微不足道的 但我正在尝试托管一个 IShellView 并且希望以最面向 shell 的方式来做到这一点 我本以为 IShellFolder 中会有一个 createo
  • 指示 GDB 6.5 使用目标文件中嵌入的源代码

    我一直在努力让GNU gdb 6 5 14在调试时使用嵌入在目标文件中的源代码 而不是扫描某些目录 主要原因是我是为嵌入式平台开发的 并且是交叉编译的 这意味着所有源代码都在我的电脑中 我读到了关于 ggdb3标志 其中包含许多额外信息 包
  • C 中的 const 和指针

    将 const 与指针一起使用可能会导致指针对象无法通过使用相关指针取消引用来修改 但为什么我也不能修改指针不直接指向的内容呢 例如 int a 3 const int ptr a ptr 5 不会编译 但为什么 ptr 2 5 还编译不了
  • 错误:非聚合类型“vector”无法使用初始值设定项列表进行初始化

    我是 C 的初学者 每次跑步时vector
  • 如何搜索来自另一个页面模型的串联名称列表?

    我的项目中有多个模型 但在下面的屏幕中 大多数字段 属性都位于 SecurityLog 模型中 下面是我显示的官员串联列表 除了军官姓名之外 我的搜索和列标题排序功能正常 我很难合并官员姓名 因为该列表来自另一个页面模型 这是我的数据库架构
  • 实施黑名单的最有效方法

    我开发了一个 Ip 过滤器 并猜测我如何使用任何类型的 esque 数据结构开发一个非常高效且快速的黑名单过滤器 我想做的很简单 每个传入 传出连接我都必须检查被阻止的 IP 列表 IP是分散的 内存使用应该是线性的 不依赖于阻止列表的数量
  • 生成一定范围内的 N 个随机数,其总和为常数

    我想生成从 a b 之间的特定分布 例如均匀随机 抽取的 N 个随机数 其总和为常数 C 我尝试了一些我自己能想到的解决方案 以及在类似线程上提出的一些解决方案 但是他们中的大多数要么适用于有限形式的问题 要么我无法证明结果仍然遵循所需的分
  • 同一个盒子上的进程间通信 - 2 个应用程序或进程之间的通信

    在同一机器上的应用程序之间实现进程间通信的最佳方法是什么 两者都是用 C 编写的 管理器应用程序将向其他应用程序发送命令 例如 停止 启动 它还将监视应用程序并可能要求提供数据 所有应用程序都将在同一台运行 Windows 7 操作系统的计
  • 使用 解释 gprof 输出

    我试图在我的程序中找到性能问题 从而通过分析来检测代码 gprof 创建一个如下所示的平面配置文件 Flat profile Each sample counts as 0 01 seconds cumulative self self t
  • C# 如何更改 windows.forms.listview 中的网格线颜色

    如何更改 windows forms listview 中的网格线颜色 我认为没有办法在不覆盖 Paint Event 的情况下在列表视图上执行此操作 但是 如果您能够切换到网格视图 您可以这样做 this dataGridView1 Gr
  • 为什么禁止在后续私有派生类中简单提及基指针/引用?

    struct B struct D private B B fun return new D ok struct DD public D B foo return 0 error struct B B B is inaccessible 这

随机推荐

  • Vue 解决跨域问题

    Vue 解决跨域问题 通常在不同服务器访问过程中可能会遇到跨域问题 也就是口头上常说的策略同源问题 CORS 出现跨越一般就是判断三个地方 http协议 请求地址 端口号 三者若有一处不相同 那么就会出现跨域 解决这个问题就要配置一个代理服
  • 实现不同局域网文件共享的解决方案:使用Python自带HTTP服务和端口映射

    文章目录 1 前言 2 本地文件服务器搭建 2 1 python的安装和设置 2 2 cpolar的安装和注册 3 本地文件服务器的发布 3 1 Cpolar云端设置 3 2 Cpolar本地设置 4 公网访问测试 5 结语 1 前言 数据
  • LLM系列

    简介 月黑见渔灯 孤光一点萤 微微风簇浪 散作满河星 小伙伴们好 我是微信公众号 小窗幽记机器学习 的小编 卖钢丝球的小男孩 今天这篇小作文主要介绍Vicuna模型 基于官方模型13B模型部署服务及对话实测 更多 更新文章欢迎关注微信公众号
  • 【R语言】——UpSetR包绘制集合图(保姆级教程)

    本期介绍利用UpsetR包大于5个样本的 维恩图 即集合图 上期 R语言 VennDiagram包绘制维恩图 Venn diagram 保姆级教程 介绍2 5个样本绘制维恩图的方法 但当样本超过5个时 常规维恩图可视化结果的直观性及数据的捕
  • JAVA_求数组的和

    public class ChangeArgs public static void main String args 求数组的和 int arr 1 3 4 6 7 8 9 10 Counto001 y new Counto001 int
  • centos7 离线安装gcc g++(已帮你备好所有rpm文件)

    rpm文件下载链接 链接 https pan baidu com s 1ehb7GiQdGwDHcMNRgBbaRw 提取码 pys4 下载完成后 将所有rpm文件放入机器中的任意目录下 进入目录 执行 rpm Uvh rpm nodeps
  • 【硬件架构的艺术】学习笔记(2)同步和复位

    目录 写在前面 2 同步和复位 2 1 同步设计 2 1 1 避免使用行波计数器 2 1 2 门控时钟 2 1 3 双边沿或混合边沿时钟 2 1 4 用触发器驱动另一个触发器的异步复位端 2 2 推荐的设计技术 2 2 1 避免在设计中出现
  • lstm原文_上海交通大学张宇帆,艾芊等:如何理解深度LSTM网络并实现超短期负荷准确预测?...

    戳题目 看全文 基于深度长短时记忆网络的区域级超短期负荷预测方法 张宇帆 艾芊 林琳 袁帅 李昭昱 电网技术 2019年第6期 1884 1891 微文内容 一 研究背景 超短期负荷预测一般指当前时刻往后一个小时内的负荷预测 主要用于日内及
  • Tip: built files are meant to be served over an HTTP server. Opening index.html over file:// won't

    vue cli项目打包报错 Tip built files are meant to be served over an HTTP server Opening index html over file won t work 报错原因 因为
  • cmake自动选择编译器脚本

    一 什么是cmake CMake 是一个跨平台的 开源的构建系统 BuildSystem CMake 可以通过 CMakeLists txt 文件来产生特定平台的标准的构建文件 例如 为 Unix 平台生成makefiles文件 使用GCC
  • Mac安装鸿蒙系统,搭建MAC系统下的Wi-Fi loT Hi3861鸿蒙开发环境

    前言 周二就收到了Wi Fi loT Hi3861的试用开发板 最近忙的一直没有时间开始研究 终于今天周六睡了一个大懒觉起来开始准备开发环境 因为harmonyos目前只能使用ubuntu进行编译 刷写固件需要windows环境 而我习惯在
  • C++选择题

    选择题 1 说明函数int method a floct 是友元函数应为 A A friend int method a a floctz B friend int method a floctz C int friend a floctz
  • 初识ES-安装kibana

    部署kibana kibana可以给我们提供一个elasticsearch的可视化界面 便于我们学习 部署 运行docker命令 部署kibana docker run d name kibana e ELASTICSEARCH HOSTS
  • PHP完整源码编译安装

    liunx安装步骤 第一步 去官网下载php源码包 pph官网地址 下载好之后上传到liunx 这里我的用的windows vm虚拟机 推荐一个好用的命令将本地文件上传到服务器 yum install lrzsz 先安装依赖 root lo
  • 虚拟机中,安装tensorflow时killed解决办法

    我在图形界面的虚拟Linux里的pycharm里 用pip命令安装clu时 要先安装tensorflow 第一次tensorflow下载成功了 存在了cache文件夹里 但其他包发生了冲突 连接失败 当解决完冲突 在此用pip安装时 到了收
  • 【webpack】配置React环境

    配置React打包环境 开始搭建 安装webpack 基础配置 开发环境 使用Typescript 使用React 代码规范 Prettier Eslint 总结 一个现代化的web应用 已经不是单纯地优html css javascrip
  • 查看linux 所有运行的应用和端口命令

    要查看 Linux 中所有运行的应用程序及其对应的端口 可以使用以下命令 1 使用 netstat 命令 已被弃用 建议使用 ss 命令 netstat tuln 这会显示当前系统上所有打开的网络连接和监听的端口 其中 t 表示显示 TCP
  • 自动标注软件AnyLabeling安装及使用教程

    自动标注软件AnyLabeling AnyLabeling自动标注软件介绍 AnyLabeling标注软件安装 anylabeling标注软件使用 AnyLabeling自动标注软件介绍 该工具作为一个具有Segment Anything和
  • JavaScript高级

    文章目录 今日内容 一 DOM简单学习 为了满足案例要求 1 功能 二 事件简单学习 三 BOM 1 概念 Browser Object Model 浏览器对象模型 2 组成 3 Window 窗口对象 案例2 轮播图 4 Location
  • c# - A study on the NativeWindow - encapsulate window handle and procedure

    NativeWindow gives you a way to way to intercept the window messages are you familiar with the Windows Message Pump Aslo