关于线程中调用窗体?(100分)

  • 主题发起人 主题发起人 happycyp
  • 开始时间 开始时间
H

happycyp

Unregistered / Unconfirmed
GUEST, unregistred user!
unit UCMsgThread;
interface
uses
ExtCtrls,Classes,Forms,SysUtils,Dialogs;
type TMsgThread = class(TThread)
private
procedure GetMsg;
protected
published
procedure Execute ;
override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;
override;
end;

implementation
uses USMS, UCHint;
{ TMsgThread }
constructor TMsgThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
end;

destructor TMsgThread.Destroy;
begin
inherited destroy;
end;

procedure TMsgThread.Execute;
begin
inherited;
//Application.ProcessMessages;
//放到线程里没用
while truedo
//循环执行
begin
sleep(200);
GetMsg;
if Terminated then
Break;
end;
end;

procedure TMsgThread.GetMsg;
var
Mobile,CentreNo,CenDate,RcvDate : string;
Order : integer;
bSuc : boolean;
sSuc : string;
begin

if SMS.Busy then
exit;
if SMS.GetMsgStaus(Order,Mobile,CentreNo,CenDate,RcvDate,bSuc) then
begin
if bSuc then
sSuc := '成功。' else
sSuc := '失败。';
ShowMessage('手机:'+Mobile+'收到短信'+#13
+ '短消息中心:'+CentreNO
+ '到达短信中心时间:' + CenDate+ #13
+ '到达对方时间:'+RcvDate+' '+sSuc);
//可以显示对话框。
// sendmessage(yourhandl,WM_user+2001,yourmessage,0);
{ if not Assigned(FrmHint) then
FrmHint := TFrmHint.Create(nil);
FrmHint.LabHint.Caption := ('手机:'+Mobile+'收到短信'+#13
+ '短消息中心:'+CentreNO
+ '到达短信中心时间:' + CenDate+ #13
+ '到达对方时间:'+RcvDate+' '+sSuc);
FrmHint.Top := Screen.Height -30;
FrmHint.Left := Screen.Width - FrmHint.Width;
FrmHint.TM.Enabled := True;
FrmHint.BitBtnLook.Visible := False;
FrmHint.Show;
} //调试发现,执行到这里了,显示不了呀
end;
end;

end.
上面的线程怎么让窗体弹出来呀。
如果用SendMessage发送消息,那么窗体如何接收这个消息。
我的这个线程每0.2s执行一次。请高手指教。
 
线程的Execute方法改一下,增加消息处理,如下:
procedure TMsgThread.Execute;
var
Msg: TMsg;
begin
repeat
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;

GetMsg;
until Terminated;
end;
 
还有一种方法是不要在TMsgThread线程中创建窗体,在主线程中创建,TMsgThread中把它显示出来就行了.
 
//Application.ProcessMessages;
//放到线程里没用
不是没用,而是你没有放在循环体内
 
阿海兄,改了,还是弹不出来。
 
理论上讲是可以显示的
你的是不是有个timer控件,时间到了就关闭啊?
你别管那么多,先直接显示出来就行,包括位置什么都不要写了
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit3, Unit2;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
form3:TForm3;
MsgThread :TMsgThread;
procedure AcceptMessage(var Msg:TMessage);message wm_User+2001;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation

{$R *.dfm}
procedure TForm1.AcceptMessage(var Msg:TMessage);
begin
form3.Show;
Form3.Memo1.Lines.Add(inttostr(Msg.WParam ));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MsgThread :=TMsgThread.Create(true);
MsgThread.MsgHandle :=self.Handle;
MsgThread.Execute;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
form3:=TForm3.Create(self);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Form3.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
MsgThread.Suspend;
MsgThread.Terminate;
application.ProcessMessages;
end;

end.
////
unit Unit2;
interface
uses
ExtCtrls,Classes,Forms,SysUtils,Dialogs,windows,messages;
type TMsgThread = class(TThread)
private
i:integer;
procedure GetMsg;
protected
published
procedure Execute ;
override;
public
MsgHandle:HWND;
constructor Create(CreateSuspended: Boolean);
destructor Destroy;
override;
end;

implementation

{ TMsgThread }
constructor TMsgThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
end;

destructor TMsgThread.Destroy;
begin
inherited destroy;
end;

procedure TMsgThread.Execute;
begin
inherited;
//Application.ProcessMessages;
//放到线程里没用
while truedo
//循环执行
begin
sleep(200);
GetMsg;
if Terminated then
Break;
application.ProcessMessages;

end;
end;

procedure TMsgThread.GetMsg;
var
Mobile,CentreNo,CenDate,RcvDate : string;
Order : integer;
bSuc : boolean;
sSuc : string;
begin
inc(i);
Sendmessage(MsgHandle,WM_User+2001,i,0);
{if SMS.Busy then
exit;
if SMS.GetMsgStaus(Order,Mobile,CentreNo,CenDate,RcvDate,bSuc) then
begin
if bSuc then
sSuc := '成功。' else
sSuc := '失败。';
ShowMessage('手机:'+Mobile+'收到短信'+#13
+ '短消息中心:'+CentreNO
+ '到达短信中心时间:' + CenDate+ #13
+ '到达对方时间:'+RcvDate+' '+sSuc);} //可以显示对话框。
// sendmessage(yourhandl,WM_user+2001,yourmessage,0);
{ if not Assigned(FrmHint) then
FrmHint := TFrmHint.Create(nil);
FrmHint.LabHint.Caption := ('手机:'+Mobile+'收到短信'+#13
+ '短消息中心:'+CentreNO
+ '到达短信中心时间:' + CenDate+ #13
+ '到达对方时间:'+RcvDate+' '+sSuc);
FrmHint.Top := Screen.Height -30;
FrmHint.Left := Screen.Width - FrmHint.Width;
FrmHint.TM.Enabled := True;
FrmHint.BitBtnLook.Visible := False;
FrmHint.Show;
//调试发现,执行到这里了,显示不了呀
end;
}
end;

end.
//////
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
public
{ Public declarations }
end;

implementation
{$R *.dfm}
end.
 
谢谢魏哥了。哈哈搞定了。
 
谢谢魏哥了。哈哈搞定了。
 
后退
顶部