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的更多相关文章

  1. Android Message Handling Mechanism

    转自:http://solarex.github.io/blog/2015/09/22/android-message-handling-mechanism/ Android is a message ...

  2. Understanding the WPF Layout System

    Many people don't understand how the WPF layout system works, or how that knowledge can help them in ...

  3. Android Message handling (based on KK4.4)

    一.几个关键概念 1.MessageQueue:是一种数据结构,见名知义,就是一个消息队列.存放消息的地方.每个线程最多仅仅能够拥有一个MessageQueue数据结构. 创建一个线程的时候,并不会自 ...

  4. 7.4 GRASP原则四:控制器 Controller

    4.GRASP原则四:控制器 Controller  What first object beyond the UI layer receives and co-ordinates (control ...

  5. 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 ...

  6. 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:// ...

  7. System IPC 与Posix IPC(semaphore信号灯)

    POSIX下IPC主要包括三种: posix message queue posix semaphores posix shared memory sysytem v IPC包括: system v ...

  8. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  9. System中记录体函数命名怪异

    //1019unit System; 中发现记录体函数命名怪异//乍一看,很怪异,其实是结构体里面 的变量后面直接写 函数类型了.不像传统先定义T***Event      = procedure(S ...

随机推荐

  1. windows 自动关机命令

    Windows 的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中.如果想让Windows 2000也实现同样的效果,可以把Shutdown.exe复制到系统 ...

  2. 讲解HTML服务器推送相关技术知识(转)

    1. 为什么需要服务器推送? 最大的优点:实时 健康知识平台重庆男科医院 重庆妇科医院适用场景:实时股票价格.商品价格.实时新闻.Twitter/weibo timeline.基于浏览器的聊天系统 2 ...

  3. OpenERP 7.0 中文报表PDF乱码(WindowsXP)

    OpenERP默认安装输出的PDF中文报表都是一些方块: 此问题可以通过oecn_base_fonts模块解决: 更多关于oecn_base_fonts的信息请参考: 1. OpenERPv7.0 中 ...

  4. selenium中处理不带ID的弹出窗口

    在selenium中虽然有selectWindow(String windowID) 方法,但是对于一些窗口很难取得其window ID的话,如果开发人员没有在html代码中提供ID ,或者getAl ...

  5. ajax给全局变量赋值问题

    ajax给全局变量赋值问题 今天在做项目时,遇到了一个问题.我用的是ajax,要在$.ajax({里面给一个全局变量赋值,结果死活赋值不上,纠结了好半天,后来上网查了查,才知道,ajax默认是异步请求 ...

  6. Name-based virtual servers 给予名称的虚拟服务

    nginx first decides which server should process the request. Let’s start with a simple configuration ...

  7. 转 AI教程 logo

    版权申明:本文原创作者飞屋工作室,感谢飞屋工作室的原创分享! 这篇AI制作标志教程是一个非常实用的教程.通过这个教程飞特的朋友们将会学习到AI制作标志的流程和标志的创作思路.非常实用.推荐过来和飞特的 ...

  8. Linux-sed用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命 ...

  9. linux库文件编写入门(笔记)

    linux库文件的编写 作者: laomai地址: http://blog.csdn.net/laomai 本文主要参考了如下资料⑴hcj写的"Linux静态/动态链接库的创建和使用&quo ...

  10. Hadoop概述

    本章内容 什么是Hadoop Hadoop项目及其结构 Hadoop的体系结构 Hadoop与分布式开发 Hadoop计算模型—MapReduce Hadoop的数据管理 小结 1.1 什么是Hado ...