我在此谢谢大家提供的建议。但是似乎这些方法都不能达到我所希望的效果。 经过努力,我通过在dpr文件中写一个“WinMain”函数完成该功能。效果还不错,但是不保证用了只后delphi中别的功能不受影响(比如onidle事件)。程序如下:program Project1;uses windows, messages, Forms, Unit1 in 'Unit1.pas' {Form1};{$R *.res} var msg : TMSG; done: boolean;begin //这两行是原来就有的 Application.Initialize; Application.CreateForm(TForm1, Form1); //删除Application.Run; done:=false;//以下是消息循环 while not done do if (PeekMessage(Msg,0,0,0,PM_REMOVE)) then if msg.message=WM_QUIT then done:=true else begin TranslateMessage(msg); DispatchMessage(msg); end else if Application.Active then begin //加入空闲时调用的函数 Form1.Idle; //这个是在Form1中自己定义的函数 end;end.
如果是这样你可以在ApplicationEvents控件的onmessage事件中捕获键盘和鼠标操作,如有一段时间没有,就起动相应的函数,不过我想得用thread线程来做。
以便随时开起和终止。
这个在c++builder中很容易做到,但是在delphi中怎么这么困难
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
var Done: Boolean);
begin
//你的函数。
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
var Done: Boolean);
begin
//你的函数。
end;end.
为什么ApplicationOnIdle在不动键盘或者鼠标的时候不会发生~!!
难道是我的delphi的问题?
2、把TApplicationEvents控件(在Additional上)拉到Form上。
2、编写TApplicationEvents的OnMessage脚本void __fastcall TForm1::OnMessage(...)
{
Timer1->Enabled=false;
Timer1->Enabled=true;//只要收到消息就重置定时器
}void __fastcall TForm1::Timer1OnTimer(...)
{
//如果定时器被触发,证明用户已经有一段时间没有用过你的程序了。
}
1 放一个actionlist控件上去,就是standard最右边那个
2 随便定义一个动作,把你要执行的代码放到该动作的onupdate事件中,这个事件会在程序空闲时执行。
经过努力,我通过在dpr文件中写一个“WinMain”函数完成该功能。效果还不错,但是不保证用了只后delphi中别的功能不受影响(比如onidle事件)。程序如下:program Project1;uses
windows,
messages,
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}
var
msg : TMSG;
done: boolean;begin
//这两行是原来就有的
Application.Initialize;
Application.CreateForm(TForm1, Form1);
//删除Application.Run; done:=false;//以下是消息循环
while not done do
if (PeekMessage(Msg,0,0,0,PM_REMOVE)) then
if msg.message=WM_QUIT then done:=true else
begin
TranslateMessage(msg);
DispatchMessage(msg);
end
else
if Application.Active then
begin
//加入空闲时调用的函数
Form1.Idle; //这个是在Form1中自己定义的函数
end;end.
程序总是会运行错误,太麻烦了
程序总是会运行错误,太麻烦了
估计这个消息会失效