我也试了一下,但是用postthreadmessage时,返回的错误信息为:窗口不是子窗口,大侠
给看看:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
wm_usermsg=wm_user+20;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ExitThread(Sender:Tobject);
private
{ Private declarations }
public
{ Public declarations }
end;
msgThread=class(tthread)
protected
procedure execute;override;
public
constructor Create(CreateSuspended: Boolean);
end;
var
Form1: TForm1;
bExit:boolean=false;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
{ msgThread }
constructor msgThread.Create(CreateSuspended: Boolean);
begin
inherited;
freeonterminate:= true;
messagebox(0,pchar('Thread has create'),'look',0);
end;
procedure msgThread.execute;
var
msg: TMsg;
dwHandleSignaled
WORD;
begin
OnTerminate :=form1.ExitThread;
while true do
begin
Application.ProcessMessages ;
If peekmessage(msg, 0, 0, 0, PM_REMOVE) then
begin
case msg.message of
wm_usermsg:
begin
messagebox(0,pchar('Message has received!'),'look',0);
end;
end;
end;
if bExit then
break;
end;
messagebox(0,pchar('Exit'),'Info',0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tmpThread:msgThread;
begin
tmpThread:=msgthread.Create(false);
if postthreadmessage(tmpThread.ThreadID,wm_usermsg,0,0) then
begin
showmessage('post message success!');
end;
showmessage(syserrormessage(getlasterror));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
bExit:=true;
end;
procedure TForm1.ExitThread(Sender:Tobject);
begin
showmessage('This is thread exit!');
end;
end.