Основной поток
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 |
unit uMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Buttons, uApiTestThread; const WM_DATA_UPDATE = WM_APP + 1000; type TMain = class(TForm) pTop: TPanel; bTest: TBitBtn; mAllTests: TMemo; mErrors: TMemo; procedure bTestClick(Sender: TObject); private procedure HandleProc(var aMsg: TMessage); message WM_DATA_UPDATE; end; var Main: TMain; implementation {$R *.dfm} procedure TMain.bTestClick(Sender: TObject); var t: TApiTestThread; begin t := TApiTestThread.Create(true); t.MainHandle := Self.Handle; t.FreeOnTerminate := true; t.Start; end; procedure TMain.HandleProc(var aMsg: TMessage); var txt: PWideChar; begin txt := PWideChar(aMsg.lParam); mAllTests.Lines.Add(aMsg.WParam.ToString()); mErrors.Lines.Add(txt); end; 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 |
unit uApiTestThread; interface uses System.Classes, Winapi.Messages, Winapi.Windows; const WM_DATA_UPDATE = WM_APP + 1000; type TApiTestThread = class(TThread) private FMainHandle: Hwnd; procedure Test; procedure SetMainHandle(const Value: Hwnd); public property MainHandle: Hwnd read FMainHandle write SetMainHandle; protected procedure Execute; override; end; implementation { TApiTestThread } procedure TApiTestThread.SetMainHandle(const Value: Hwnd); begin FMainHandle := Value; end; procedure TApiTestThread.Test(); var h: hwnd; s: string; begin h := FMainHandle; s := 'Привет'; Sendmessage(h, WM_DATA_UPDATE, 20, DWORD(PChar(s))); end; procedure TApiTestThread.Execute; begin Test(); end; end. |