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. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.1.系统界面报错Gnome

    1.错误信息:登录系统后,屏幕弹出几个错误对话框,无菜单.无按钮 GConf error: Failed to contact configuration server; some possible ...

  2. 小结JS中的OOP(中)

    此篇文章主要是提炼<JavaScript高级程序设计>中第六章的一些内容. 一:JS中OOP相关的概念 开始之前先总结JS中OOP相关的一些概念: 构造函数:JS中的构造函数就是普通的函数 ...

  3. [转] C#中的Dictionary的使用

    txw1958 的 原文 说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键 ...

  4. js闭包用法

    闭包 既保证了 内部函数的私有性 又可以向外公开 通过一个已有对象 向它注入属性 /** * 闭包 * 在函数中定义的函数,在外部使用 * 1.在函数内部定义的函数,在外部不能访问 */ functi ...

  5. EhCache 分布式缓存/缓存集群

    开发环境: System:Windows JavaEE Server:tomcat5.0.2.8.tomcat6 JavaSDK: jdk6+ IDE:eclipse.MyEclipse 6.6 开发 ...

  6. Hbase shell详情

    HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”.HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建.删 ...

  7. bat 批处理脚本

    目录: 1:ping多个不同服务器IP 2:每隔一段时间清一次DNS缓存 3:将一个文件夹中的所有文件,分别保存在一个新文件夹中,以保持每个文件夹一个文件 功能1:ping多个不同服务器IP 环境开通 ...

  8. C#读写文件总结

    1.使用FileStream读写文件   文件头:   using System; using System.Collections.Generic; using System.Text; using ...

  9. 序列for循环语句

    序列for循环语句 序列for循环语句允许重复遍历一组序列,而这组序列可以是任何可以重复遍历的序列,如由begin()和end()函数定义的STL序列.所有的标准容器都可用作这种序列,同时它也同样可以 ...

  10. (转载)OC学习篇之---类目的概念和使用

    上一篇文章介绍了OC中的@class关键字的使用,这一篇我们介绍一下,OC中的一个特有的亮点:类目 首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首 ...