你可以将该过程写在线程中 procedure TTestThread.Execute(); begin FreeOnTerminate:=true; //DoSomething while not (Terminated) do begin end; //DoOtherthing end; //在Form里 procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); begin TestThread.Terminated(); end;
拿去,不用多加分了 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts;type TForm1 = class(TForm) ApplicationEvents1: TApplicationEvents; procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then begin // Close; //Do something here what you want to do end; end;end.
假设我的程序是: procedure doit(); begin Label1.caption:='Good'; //(按任意键继续) //这里应该加入什么样的函数 Label1.caption:='Bad'; end
private m_key:word; formcreate; begin key :=1; end;procedure do BEGIN while (key<>m_key); END;form.keyDown..... begin m_key:=key; end;procedure doit(); begin Label1.caption:='Good'; //(按任意键继续) do; //这里应该加入什么样的函数 Label1.caption:='Bad'; end
可以这样试一下 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts;type TForm1 = class(TForm) ApplicationEvents1: TApplicationEvents; procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); private { Private declarations } public { Public declarations } end;var Form1: TForm1; F:boolean;//在你的OnCreate事件中付值为False;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if f then//新添上的 if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then begin // Close; //Do something here what you want to do end; end;procedure doit();//你的doit函数 begin f:=Ture; Label1.caption:='Good'; //(按任意键继续) do; //这里应该加入什么样的函数 Label1.caption:='Bad'; f:=False; end end.
Shift: TShiftState);
begin
Showmessage('ok');
end;
好像这样就可以了
或重载WinProc函数
或使用ApplicationEnevnts控件
procedure TTestThread.Execute();
begin
FreeOnTerminate:=true;
//DoSomething
while not (Terminated) do
begin end;
//DoOtherthing
end;
//在Form里
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
TestThread.Terminated();
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, AppEvnts;type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then
begin
// Close;
//Do something here what you want to do
end;
end;end.
procedure doit();
begin
Label1.caption:='Good';
//(按任意键继续)
//这里应该加入什么样的函数
Label1.caption:='Bad';
end
你混淆了Windows编程和Dos编程的概念,Dos只有一个程序可以运行,Windows是靠线程调度的,也就是说,在Dos下面运行程序,所有资源都被程序占用了,可以靠一个死循环判断用户是否按键。但是Windows不行,如果键盘被按下,Windows会发一个消息给相应的程序,然后相应的程序再处理。如果你在函数doit()里无限等待,而且你的程序只有这一个主线程,请问,不跳出这个函数,Windows如何通知你键被按下了?这也就是,如果你在某一个函数里进行了大量的长时间操作,会发现窗口失去了响应,因为只有结束了这个函数程序才能转到处理键盘的子程序中。因此除非使用多线程,你不可能在一个函数中间等待按键,然后恢复执行的。
m_key:word;
formcreate;
begin
key :=1;
end;procedure do
BEGIN
while (key<>m_key);
END;form.keyDown.....
begin
m_key:=key;
end;procedure doit();
begin
Label1.caption:='Good';
//(按任意键继续)
do;
//这里应该加入什么样的函数
Label1.caption:='Bad';
end
我已经说了,如果你希望程序在函数中间能停止,响应消息再继续,必须使用线程,一个线程运行函数,另一个负责消息循环,这样才能实现你要的功能。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, AppEvnts;type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
F:boolean;//在你的OnCreate事件中付值为False;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if f then//新添上的
if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then
begin
// Close;
//Do something here what you want to do
end;
end;procedure doit();//你的doit函数
begin
f:=Ture;
Label1.caption:='Good';
//(按任意键继续)
do;
//这里应该加入什么样的函数
Label1.caption:='Bad';
f:=False;
end
end.
0x21中断的调用吗?