Простой пример, отправка из дополнительного потока в основной
Отправка…
1 2 3 4 5 6 7 8 9 10 11 |
const WM_WORK_TIME = WM_USER + 1000; ... TThread.CreateAnonymousThread( procedure var s: string; begin s := DateTimeToStr(Now()); PostMessage(Main.Handle, WM_WORK_TIME, 0, LParam(PChar(s))); end).Start; ... |
Прием
1 2 3 4 5 6 7 8 9 |
const WM_WORK_TIME = WM_USER + 1000; procedure UpdateWorkTime(var aMsg: TMessage); message WM_WORK_TIME; ... procedure TMain.UpdateWorkTime(var aMsg: TMessage); begin StatusBar.Panels[1].Text := PChar(aMsg.LParam); end; ... |
Пример посложнее
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
{$R *.dfm} const // WM_STRINGMESSAGE Actions: ACTION_SETCAPTION = 0; ACTION_CHANGECAPTION = 1; constructor TStrMsgObject.Create(const aMsg: string); begin inherited Create; fMsg := aMsg; end; procedure TForm2.WMStringMessage(var Msg: TMessage); // Process WM_STRINGMESSAGE messages var mo: TStrMsgObject; begin mo := TStrMsgObject(Msg.LParam); try case Msg.WParam of ACTION_SETCAPTION: begin Caption := mo.Msg; end; ACTION_CHANGECAPTION: begin Caption := Caption + mo.Msg; end; end finally mo.Free; end; end; procedure TForm2.btnSetCaptionClick(Sender: TObject); begin System.Classes.TThread.CreateAnonymousThread( procedure begin PostMessage(Handle, WM_STRINGMESSAGE, ACTION_SETCAPTION, Integer(TStrMsgObject.Create('My new caption'))); end).Start; end; procedure TForm2.btnChangeCaptionClick(Sender: TObject); begin System.Classes.TThread.CreateAnonymousThread( procedure begin PostMessage(Handle, WM_STRINGMESSAGE, ACTION_CHANGECAPTION, Integer(TStrMsgObject.Create(' changed'))); end).Start; end; end. shareimprove this answer answered May 4 '17 at 8:42 |