Trapping Messages Sent to an Application
http://www.delphicorner.f9.co.uk/articles/apps7.htm
Trapping Messages Sent to an Application
I wrote code for the OnMessage event handler of Application object
to trap all Windows messages sent to my application,
but it doesn't seem to fire on all messages.
Is there a way to trap all messages sent to my application?
There sure is. And the answer to this "problem" is amazingly simple.
But before I go into trapping messages at the application level,
I should probably discuss some mechanics.
TApplication's "Hidden" Window
It's not a commonly known fact that the default Application object creates a hidden window
when your application is started.
But you can seen evidence of this by creating a new application saving it,
then running it (make sure you don't rename anything -
just keep the main form as "Form1" and the project as "Project1).
When you run the application, you'll notice that the caption bar for your main form says,
"Form1" while the icon displayed on the task bar says "Project1."
That icon represents the application's hidden window,
and it affects your program in many ways,
especially when you're trying to handle messages sent to your application.
Delphi surfaces the OnMessage event for the Application object.
The OnMessage event handler is "supposed" to allow you trap every message sent to your application.
But there's a problem with this:
OnMessage will only fire when there's something in the Application object's message queue.
These messages are typically window management messages such as WM_PAINT
or messages sent to the application from Windows through PostMessage,
Broadcast or SystemMessage .
However, messages sent directly to a window using SendMessage
bypass the Application object's message queue,
so OnMessage doesn't fire for those types of situations.
Some of you more familiar with handling windows messages
might think that a solution to the problem above
might be to override the WndProc method for the Application object.
Unfortunately, that's not possible because TApplication's WndProc method is not only private,
it's also declared as a static method which means it's not overrideable.
So it's not only invisible, you can't create a TApplication subclass
to override WndProc (not that you'd want either).
But that doesn't mean that you can't get to the WndProc method using alternative means.
"Hooking" All Messages
Even though WndProc is all but closed to direct subclassing,
TApplication does include a method called HookMainWindow
that allows you to insert your own message handler
at the top of WndProc to intercept messages sent to your application
before they're handled by the Application object.
This is convenient for all developers, and solves the problem
of trapping any message sent to your application.
HookMainWindow is declared under TApplication as follows:
procedure HookMainWindow(Hook : TWindowHook);
Notice that HookMainWindow takes one parameter,
Hook of type TWindowHook.
TWindowHook is a method pointer type that's defined like so:
type
TWindowHook = function(var Message : TMessage) : Boolean of object;
Since TWindowHook is a method pointer, you can define your own method as the hook function
as long as it follows the nomenclature defined for TWindowHook.
Notice that the return value of the function is of type Boolean.
This is the equivalent of the "Handled" parameter of OnMessage.
If your function handles a particular message, you'd return true.
This will be passed back to the Application's WndProc and
message processing for that message will be terminated.
Otherwise, you'd return False. Here's an example method:
function TForm1.AppHookFunc(var Message : TMessage) : Boolean;
begin
Result := False; //I just do this by default
if Message.Msg = WM_<SomethingOrOther> then begin
...DoSomething...
Result := True;
end;
end;
Okay, now that we've set up everything,
we need to make the application hook the messages.
This can be done in the main form's OnCreate method:
function TForm1.FormCreate(Sender : TObject);
begin
HookMainWindow(AppHookFunc);
end;
I should mention that you need to clear the hook using,
you guessed it, UnHookMainWindow,
after you're done using it, and this can be done
in the OnDestroy for the main form:
function TForm1.FormDestroy(Sender : TObject);
begin
UnHookMainWindow(AppHookFunc);
end;
Okay, disgustingly simple.
But I feel the best things in life are those that give maximum satisfaction
for the least amount of cost (please don't read ANYTHING into that <G>).
So, now you've got the tools to create your own message "hooker" (sorry, had to do that at least once).
Until next time...
Trapping Messages Sent to an Application的更多相关文章
- Sending messages to non-windowed applications -- AllocateHWnd, DeallocateHWnd
http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm Page 1: How Delphi dispatches messages in ...
- Application.HookMainWindow完全替代了原来的窗口过程(但是好像也会继续传递)
unit HookMain; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialo ...
- 对发给Application.Handle消息的三次执行(拦截)消息的过程
unit Main; interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms ...
- VCL -- Understanding the Message-Handling System
Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...
- windows消息机制详解(转载)
消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...
- 将asp.net core站点发布到IIS上遇到的问题
今天第一次将整个 asp.net core 站点发布到 IIS 上,以前都是发布到 Linux 服务器上. 开始使用 dotnet publish -c release 命令发布,用浏览器访问站点时出 ...
- Competing Consumers Pattern (竞争消费者模式)
Enable multiple concurrent consumers to process messages received on the same messaging channel. Thi ...
- Logging configuration
The Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4 ...
- django+nginx+xshell简易日志查询,接上<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>
纠正一下之前在<关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思>中说到的PHP+MySQL太慢,这里只是说我技术不好,没 ...
随机推荐
- jQuery autoComplete 样式
前提:使用了jQuery-ui 官网:http://jqueryui.com/autocomplete/ /*** autocomplete ***/ .ui-widget-content { bac ...
- c排序算法大全
排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法 对算法本身的速度要求很高. 而一般我们所谓的算法的性能主要是指算法的复杂度,一般用O方法来表示.在后面将给出详细的说明.& ...
- fscanf的返回值未成功输入的元素个数 .xml
pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...
- SublimeText3 初探(工欲善其事,必先利其器)
告别2015,迎来了2016.祝看到这篇博客的兄弟们都猴年发财,人生走上另一个高度. 新年新气象,猴年我会常常来更新自己的博客,希望能接触到更多的园友. sublime text3 和atom 各有各 ...
- VS2013 调试MVC源码[MVC5.2.3+MVC4Web项目]
1.目前MVC源码版本为5.2.3,下回来后用VS2013打开,把System.Web.Mvc项目的版本号改为4.0.0.1 2.在解决方案下建一个MVC4项目,.NET选4.5,修改根目录以及Vie ...
- .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)
1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/
- 谷歌眼镜--UI指南
1>使用玻璃HTML模板 不是所有的内容都在几行文字来表达.有时候你需要结构化的内容发送到用户的时间轴,或者你需要控制对格式.为了适应这种情况,镜像API提供了一个 HTML 时间表的项目,接受 ...
- 非对称认证方式 可以用在 asp.net webapi 的安全机制里面
//Client端调用 static void Main(string[] args) { string publicKey = "DpLMCOihcYI2i6DaMbso9Dzo1miy7 ...
- dom 绘制正方形
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Openstack之Swift架构(Cloud Storage)
Swift是OpenStack的子项目之一,也称为对象储存,适用于储存永久类型的静态数据,例如:虚拟机镜像文件.图片.存档备份等 复制的三个副本如何联系在一起? 让我们用一些具体场景和介绍一些组件,来 ...