在线程中动态创建BitBtn控件问题异常,请高手请进!(100)

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;
所有代码可以直接运行,不要被这么长的代码吓住了,谢谢各位!
 
代码自行完毕了但是看不到动态创建的控件,郁闷中!~~~~
 
线程中是没有消息的,另外你的线程showPicture('');后马上就结束了!1.事件不能在线程中2.控件只能在Syntho()中建立3.线程不能立即结束!
 
to:wql谢谢你能不能在我代码上给修改一下呀多谢我自己搞了很久了搞不好
 
我这里如果在创建控件后,进行消息分发,就可以看到新创建的控件了,也可以进行正常的使用。 while (GetMessage(msg, 0, 0, 0))do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
但是,在退出应用程序时就会报错,因为创建的控件资源没有释放。释放不了,这又是因为用了while (GetMessage(msg, 0, 0, 0))do
这是一个死循环,所有的消息都进入了这里,所以释放不了。这个问题如何解决,谢谢大家了
 
有没有办法,让GetMessage(msg, 0, 0, 0))do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
先停止,然后去释放资源,再退出程序?????
 
这里面高手云集,不会没人知道吧?各位大虾,讨论一下落我在线等待,分数马上发放谢谢
 
Form1在主线程。你创建的在另外的线程 然而又摆在Form1上。脑子进水了吗?
 
to:地质灾害救星呀,我确实这么做的为什么不行?自动创建的控件就是要放form1上呀,至于为什么要用现成,是因为创建控件有很多工作要做,所以才采用线程的。怎么办了呢?另外我刚才发现,报错的原因就是这句话:Btn[i-1].Parent := form1;但是不指定parent又不行,因为不制定看不到新创建的控件。
 
地质灾害以及其他的高手们哪去了呀很急,在线等待谢谢
 
关闭程序前终止线程就行了。Form1.OnClose里写Windows.PostThreadMessage(TestThread_Picture.ThreadId,WM_QUIT,0,0);TestThread_Picture.WaitFor();TestThread_Picture.Free();
 
地质灾害TestThread_Picture.WaitFor();如果这样,就停在这里不动了,死了
 
我在procedure Test_Thread.Execute;
begin
{ Place thread code here } FreeOnTermiNate := True;
showPicture('');
end;
中有 FreeOnTermiNate := True;线程执行完会自己释放的吧
 
while (not Terminated and GetMessage(msg, 0, 0, 0))do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
Form1.OnClose里写TestThread_Picture.Terminated := True;TestThread_Picture.WaitFor();TestThread_Picture.Free();不要用FreeOnTermiNate := True
 
btn inherit tcomponent inherit tpersistent,tpersistent indicate hold,if you create btn in thread,who hode it when thread destroy?????????even you hadnot excuted with the func showPicture!!!!!
 
to:jonathan236谢谢你,同时也非常感谢地质灾害jonathan236,那要怎么写代码呢,能把代码写出来不说明用汉语落,我英文比较烂,谢谢。:)
 
to:地质灾害while (not Terminated and GetMessage(msg, 0, 0, 0))do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
Form1.OnClose里写TestThread_Picture.Terminated := True;TestThread_Picture.WaitFor();TestThread_Picture.Free();无你这样也不行的,整个程序就死在了:TestThread_Picture.WaitFor();这里!
 
这是你写的:while (not Terminated and GetMessage(msg, 0, 0, 0))do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
Form1.OnClose里写TestThread_Picture.Terminated := True;TestThread_Picture.WaitFor();TestThread_Picture.Free();不要用FreeOnTermiNate := True以上是你写的。但是,实际上这段程序不通,整个程序停在TestThread_Picture.WaitFor();无法继续往下执行
 
而且也不会有:TestThread_Picture.Terminated := True;写法,肯定是TestThread_Picture.Terminate;
 
你肯定也没有测试,随手写的不?
 

Similar threads

I
回复
0
查看
623
import
I
I
回复
0
查看
539
import
I
顶部