请教 线程向窗体发消息(50分)

  • 主题发起人 hengheng
  • 开始时间
H

hengheng

Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个程序启动时启动一个线程,当线程接收到响应后,向某一窗体发送一消息,让它的某一控件刷新,
关于消息的发送与响应,清高手指教,就50分了,有例子才好!
 
线程单元:
unit Unit2;
interface
uses
Windows, Messages, Classes;
const
//自军定义消息;
MSG_MYMSG = WM_USER + 100;
type
TMyThread = class(TThread)
private
FMainHandle: THandle;
//调用本线程的主线程句柄;
protected
procedure Execute;
override;
public
constructor Create(AHandle: THandle);
end;

implementation
{ TMyThread }
constructor TMyThread.Create(AHandle: THandle);
begin
FMainHandle := AHandle;
inherited Create(True);
end;

procedure TMyThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate := True;
while not Terminateddo
begin
//这里放你的处理;
if FMainHandle <> 0 then
//反馈进度;
PostMessage(FMainHandle, MSG_MYMSG, 0, 0);
end;
end;

end.

窗体单元:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2;
type
TForm1 = class(TForm)
private
protected
procedure WndProc(var Message: TMessage);
override;
public
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
{ TForm1 }
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = MSG_MYMSG then
begin
//在这里处理消息;
end
else
inherited;
end;

end.
 
接受答案了.
 
顶部