J
jsw0525
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾好!我需要动态创建一些btibtn控件,如果不用线程,一切可以。如果把这些代码放到线程中,则代码执行完毕,没报异常,但是看不到创建的控件!(并且整个程序退出时,报无效的窗口句柄错误)。我的代码如下(所有的代码,可以直接粘贴,编译即可)主窗口代码:var TestThread_Picture:Test_Thread;procedure TForm1.Button2Click(Sender: TObject);
begin
TestThread_Picture := Test_Thread.Create(false);
end;
线程类:unit UThread;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls;type Test_Thread = class(TThread) private { Private declarations } protected procedure Execute;
override;
public procedure showPicture(id: string);
procedure mousedown(sender:tobject;button:tmousebutton;shift:tshiftstate;x,y:integer);
procedure mousemove(sender:tobject;shift:tshiftstate;x,y:integer);
procedure mouseup(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
end;
var Btn:array[0..200] of TBitBtn;
i:integer;
flag:boolean;
x1,y1 :integer;implementationuses Unit1;procedure Test_Thread.mousedown(sender:tobject;button:tmousebutton;shift:tshiftstate;x,y:integer);
begin
if button=mbleft then
begin
flag:=true;
x1:=x;
y1:=y;
end;
end;
procedure Test_Thread.mousemove(sender:tobject;shift:tshiftstate;x,y:integer);
begin
if flag then
begin
TBitBtn(Sender).Left:=TBitBtn(Sender).Left+(x-x1);
TBitBtn(Sender).Top:=TBitBtn(Sender).Top+(y-y1);
end;
end;
procedure Test_Thread.mouseup(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
flag:=false;
end;
procedure Test_Thread.showPicture(id: string);
begin
i:=i+1;
Btn[i-1] := TBitBtn.Create(application);
Btn[i-1].Parent := form1;
Btn[i-1].Caption:='控件'+intTostr(i);
Btn[i-1].Top := 10+20*i;
Btn[i-1].Left := 20+50*i;
Btn[i-1].tag:=i;
Btn[i-1].OnMouseDown:=mousedown;
Btn[i-1].OnMouseMove:=mousemove;
Btn[i-1].OnMouseUp:=mouseup;
end;
procedure Test_Thread.Execute;
begin
{ Place thread code here } FreeOnTermiNate := True;
showPicture('');
end;
end.
var TestThread_Picture:Test_Thread;procedure TForm1.Button2Click(Sender: TObject);
begin
TestThread_Picture := Test_Thread.Create(false);
end;
所有代码可以直接运行,不要被这么长的代码吓住了,谢谢各位!
begin
TestThread_Picture := Test_Thread.Create(false);
end;
线程类:unit UThread;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls;type Test_Thread = class(TThread) private { Private declarations } protected procedure Execute;
override;
public procedure showPicture(id: string);
procedure mousedown(sender:tobject;button:tmousebutton;shift:tshiftstate;x,y:integer);
procedure mousemove(sender:tobject;shift:tshiftstate;x,y:integer);
procedure mouseup(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
end;
var Btn:array[0..200] of TBitBtn;
i:integer;
flag:boolean;
x1,y1 :integer;implementationuses Unit1;procedure Test_Thread.mousedown(sender:tobject;button:tmousebutton;shift:tshiftstate;x,y:integer);
begin
if button=mbleft then
begin
flag:=true;
x1:=x;
y1:=y;
end;
end;
procedure Test_Thread.mousemove(sender:tobject;shift:tshiftstate;x,y:integer);
begin
if flag then
begin
TBitBtn(Sender).Left:=TBitBtn(Sender).Left+(x-x1);
TBitBtn(Sender).Top:=TBitBtn(Sender).Top+(y-y1);
end;
end;
procedure Test_Thread.mouseup(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
flag:=false;
end;
procedure Test_Thread.showPicture(id: string);
begin
i:=i+1;
Btn[i-1] := TBitBtn.Create(application);
Btn[i-1].Parent := form1;
Btn[i-1].Caption:='控件'+intTostr(i);
Btn[i-1].Top := 10+20*i;
Btn[i-1].Left := 20+50*i;
Btn[i-1].tag:=i;
Btn[i-1].OnMouseDown:=mousedown;
Btn[i-1].OnMouseMove:=mousemove;
Btn[i-1].OnMouseUp:=mouseup;
end;
procedure Test_Thread.Execute;
begin
{ Place thread code here } FreeOnTermiNate := True;
showPicture('');
end;
end.
var TestThread_Picture:Test_Thread;procedure TForm1.Button2Click(Sender: TObject);
begin
TestThread_Picture := Test_Thread.Create(false);
end;
所有代码可以直接运行,不要被这么长的代码吓住了,谢谢各位!