http://www.delphidabbler.com/articles?article=1

Why do it?

Sometimes we need a non-windowed component (i.e. one that isn't derived fromTWinControl) to receive Windows messages.

To receive messages the component needs a window handle, but a non-windowed component hasn't got one!

This article is about how to enable such a component to use a hidden window to receive messages.

How it's done

The Delphi library function AllocateHWnd is used to create a hidden window for us

and the related DeallocateHWnd disposes of the window when we've finished with it.

The hidden window requires window procedure.

AllocateHWnd enables us to use a method as a window procedure where Windows normally requires a stdcall function.

We pass a reference to the required method to AllocateHWnd and

it takes care of the problem of registering the method as a window procedure for us.

Inside the registered method we handle the messages we are interested in

and hand the rest off to Windows using the DefWindowProc API call.

Listing 2 below provides a skeleton of how to use AllocateHWnd.

First though, Listing 1shows an outline definition for our component class:

type{ Our class derived from TComponent or another ancestor class }TMyClass = class(TComponent)privatefHWnd: HWND;{ field to store the window handle }...protectedprocedure WndMethod(var Msg: TMessage); virtual;{ window proc - called by Windows to handle messages passed to our hidden window }...publicconstructor Create(AOwner: TComponent); override;{ create hidden window here: store handle in fHWnd}destructor Destroy; override;{ free hidden window here }...end;

And here are the implementation details:

constructor TMyClass.Create(AOwner: TComponent);
begininherited Create(AOwner);...// Create hidden window using WndMethod as window procfHWnd := AllocateHWnd(WndMethod);...
end;destructor TMyClass.Destroy;
begin...// Destroy hidden windowDeallocateHWnd(fHWnd);...inherited Destroy;
end;procedure TMyClass.WndMethod(var Msg : TMessage);
varHandled: Boolean;
begin// Assume we handle messageHandled := True;case Msg.Msg ofWM_SOMETHING: DoSomething;// Code to handle a messageWM_SOMETHINGELSE: DoSomethingElse;// Code to handle another message// Handle other messages hereelse// We didn't handle messageHandled := False;end;if Handled then// We handled message - record in message resultMsg.Result := 0else// We didn't handle message// pass to DefWindowProc and record resultMsg.Result := DefWindowProc(fHWnd, Msg.Msg,Msg.WParam, Msg.LParam);
end;

Of course, we could just use the Windows API to create a window the hard way and provide a windows procedure.

But it is more difficult to use a method as a window procedure if we do it this way.

The clever features about AllocateHWnd are that

(a) it creates the hidden window for us and

(b) it allows us to use a method, rather than a simple procedure,

as the window procedure – and a method is more useful since it has access to the class's private data.

转载于:https://www.cnblogs.com/shangdawei/p/4015006.html

How a non-windowed component can receive messages from Windows -- AllocateHWnd相关推荐

  1. not receive messages for the subscriptions setting

  2. 让一个非窗口组件(non-windowed component)可以接受来自Windows的消息

    为什么要这样做? 有时候我们需要一个非窗口组件(比如一个非继承自TWinContrl的组件)可以接受Windows消息.要接受消息就需要一个窗口句柄,但是非窗口组件却没有句柄.这篇文章将讲述怎么让一个 ...

  3. python bp神经网络 异或_【神经网络】BP算法解决XOR异或问题MATLAB版

    第一种 %% %用神经网络解决异或问题 clear clc close ms=4;%设置4个样本 a=[0 0;0 1;1 0;1 1];%设置输入向量 y=[0,1,1,0];%设置输出向量 n=2 ...

  4. #翻译NO.3# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    2.4 Message Endpoints A Message Endpoint represents the "filte ...

  5. 使用VMware桥接模式组建局域网测试MSMQ(二)

    上一篇讲了搭建VMware虚拟机实现与宿主机相互通信,环境已经就绪,现在就可以做MSMQ的分布式开发了. 本篇准备分四点介绍MSMQ: 1.MSMQ简介 2.MSMQ的安装 3.MSMQ编程开发 4. ...

  6. Amazon SNS和Amazon SQS有什么区别?

    本文翻译自:What is the difference between Amazon SNS and Amazon SQS? 我不明白何时使用SNS与SQS,为什么它们总是耦合在一起? #1楼 参考 ...

  7. Core J2EE Patterns - Service Locator--oracle官网

    原文地址:http://www.oracle.com/technetwork/java/servicelocator-137181.html Context Service lookup and cr ...

  8. MULE ESB学习笔记

    写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态.经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工 ...

  9. Mule ESB 学习笔记

    写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态.经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工 ...

  10. 从零开始搭建spring-cloud(0) --springboot与springcloud的关系

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> Spring Cloud provides tools for developers to quickly bu ...

最新文章

  1. 简明python教程在线-简明python教程
  2. 004 Android之其他控件
  3. xlsx文件打开乱码_Excel 2016 双击无法打开xlsx文件怎么办?
  4. 这届全明星,把NBA又燃回来了
  5. SpringBoot2 整合 Drools规则引擎,实现高效的业务规则
  6. chrome浏览器,开发工具一些常用快捷键
  7. COJ1183(计算表达式的值)
  8. javaWeb注册,登陆,注销功能的实现
  9. ESP32驱动LCD液晶屏选型、262K什么意思?SPI写LCD的GRAM时序、MCU液晶屏驱动IC的寄存器功能
  10. 使用wav2sbc.exe无法转换WAV文件问题
  11. 网易笔试——迷路的牛牛
  12. 更改LXDE的语言为中文
  13. 多颗微粒的阵列光镊系统设计
  14. obsidian如何修改主题
  15. 硬核观察 #612 谷歌正式推出“切换到安卓”应用
  16. “大灯”到底指近光灯还是远光灯?
  17. vue element 父传子,第一次传不过去
  18. PCB设计--AD18导入二维码避坑指南
  19. python中使用matplotlib.pyplot画函数图像
  20. 个人住房抵押贷款流程让你的贷款更轻松

热门文章

  1. QQ小程序内测邀请码内部获取群
  2. 三国志战略版新赛季:先锋斥候北定中原
  3. Elasticsearch:在华为大数据集群 从c80升级至651版本后,执行es命令修改配置失败
  4. OpenGL_Qt学习笔记之_03(平面图形的着色和旋转)
  5. 村长选举c语言程序,大村长-第一章 选举-爱阅小说网
  6. 指纹存取控制系统的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  7. 三维软件中制作动画导入Unity中使用
  8. 条件概率公式图解推导
  9. 储存卡格式化怎么恢复?给力的操作!
  10. windows无法连接到打印机_电脑、打印机常见故障处理