unit Unit1;interfaceuses
Windows,unit2, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure MMTIMER(var msg:tmessage);message mm_timer;
public
MyThread:TMythread;
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
mythread.Suspend;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
mythread:=tmythread.Create(true);
mythread.WindowHandle:=handle;
mythread.Interval:=1000;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
mythread.Resume;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
mythread.FreeOnTerminate:=true;
mythread.Terminate;
end;procedure TForm1.MMTIMER(var msg: tmessage);
begin
showmessage(timetostr(time));
end;end.
/////////////////////////////////
unit Unit2;interfaceuses
Classes,sysutils,windows,messages;
const
MM_TIMER=WM_USER+200;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
WindowHandle:cardinal;
Interval:integer; end;implementation{ TMyThread }procedure TMyThread.Execute;
begin
while not self.Terminated do
begin
sleep(interval);
postmessage(windowhandle,mm_timer,0,0);
end;
end;end.
Windows,unit2, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure MMTIMER(var msg:tmessage);message mm_timer;
public
MyThread:TMythread;
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
mythread.Suspend;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
mythread:=tmythread.Create(true);
mythread.WindowHandle:=handle;
mythread.Interval:=1000;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
mythread.Resume;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
mythread.FreeOnTerminate:=true;
mythread.Terminate;
end;procedure TForm1.MMTIMER(var msg: tmessage);
begin
showmessage(timetostr(time));
end;end.
/////////////////////////////////
unit Unit2;interfaceuses
Classes,sysutils,windows,messages;
const
MM_TIMER=WM_USER+200;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
WindowHandle:cardinal;
Interval:integer; end;implementation{ TMyThread }procedure TMyThread.Execute;
begin
while not self.Terminated do
begin
sleep(interval);
postmessage(windowhandle,mm_timer,0,0);
end;
end;end.
像不像TTimer控件?
像不像TTimer控件?
像不像TTimer控件?
postmessage不管这些,按照自己的频率发送消息。你自己试验一下就知道了:)