VCL -- Understanding the Message-Handling System
Understanding the Message-Handling System
http://docwiki.embarcadero.com/RADStudio/XE7/en/Understanding_the_Message-Handling_System
All VCL classes have a built-in mechanism for handling messages, called message-handling methods or message handlers.
The basic idea of message handlers is that the class receives messages of some sort and dispatches them,
calling one of a set of specified methods depending on the message received.
If no specific method exists for a particular message, there is a default handler.
The following diagram shows the message-dispatch system:
Dispatching Messages
http://docwiki.embarcadero.com/RADStudio/XE7/en/Dispatching_Messages
When an application creates a window, it registers a window procedure with the Windows kernel.
The window procedure is the routine that handles messages for the window.
Traditionally, the window procedure contains a huge case statement with entries for each message the window has to handle.
Keep in mind that "window" in this sense means just about anything on the screen:
each window, each control, and so on.
Every time you create a new type of window,
you have to create a complete window procedure.
The VCL simplifies message dispatching in several ways:
- Each component inherits a complete message-dispatching system.
- The dispatch system has default handling.
You define handlers only for messages you need to respond to specially. - You can modify small parts of the message handling and rely on inherited methods for most processing.
The greatest benefit of this message dispatch system is that you can safely send any message to any component at any time.
If the component does not have a handler defined for the message, the default handling takes care of it, usually by ignoring the message.
Tracing the flow of messages
The VCL registers a method called MainWndProc as the window procedure for each type of component in an application.
MainWndProc contains an exception-handling block, passing the message structure from Windows to a virtual method called WndProc
and handling any exceptions by calling the application class's HandleException method.
MainWndProc is a nonvirtual method that contains no special handling for any particular messages.
Customizations take place in WndProc, since each component type can override the method to suit its particular needs.
WndProc methods check for any special conditions that affect their processing so they can "trap" unwanted messages.
For example, while being dragged, components ignore keyboard events,
so the WndProc method of TWinControl passes along keyboard events only if the component is not being dragged.
Ultimately, WndProc calls Dispatch, a nonvirtual method inherited from TObject,
which determines which method to call to handle the message.
Dispatch uses the Msg field of the message structure to determine how to dispatch a particular message.
If the component defines a handler for that particular message, Dispatch calls the method.
If the component does not define a handler for that message, Dispatch calls DefaultHandler.
The WndProc Method
http://docwiki.embarcadero.com/RADStudio/XE7/en/The_WndProc_Method
WndProc is the default Windows message handling function for a given control,
and the first method that receives messages on a form.
The WndProc method can be overridden in order to implement specific message responses.
WndProc passes any unhandled messages to the Dispatch method.
VCL controls have a property called WindowProc that points to the WndProc method.
This property can be used to replace or subclass the window procedure.
Before assigning a new value to WindowProc, the original value should be stored.
After completing any specialized message handling, call the original WindowProc
to make sure the normal message processing works as expected.
Note: If you are a component writer customizing the window procedure for a descendent class, y
ou should override theWndProc method instead of replacing or subclassing it.
Note: When overriding WndProc to provide specialized responses to messages,
be sure to call the inherited WndProc method at the end,
in order to dispatch any unhandled messages.
Trapping Messages
http://docwiki.embarcadero.com/RADStudio/XE7/en/Trapping_Messages
Under some circumstances, you might want your components to ignore messages.
That is, you want to keep the component from dispatching the message to its handler.
To trap a message, you override the virtual method WndProc.
For VCL components, the WndProc method screens messages
before passing them to the Dispatch method,
which in turn determines which method gets to handle the message.
By overriding WndProc, your component gets a chance to filter out messages before dispatching them.
An override of WndProc for a control derived from TWinControl looks like this:
procedure TMyControl.WndProc(var Message: TMessage);
begin
{ tests to determine whether to continue processing } inherited WndProc(Message);
end;
The TControl component defines entire ranges of mouse messages that it filters when a user is dragging and dropping controls.
Overriding WndProc helps this in two ways:
- It can filter ranges of messages instead of having to specify handlers for each one.
- It can preclude dispatching the message at all, so the handlers are never called.
VCL -- Understanding the Message-Handling System的更多相关文章
- Android Message Handling Mechanism
转自:http://solarex.github.io/blog/2015/09/22/android-message-handling-mechanism/ Android is a message ...
- Understanding the WPF Layout System
Many people don't understand how the WPF layout system works, or how that knowledge can help them in ...
- Android Message handling (based on KK4.4)
一.几个关键概念 1.MessageQueue:是一种数据结构,见名知义,就是一个消息队列.存放消息的地方.每个线程最多仅仅能够拥有一个MessageQueue数据结构. 创建一个线程的时候,并不会自 ...
- 7.4 GRASP原则四:控制器 Controller
4.GRASP原则四:控制器 Controller What first object beyond the UI layer receives and co-ordinates (control ...
- Meandering Through the Maze of MFC Message and Command Routing MFC消息路由机制分析
Meandering Through the Maze of MFC Message and Command Routing Paul DiLascia Paul DiLascia is a free ...
- TAxThread - Inter thread message based communication - Delphi
http://www.cybletter.com/index.php?id=3 http://www.cybletter.com/index.php?id=30 Source Code http:// ...
- System IPC 与Posix IPC(semaphore信号灯)
POSIX下IPC主要包括三种: posix message queue posix semaphores posix shared memory sysytem v IPC包括: system v ...
- Exception Handling Considered Harmful
异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...
- System中记录体函数命名怪异
//1019unit System; 中发现记录体函数命名怪异//乍一看,很怪异,其实是结构体里面 的变量后面直接写 函数类型了.不像传统先定义T***Event = procedure(S ...
随机推荐
- 使用jQuery Mobile实现新闻浏览器(3)
在本教程的前两篇文章中,笔者分别向大家介绍了使用jQuery Mobile框架如何去设计手机新闻浏览器,其中实现了一个WEB版本的新闻浏览器,在本教程的最后一篇中,将讲解如何将已实现的web版本的新闻 ...
- 浅谈MySQL Replication(复制)基本原理
1.MySQL Replication复制进程MySQL的复制(replication)是一个异步的复制,从一个MySQL instace(称之为Master)复制到另一个MySQL instance ...
- myeclipse 10 优化
一.Myeclipse10修改字体 MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...
- Yii系列教程(四):使用Memcache保存会话
1环境准备 安装Memcached服务端: yum -y installmemcached.x86_64 安装PHP-Memcache扩展: yum -y installphp-pecl-memcac ...
- 猎豹使用AI RoboForm填表
最近,Chrome同步书签好慢,另外因为工作的原因,chrome还是用来做工作的事情,自己的事情准备换到猎豹,但是一个重要的问题是强大的自动填表工具AI RoboForm没有整合到猎豹的地方: 搜索了 ...
- Container View Controller
有时候,我们的Controler中包含有另一个controler view的view时,可以使用这种方式. https://developer.apple.com/library/ios/featur ...
- js运动 运动效果留言本
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 全面理解Linux输入输出重定向
全面理解Linux输入输出重定向 本教程通过视频方式讲解shell操作,理解感念,教程通俗易懂,比起看一大堆文档要舒服的多.本次教程主要讲解 Linux Shell 中支持输入输出重定向,用符号&l ...
- Java 类型信息
<Thinking in Java 4th>第14章 类型信息 运行时类型信息(Run-Time Type Identification)使得你可以在程序运行时发现和使用类型信息. 14. ...
- GWT+CodeTemplate+TableCreate快速开发
刚进一家新公司,公司表示让我们几个新人写页面联系熟悉 怎么快速开发,进入正题: 1.根据设计规范设计页面excel 2.CodeTemplate根据excel生成属性类和对应方法(文本框,下拉框等等单 ...