小问题:窗口失去焦点事件 ( 积分: 20 )

  • 主题发起人 主题发起人 guanyue7613
  • 开始时间 开始时间
G

guanyue7613

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样得知用户在切换当前的活动窗口时,获得当前窗口失去焦点的事件
 
你可以在电击其他窗口时通过触发onActive事件实现
 
其他的窗口不是我程序里的窗口,而是操作系统里其他程序的窗口,可能是一个记事本,也可能是任务管理器............
 
Occurs&nbsp;when&nbsp;the&nbsp;form&nbsp;loses&nbsp;focus.<br>&nbsp;<br>Class&nbsp;<br>TCustomForm&nbsp;<br><br>Syntax&nbsp;<br><br><br>[Delphi]&nbsp;property&nbsp;OnDeactivate:&nbsp;TNotifyEvent;
 
Description&nbsp;<br>Use&nbsp;OnDeactivate&nbsp;to&nbsp;perform&nbsp;special&nbsp;processing&nbsp;when&nbsp;the&nbsp;form&nbsp;transitions&nbsp;from&nbsp;being&nbsp;the&nbsp;active&nbsp;form&nbsp;to&nbsp;another&nbsp;form&nbsp;in&nbsp;the&nbsp;same&nbsp;application&nbsp;becoming&nbsp;the&nbsp;active&nbsp;form.&nbsp;If&nbsp;activation&nbsp;goes&nbsp;to&nbsp;another&nbsp;application,&nbsp;this&nbsp;event&nbsp;is&nbsp;not&nbsp;triggered.&nbsp;To&nbsp;determine&nbsp;if&nbsp;another&nbsp;application&nbsp;has&nbsp;become&nbsp;active,&nbsp;Use&nbsp;the&nbsp;TApplication&nbsp;object's&nbsp;OnDeactivate&nbsp;event.<br><br>只是在同一个程序里不同窗体之间的焦点切换时,发生
 
If&nbsp;activation&nbsp;goes&nbsp;to&nbsp;another&nbsp;application,&nbsp;this&nbsp;event&nbsp;is&nbsp;not&nbsp;triggered.&nbsp;To&nbsp;determine&nbsp;if&nbsp;another&nbsp;application&nbsp;has&nbsp;become&nbsp;active,&nbsp;Use&nbsp;the&nbsp;TApplication&nbsp;object's&nbsp;OnDeactivate&nbsp;event.<br><br>如果对于整个程序,在主窗体里加上TApplicationEvents组件(Additional页上)<br>在它的Deactivate事件中编写你的代码!<br><br>procedure&nbsp;TForm1.ApplicationEvents1Deactivate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;OutputDebugString(PChar('lost&nbsp;focus!'));<br>end;
 
嗯,我需要的是获取在不同程序里各个窗体之间的焦点切换时发生的事件!!<br><br>如果有相关demo的话更好,不够分可以再加
 
谢谢cnzzlp,可能您没有理解清楚我的意思:<br><br>我的程序在后台运行,需要获得当前其他windows程序的运行——当前被激活的窗口,当此激活窗口(!!!不是我的程序里的窗口啊!!!!!)被失去焦点的时候发生的事件
 
The&nbsp;GetForegroundWindow&nbsp;function&nbsp;returns&nbsp;a&nbsp;handle&nbsp;to&nbsp;the&nbsp;foreground&nbsp;window&nbsp;(the&nbsp;window&nbsp;with&nbsp;which&nbsp;the&nbsp;user&nbsp;is&nbsp;currently&nbsp;working).&nbsp;The&nbsp;system&nbsp;assigns&nbsp;a&nbsp;slightly&nbsp;higher&nbsp;priority&nbsp;to&nbsp;the&nbsp;thread&nbsp;that&nbsp;creates&nbsp;the&nbsp;foreground&nbsp;window&nbsp;than&nbsp;it&nbsp;does&nbsp;to&nbsp;other&nbsp;threads.<br>通过判断当前最顶层窗体的handle,你可有用两个变量保存进行比较.<br><br>procedure&nbsp;TForm1.Timer1Timer(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;hwd:&nbsp;THandle;<br>&nbsp;&nbsp;buf:&nbsp;PChar;<br>begin<br>//&nbsp;&nbsp;hwd&nbsp;:=&nbsp;GetActiveWindow;<br>&nbsp;&nbsp;GetMem(buf,1024);<br>&nbsp;&nbsp;hwd&nbsp;:=&nbsp;GetForegroundWindow;<br>&nbsp;&nbsp;if&nbsp;hwd&nbsp;&lt;&gt;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;GetWindowText(hwd,buf,1024);<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1.Lines.Add(Format('Caption:%s,Handle:%d',[buf,hwd]));<br>&nbsp;&nbsp;end;<br>end;
 
接受答案了.
 

Similar threads

后退
顶部