在线程能不能创建窗体?贴上我完整的代码,请大家帮我看看!! ( 积分: 100 )

V

Vision

Unregistered / Unconfirmed
GUEST, unregistred user!
unit myThreadUnit;
interface
uses
Windows, Forms, Controls, SysUtils, Classes, Messages;
type
TmyForm = class;
TmyThread = class(TThread)
private
myForm: TmyForm;
protected
procedure Execute;
override;
procedure CreatemyForm;
public
constructor Create();
destructor Destroy;
override;
end;

TmyForm = class(TForm)
{ ... }
private
{ ... }
public
constructor Create();
destructor Destroy;
override;
end;

implementation
{ TmyForm }
constructor TmyForm.Create();
begin
Inherited Create(nil);
end;

destructor TmyForm.Destroy;
begin
Inherited Destroy;
end;

{ TmyThread }
procedure TmyThread.CreatemyForm;
begin
if myForm=nil then
try
myForm:=TmyForm.Create();
//--->每次执行到这行出错
myForm.Visible:=True;
myForm.BorderIcons:=[biSystemMenu,biMinimize];
myForm.Top:=0;
myForm.Left:=0;
myForm.Width:=100;
myForm.Height:=100;
except
Application.MessageBox('出错了','错误');
end;
end;

procedure TmyThread.Execute;
begin
while not Terminateddo
begin
if myForm=nil then
Synchronize(CreatemyForm);
{ 其它代码略... }
Sleep(100);
end;
end;

constructor TmyThread.Create();
begin
Inherited Create(True);
FreeOnTerminate:=False;
Suspended:=False;
end;

destructor TmyThread.Destroy;
begin
Inherited Destroy;
end;

end.
 
因为你这个窗体少了个dfm文件,所以创建的时候出错,你没有看到它说是少了资源吗!
在implementation下加上这句:
{$R *.dfm}
然后新建一个文档并命名为myThreadUnit.dfm。里面加上以下两句:
object myForm: TmyForm
end
 
如果是缺少dfm,编译时就该出错了,我现在是编译完全没问题,就是运行时出错。
而且我这个窗体只是动态创建的,不需要dfm。
 
说你又不信,真是没办法,你试过我的方法再说吧,多看看Debug的信息,如果你没有打开Debug。把你的Application.MessageBox('出错了','错误');改成。
on E: Exceptiondo
begin
Application.MessageBox(pchar(e.Message), '错误');
end;
我的答案是根据这个信息得出来的,并且在Delphi7中经过测试证实是正确的!
你说如果是缺少dfm编译时就该出错了,那你试过窗体的{$R *.dfm}这行删除看看能不能编译???
别人给你答案请你先做测试再评论。
 
说的对,需要一个dfm文件,
改成 TmyForm = class(Tform1) ,利用form1的dfm就不错了
 
呵呵,不好意思,因为我没有 dfm 编译也能通过,所以才搞个
Application.MessageBox('出错了','错误');
来运行测试,试了 qqjm 的方法,确实可行的,非常感谢。
另外我也试过了,myForm不要用继承一个窗体的方法,直接
myForm:=TForm.Create(nil);
就可以只要pas,不要dfm
以下代码在 D7 调试通过
unit myThreadUnit;
interface
uses
Windows, Forms, Controls, SysUtils, Classes, Messages, MPlayer;
type
//TmyForm = class;
TmyThread = class(TThread)
private
myForm: TForm;
MediaPlayer: TMediaPlayer;
protected
procedure Execute;
override;
procedure CreatemyForm;
public
constructor Create();
destructor Destroy;
override;
end;
{
TmyForm = class(TForm)
private
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
end;
}
implementation
{ TmyForm }
//{$R *.dfm}
{
constructor TmyForm.Create(AOwner:TComponent);
begin
Inherited Create(AOwner);
end;

destructor TmyForm.Destroy;
begin
Inherited Destroy;
end;
}
{ TmyThread }
procedure TmyThread.CreatemyForm;
begin
if myForm=nil then
try
myForm:=TForm.Create(nil);
//--->每次执行到这行出错(现在不出错了哈)
myForm.Visible:=True;
myForm.BorderIcons:=[biSystemMenu,biMinimize];
myForm.Top:=0;
myForm.Left:=0;
myForm.Width:=300;
myForm.Height:=100;
MediaPlayer:=TMediaPlayer.Create(nil);
MediaPlayer.Parent:=myForm;
MediaPlayer.DeviceType:=dtAutoSelect;
MediaPlayer.FileName:='L:/Music1.mid';
MediaPlayer.Open;
MediaPlayer.Play;
except
on E: Exceptiondo
begin
Application.MessageBox(Pchar(e.Message), '错误');
end
end;
end;

procedure TmyThread.Execute;
begin
while not Terminateddo
begin
if myForm=nil then
begin
Synchronize(CreatemyForm);
end;
Sleep(100);
end;
end;

constructor TmyThread.Create();
begin
Inherited Create(True);
FreeOnTerminate:=False;
Suspended:=False;
end;

destructor TmyThread.Destroy;
begin
Inherited Destroy;
MediaPlayer.Free;
myForm.Free;
end;

end.
 
谢谢两位兄弟在帮忙!!
 
多人接受答案了。
 
顶部