举个简单的例子拉,希望对你有帮助!unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TMyThread = class(TThread)
private
child : TComponent; public
procedure draw;
constructor Create(parent : TComponent);
procedure Execute; override; end;var
Form1: TForm1;
implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(parent: TComponent);
begin
child := parent;
inherited Create(false);
end;procedure TMyThread.draw;
begin
if (child is TEdit) then
begin
(child as TEdit).Text := 'OK';
end
else if(child is TImage) then
begin
(child as TImage).Canvas.Brush.Color := clBlue;
(child as TImage).Canvas.FillRect(rect(0,0,100,100));
end;
end;procedure TMyThread.Execute;
begin
inherited;
synchronize(draw);
if Terminated then Exit;
end;procedure TForm1.Button1Click(Sender: TObject);begin
TMythread.Create(Edit1);
TMythread.Create(image1);
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TMyThread = class(TThread)
private
child : TComponent; public
procedure draw;
constructor Create(parent : TComponent);
procedure Execute; override; end;var
Form1: TForm1;
implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(parent: TComponent);
begin
child := parent;
inherited Create(false);
end;procedure TMyThread.draw;
begin
if (child is TEdit) then
begin
(child as TEdit).Text := 'OK';
end
else if(child is TImage) then
begin
(child as TImage).Canvas.Brush.Color := clBlue;
(child as TImage).Canvas.FillRect(rect(0,0,100,100));
end;
end;procedure TMyThread.Execute;
begin
inherited;
synchronize(draw);
if Terminated then Exit;
end;procedure TForm1.Button1Click(Sender: TObject);begin
TMythread.Create(Edit1);
TMythread.Create(image1);
end;
end.
我不知道你到底要干嘛?
procedure TMyThread.draw;
var
iLen,i : integer;
Source : TImage;
begin
if (child is TEdit) then
begin
(child as TEdit).Text := 'OK';
end
else if(child is TImage) then
begin
(child as TImage).Canvas.Brush.Color := clBlue;
(child as TImage).Canvas.FillRect(rect(0,0,100,100));
end
else if (child is TForm) then
begin iLen := child.ComponentCount;
for i := 0 to iLen - 1 do
if (child.Components[i] is TImage) then
begin
Source := TImage(Child .Components[i]);
Source.Canvas.Brush.Color := clBlue;
Source.Canvas.FillRect(rect(0,0,100,100));
end
end;
end;
var
qry: TQueryqry := TQuery.Create(nil);
.....
qry.Free;
如果线程类在另一文件,Uses Unit1就可以了
不过要注意同步问题
to hellion:那还要将一堆属性初始化。
to pathe:比如,我在线程中访问数据库,访问数据库的控件是主form上的query,结果返回显示在主form上的edit,状态信息显示在主form的statusbar...
因为若楼上所说,只要调用vcl用同步就行了!
你的query不变,而结果是等现成完成后,一次刷新(可以在Form做).
另,直接引用是可以的,只不过程序结构不好,还要避免循环!
如楼上所说,只要用到vcl用同步就行了!
直接引用是可以的,但程序结构不好,还要避免循环引用(uses分别放到不同地方),同时当然也避免不了同步的问题!