匪
匪匪
Unregistered / Unconfirmed
GUEST, unregistred user!
我的主窗口中动态生成了一个TMediaPlayer的对象,在线程中调用这个对象播放音乐,为什么动态生成的就会影响程序主界面移动一次后即被锁定,而静态生成的TMediaPlayer对象,则不会影起主窗口的锁屏呢?
程序代码如下:
TForm1 = class;
TChThread = class(TThread)
private
EventHandle: THandle;
FForm: TForm1;
procedure Action;
protected
procedure Execute;override;
public
constructor Create(form: TForm1);
destructor Destroy;override;
end;
TForm1 = class(TForm)
Label1: TLabel;
Button2: TButton;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
Thread: TChThread;
FSpeed: Integer;
FStopAction: Boolean;
public
{ Public declarations }
Mediaplayer1: TMediaPlayer;
proceduredo
It(Labe: TLabel;
p: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FStopAction:= False;
FSpeed:= 20;
MediaPlayer1:= TMediaPlayer.Create(Self);
Mediaplayer1.Parent:= Self;
//此句注释后就不会有问题了.或者静态生成的控件也不会有问题
//MediaPlayer1.Visible:= False;
Thread:= TChThread.Create(Self);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetEvent(Thread.EventHandle);
end;
{ TChThread }
procedure TChThread.Action;
begin
FForm.DoIt(FForm.Label1, 'C:/Program Files/Tencent/QQ/sound/call.wav' );
FForm.DoIt(FForm.Label2, 'C:/Program Files/Tencent/QQ/sound/msg.wav' );
end;
constructor TChThread.Create(form: TForm1);
begin
FForm:= form;
FreeOnTerminate:= True;
EventHandle:= CreateEvent(nil, True, False, 'Kevin.J.Hoo');
inherited Create(False);
end;
destructor TChThread.Destroy;
begin
CloseHandle(EventHandle);
Terminate;
inherited;
end;
procedure TChThread.Execute;
begin
while not Terminateddo
begin
WaitForSingleObject(EventHandle, INFINITE);
ResetEvent(EventHandle);
if not FForm.FStopAction then
Action;
if Terminated then
Break;
end;
end;
procedure TForm1.DoIt(Labe: TLabel;
P: string);
var
i, Height, Width: Integer;
rRect: TRect;
begin
MediaPlayer1.FileName:= p;
MediaPlayer1.Open;
MediaPlayer1.Play;
with Labedo
begin
Height:= Canvas.TextHeight(Caption);
Width:= Canvas.TextWidth(Trim(Caption));
//此处Width在字体变斜后,会不正确,有待改进
for i:= 0 to Widthdo
if not FStopAction then
begin
Canvas.Lock;
Canvas.Brush.Style:= bsClear;
Canvas.Font.Color:= clBlue;
rRect:= Rect(0, 0, i, Height);
Sleep(FSpeed);
Canvas.TextRect(rRect, 0, 0, Caption);
Canvas.Unlock;
end
else
Break;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Thread.Terminate;
FStopAction:= True;
end;
程序代码如下:
TForm1 = class;
TChThread = class(TThread)
private
EventHandle: THandle;
FForm: TForm1;
procedure Action;
protected
procedure Execute;override;
public
constructor Create(form: TForm1);
destructor Destroy;override;
end;
TForm1 = class(TForm)
Label1: TLabel;
Button2: TButton;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
Thread: TChThread;
FSpeed: Integer;
FStopAction: Boolean;
public
{ Public declarations }
Mediaplayer1: TMediaPlayer;
proceduredo
It(Labe: TLabel;
p: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FStopAction:= False;
FSpeed:= 20;
MediaPlayer1:= TMediaPlayer.Create(Self);
Mediaplayer1.Parent:= Self;
//此句注释后就不会有问题了.或者静态生成的控件也不会有问题
//MediaPlayer1.Visible:= False;
Thread:= TChThread.Create(Self);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetEvent(Thread.EventHandle);
end;
{ TChThread }
procedure TChThread.Action;
begin
FForm.DoIt(FForm.Label1, 'C:/Program Files/Tencent/QQ/sound/call.wav' );
FForm.DoIt(FForm.Label2, 'C:/Program Files/Tencent/QQ/sound/msg.wav' );
end;
constructor TChThread.Create(form: TForm1);
begin
FForm:= form;
FreeOnTerminate:= True;
EventHandle:= CreateEvent(nil, True, False, 'Kevin.J.Hoo');
inherited Create(False);
end;
destructor TChThread.Destroy;
begin
CloseHandle(EventHandle);
Terminate;
inherited;
end;
procedure TChThread.Execute;
begin
while not Terminateddo
begin
WaitForSingleObject(EventHandle, INFINITE);
ResetEvent(EventHandle);
if not FForm.FStopAction then
Action;
if Terminated then
Break;
end;
end;
procedure TForm1.DoIt(Labe: TLabel;
P: string);
var
i, Height, Width: Integer;
rRect: TRect;
begin
MediaPlayer1.FileName:= p;
MediaPlayer1.Open;
MediaPlayer1.Play;
with Labedo
begin
Height:= Canvas.TextHeight(Caption);
Width:= Canvas.TextWidth(Trim(Caption));
//此处Width在字体变斜后,会不正确,有待改进
for i:= 0 to Widthdo
if not FStopAction then
begin
Canvas.Lock;
Canvas.Brush.Style:= bsClear;
Canvas.Font.Color:= clBlue;
rRect:= Rect(0, 0, i, Height);
Sleep(FSpeed);
Canvas.TextRect(rRect, 0, 0, Caption);
Canvas.Unlock;
end
else
Break;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Thread.Terminate;
FStopAction:= True;
end;