unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; TMsgRecord = record MsgID1: Integer; MsgID2: Integer; MsgName: string[15]; MsgDesc: string[27]; end; TMsgHandleClass = class private procedure SM_Message1(var MsgRec: TMsgRecord); message 2000; procedure SM_Message2(var MsgRec: TMsgRecord); message 2001; procedure SM_Message3(var MsgRec: TMsgRecord); message 2002; public procedure DefaultHandler(var Message); override; end; var Form1: TForm1; MsgObject: TMsgHandleClass; implementation {$R *.dfm} { TMsgHandleClass } procedure TMsgHandleClass.DefaultHandler(var Message); begin inherited; ShowMessage(‘DefaultHandler: ‘); end; procedure TMsgHandleClass.SM_Message1(var MsgRec: TMsgRecord); begin ShowMessage(‘2000‘); end; procedure TMsgHandleClass.SM_Message2(var MsgRec: TMsgRecord); begin ShowMessage(‘2001‘); end; procedure TMsgHandleClass.SM_Message3(var MsgRec: TMsgRecord); begin ShowMessage(‘2002‘); end; procedure TForm1.Button1Click(Sender: TObject); var MyMsg: TMsgRecord; begin FillChar(MyMsg, SizeOf(TMsgRecord), 0); MyMsg.MsgID1 := 2001; MyMsg.MsgID2 := 2000; MyMsg.MsgName := ‘2000消息‘; MyMsg.MsgDesc := ‘这是消息号为2000的消息‘; MsgObject.Dispatch(MyMsg); end; procedure TForm1.FormCreate(Sender: TObject); begin MsgObject := TMsgHandleClass.Create; end; procedure TForm1.FormDestroy(Sender: TObject); begin MsgObject.Free; MsgObject := nil; end; end.
时间: 2024-11-14 13:05:54