How a non-windowed component can receive messages from Windows -- AllocateHWnd
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)
private
fHWnd: HWND;
{ field to store the window handle }
...
protected
procedure WndMethod(var Msg: TMessage); virtual;
{ window proc - called by Windows to handle
messages passed to our hidden window }
...
public
constructor 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);
begin
inherited Create(AOwner);
...
// Create hidden window using WndMethod as window proc
fHWnd := AllocateHWnd(WndMethod);
...
end; destructor TMyClass.Destroy;
begin
...
// Destroy hidden window
DeallocateHWnd(fHWnd);
...
inherited Destroy;
end; procedure TMyClass.WndMethod(var Msg : TMessage);
var
Handled: Boolean;
begin
// Assume we handle message
Handled := True;
case Msg.Msg of
WM_SOMETHING: DoSomething;
// Code to handle a message
WM_SOMETHINGELSE: DoSomethingElse;
// Code to handle another message
// Handle other messages here
else
// We didn't handle message
Handled := False;
end;
if Handled then
// We handled message - record in message result
Msg.Result :=
else
// We didn't handle message
// pass to DefWindowProc and record result
Msg.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.
How a non-windowed component can receive messages from Windows -- AllocateHWnd的更多相关文章
- How a non-windowed component can receive messages from Windows
Why do it? Sometimes we need a non-windowed component (i.e. one that isn't derived fromTWinControl) ...
- [React Intl] Use a react-intl Higher Order Component to format messages
In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...
- 让一个非窗口组件(non-windowed component)可以接受来自Windows的消息
为什么要这样做? 有时候我们需要一个非窗口组件(比如一个非继承自TWinContrl的组件)可以接受Windows消息.要接受消息就需要一个窗口句柄,但是非窗口组件却没有句柄.这篇文章将讲述怎么让一个 ...
- WebWorker SharedWorker ServiceWorker
WebWorker 是什么? 为 JavaScript 引入线程技术 不必再用 setTimeout().setInterval().XMLHttpRequest 来模拟并行 Worker 利用类似线 ...
- Receive Windows Messages In Your Custom Delphi Class - NonWindowed Control - AllocateHWnd
http://delphi.about.com/od/windowsshellapi/a/receive-windows-messages-in-custom-delphi-class-nonwind ...
- SSIS Component的ValidateExternalMetadata属性
ValidateExternalMetadata Property Indicates whether the component validates its column metadata agai ...
- How PhoneGap & Titanium Works
转载自 http://www.appcelerator.com/blog/2012/05/comparing-titanium-and-phonegap/ How PhoneGap Works As ...
- Microsoft Win32 to Microsoft .NET Framework API Map
Microsoft Win32 to Microsoft .NET Framework API Map .NET Development (General) Technical Articles ...
- .net Framework Class Library(FCL)
from:http://msdn.microsoft.com/en-us/library/ms229335.aspx 我们平时在VS.net里引用的那些类库就是从这里来的 The .NET Frame ...
随机推荐
- ylb:创建数据库、表,对表的增查改删语句
ylbtech-SQL Server:SQL Server-创建数据库.表,对表的增查改删语句 SQL Server 创建数据库.表,对表的增查改删语句. 1,ylb:创建数据库.表,对表的增查改删语 ...
- T-SQL:SQL Server-数据开发(经典)
ylbtech-SQL Server-Doc-Help:SQL Server-数据开发(经典) SQL Server 数据开发(经典). 1,数据开发(经典) 返回顶部 1.按姓氏笔画排序: Sele ...
- free-jqGrid
PM> Install-Package free-jqGrid jqGrid 是一个用来显示网格数据的jQuery插件,通过使用jqGrid可以轻松实现前端页面与后台数据的ajax异步通信.文档 ...
- Raspberry Pi3 ~ 安装samba服务
文章转载自此博文 1. sudo apt-get install samba 如果出现错误提示,则需要先执行sudo apt-get update,再重新执行sudo apt-get install ...
- just test Gson
just test Gson code package com.qilin.test; import com.google.gson.Gson; import org.apache.commons.l ...
- PHPSTORM 与 Xdebug 配合调试
基本的配置可以参考网上的文档, 浏览器中装插件(xdebug)或直接在请求中加上如下的参数也可启动调试 ?XDEBUG_SESSION_START=PHPSTORM
- VS常用技巧
VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...
- 9段高效率开发PHP程序的代码
php是世界上最好的语言 在php网站开发中,大家都希望能够快速的进行程序开发,如果有能直接使用的代码片段,提高开发效率,那将是起飞的感觉.今天由杭州php工程师送出福利来了,以下9段高效率开发PHP ...
- gVIM 简洁配置 in Windows
原文链接:http://www.errdev.com/post/2/ 捣鼓了一段时间的VIM,神器终归是神器,果然编码效率提升了许多,当然还需要很多插件来配合.自己装插件很麻烦,还要有Vundle这个 ...
- 在Spring MVC中使用注解的方式校验RequestParams
概述 Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...