上节已经说了,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 机制
uses
System.Messaging, // RTL 消息单元
....;
//直接使用 TMessage 不会出现问题
procedure OnMessageManager(const Sender: TObject; const M: TMessage);
示例二:仅使用windows消息机制
uses
Winapi.Messages, // windows 消息单元
....;
//直接使用 TMessage 不会出现问题
procedure TForm_Message_VCL.OnWinMessage(var Message);
var
PStudent : ^TStudent;
begin
PStudent := Pointer( TMessage(Message).WParam );
....
end;
示例三:两种机制都使用
uses
System.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);
var
PStudent : ^TStudent;
begin
PStudent := Pointer( Winapi.Messages.TMessage(Message).WParam );
....
end;
这样在一个单元中就可以同时使用两种消息机制,不过一般情况下也不会同时使用,另外Winapi.Messages也只对VCL有效。
下一节:【Delphi】中使用消息Messages(四)RTL 消息演示Demo