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 ...
随机推荐
- 通过文件流stream下载文件
public ActionResult ShowLocalizedXML(int id) { string orderName = ""; string xmlString = G ...
- android去掉EditView的默认焦点问题
在EditText的父级控件中找一个,设置成 <LinearLayout android:layout_width="0dp" android:layout_height=& ...
- Codeforces Round #217 (Div. 2) c题
C. Mittens time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- ylb:子查询(嵌套子查询)和子查询(相关子查询)
ylbtech-SQL Server:SQL Server-子查询(嵌套子查询)和子查询(相关子查询) SQL Server 子查询(嵌套子查询)和子查询(相关子查询). 1,ylb:1,子查询(嵌套 ...
- 讲解HTML服务器推送相关技术知识(转)
1. 为什么需要服务器推送? 最大的优点:实时 健康知识平台重庆男科医院 重庆妇科医院适用场景:实时股票价格.商品价格.实时新闻.Twitter/weibo timeline.基于浏览器的聊天系统 2 ...
- TestNG中同一个类中执行多个test()方法如何配置testng.xml
public class IndexInfo extends BaseTesting{ private IndexPage IndexPage1;// private AddEquipmentInfo ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- c#中格式化导出Excel数据
在项目开发过程中经常会遇到数据导出Excel.如果只是导出数据就好办了.但往往用户会有各种格式要求.加粗.边框.合并单元格.汇总等功能. 以下的方法是基于Excel模版方式写入数据导出的功能.可以最大 ...
- android get uuid获取uuid
https://github.com/Paldom/UniqueDeviceID protected void getDeviceUUID(){ try { Context context = cor ...
- 第三百五十七天 how can I 坚持
502是我对你没有爱的意思吗?为什么要要这样啊,好绝情. 明天要去加班,今晚回来也好晚了,晚上回来都有点精神恍惚了. 他们要聚会,本来要想去樱木花道来,哎. 后天..什么都没学. .. .. .. 准 ...