如何判断程序打开却没有失去焦点又没有使用? ( 积分: 200 )

  • 主题发起人 主题发起人 blinking1313
  • 开始时间 开始时间
B

blinking1313

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做这样一个功能:一个程序虽然是打开的,如果一直没有使用,几分钟后,再想使用此程序就要重新登录.<br><br>我做了下面一段程序,但它只能在程序失去焦点时使用,如果程序没有失去焦点又没有使用,有什么好办法可以判断<br>这时的程序已长时间未使用?<br><br><br>var<br>&nbsp;&nbsp;g_freetime:integer;<br><br><br>procedure&nbsp;Tmain.Timer1Timer(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;If&nbsp;Application.Active=False&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;inc(g_freetime);<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;g_freetime&gt;10&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g_freetime:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;assigned(Form_login)&nbsp;then&nbsp;&nbsp;//Form_login&nbsp;是登录窗体<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Form_login:=tForm_login.create(application);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;g_freetime:=0;<br>&nbsp;&nbsp;end;<br><br>end;
 
GetLastInputInfo<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3269717
 
楼上的回帖可以解决完全不用计算机的情况。<br>还有一种情况不能解决,就是点击主程序在系统任务栏中(不是托盘中的小图标)中的按钮使程序最小化,而其它程序在用计算机。这时怎么知道主程序有多久没有使用了?<br>又如何获取再次点击主程序在系统任务栏中的按钮使程序还原的消息,并加以利用?
 
&gt;&gt;又如何获取再次点击主程序在系统任务栏中的按钮使程序还原的消息,并加以利用?&nbsp;&nbsp;<br><br>你不是说已经实现了?<br>&gt;&gt;但它只能在程序失去焦点时使用,如果程序没有失去焦点又没有使用<br><br>两个结合不就可以了
 
现在理一理:<br>我想做这样一个功能:一个程序虽然是打开的,如果一直没有使用,几分钟后,再想使用此程序就要重新登录.<br>有几种情况:<br>第一,程序打开之后计算机完全没有使用。(用ysai方法可解决)<br>第二,程序打开之后没有最小化到系统任务栏,这时正在使用其它程序。(用我的方法可解决)<br>第三,程序打开之后最小化到系统任务栏,正在使用其它程序。(未解决!)<br>而且点击程序右上方的最小化按钮和点击主程序在系统任务栏中的按钮所触发的消息是不一样的,所以我想如果能得到最小化和还原两个消息之间的时间差不是就能解决了吗?<br>但我不知道如何获取点击主程序在系统任务栏中的按钮使程序最小化或是还原的消息,并加以利用。
 
在主窗体上放一个TApplicationEvents组件,在它的事件中控制
 
试试下面的,TApplicationEvents+Ttimer<br>假设超过10秒没动作需要重复登录<br>//呵呵,抱歉,刚才发贴的时候,多写了个判断,错了<br>//用下面这个修改过的吧,可以解决最小化的问题(或者说,这样无所谓是不是最小化)<br><br><br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;ExtCtrls,&nbsp;AppEvnts;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;ApplicationEvents1:&nbsp;TApplicationEvents;<br>&nbsp;&nbsp;&nbsp;&nbsp;Timer1:&nbsp;TTimer;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ApplicationEvents1Idle(Sender:&nbsp;TObject;&nbsp;var&nbsp;Done:&nbsp;Boolean);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Timer1Timer(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;FLogined:boolean;&nbsp;&nbsp;&nbsp;{是否已登录}<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;SetLogin(value:boolean);<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.SetLogin(value:boolean);<br>begin<br>&nbsp;&nbsp;FLogined:=not&nbsp;value;<br>&nbsp;&nbsp;if&nbsp;not&nbsp;FLogined&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;timer1.Enabled:=false;<br>&nbsp;&nbsp;&nbsp;&nbsp;self.Caption:='要登录';<br>&nbsp;&nbsp;&nbsp;&nbsp;//showmessage('要登录');<br>&nbsp;&nbsp;&nbsp;&nbsp;FLogined:=true;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;self.Caption:='已登录';<br>end;<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;timer1.Interval:=10000;<br>&nbsp;&nbsp;timer1.Enabled:=false;<br>&nbsp;&nbsp;SetLogin(true);<br>end;<br><br>procedure&nbsp;TForm1.ApplicationEvents1Idle(Sender:&nbsp;TObject;<br>&nbsp;&nbsp;var&nbsp;Done:&nbsp;Boolean);<br>begin<br>&nbsp;&nbsp;if&nbsp;FLogined&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;application.Active&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetLogin(true)<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer1.Enabled:=true;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.Timer1Timer(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;SetLogin(true);<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;SetLogin(false);<br>end;<br><br>end.
 
unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;AppEvnts;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;ApplicationEvents1:&nbsp;TApplicationEvents;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ApplicationEvents1Idle(Sender:&nbsp;TObject;&nbsp;var&nbsp;Done:&nbsp;Boolean);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ApplicationEvents1Message(var&nbsp;Msg:&nbsp;tagMSG;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;Handled:&nbsp;Boolean);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;LastUse:&nbsp;DWord;<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.ApplicationEvents1Idle(Sender:&nbsp;TObject;<br>&nbsp;&nbsp;var&nbsp;Done:&nbsp;Boolean);<br>begin<br>&nbsp;&nbsp;LastUse&nbsp;:=&nbsp;GetTickCount;<br>end;<br><br>procedure&nbsp;TForm1.ApplicationEvents1Message(var&nbsp;Msg:&nbsp;tagMSG;<br>&nbsp;&nbsp;var&nbsp;Handled:&nbsp;Boolean);<br>begin<br>&nbsp;&nbsp;if&nbsp;GetTickCount&nbsp;-&nbsp;LastUse&nbsp;&gt;&nbsp;10000&nbsp;then&nbsp;&nbsp;//超过10秒<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;//重新登录<br>&nbsp;&nbsp;end;<br>end;<br><br>end.
 
搞定,谢谢诸君帮助。
 
后退
顶部