当前位置: 代码迷 >> 综合 >> 【Delphi】中使用消息Messages(三)
  详细解决方案

【Delphi】中使用消息Messages(三)

热度:22   发布时间:2024-01-18 22:24:07.0

上节已经说了,FireMonkey是支持RTL消息的,VCL支持RTL消息和自身的windows消息机制,本节开始通过Demo详细介绍各自的使用方法和区别。

平台框架 可使用消息机制
FireMonkey RTL
VCL RTL、Windows消息机制(SendMessage,PostMessage)

每个消息机制对应的delphi单元:

RTL System.Messging
Windows消息机制 Winapi.Messages

如果使用 RTL 请引用 System.Messging 单元,如果使用Windows消息机制,请引用Winapi.Messages单元。如果在一个单元(Unit)中,两种机制都需要使用,请在对应的消息变量定义前写上对应的单元名称,否则会引起混淆,导致编译错误。

示例一:仅使用 RTL 机制

usesSystem.Messaging, // RTL 消息单元....;//直接使用 TMessage 不会出现问题
procedure OnMessageManager(const Sender: TObject; const M: TMessage);

示例二:仅使用windows消息机制

usesWinapi.Messages, // windows 消息单元....;//直接使用 TMessage 不会出现问题
procedure TForm_Message_VCL.OnWinMessage(var Message);
varPStudent : ^TStudent;
beginPStudent := Pointer( TMessage(Message).WParam );....
end;

示例三:两种机制都使用

usesSystem.Messaging, // RTL 消息单元 Winapi.Messages,  // windows 消息单元....;//System.Messaging 中的消息响应事件,TMessage需要带上前缀System.Messaging procedure OnMessageManager(const Sender: TObject; const M: System.Messaging.TMessage);//windows消息机制,不可直接使用 TMessage ,需要带上单元前缀Winapi.Messages
procedure TForm_Message_VCL.OnWinMessage(var Message);
varPStudent : ^TStudent;
beginPStudent := Pointer( Winapi.Messages.TMessage(Message).WParam );....
end;

这样在一个单元中就可以同时使用两种消息机制,不过一般情况下也不会同时使用,另外Winapi.Messages也只对VCL有效。

下一节:【Delphi】中使用消息Messages(四)??????????????RTL 消息演示Demo

  相关解决方案