iT邦幫忙

5

使用 autoit 關閉長時間閒置程式

eigen 2013-08-23 01:32:2310133 瀏覽

使用 autoit 關閉長時間閒置程式

主要的問題: http://ithelp.ithome.com.tw/question/10124297

參考 http://ithelp.ithome.com.tw/question/10085874

以下就是最後做出來的結果。
為什麼會做的這麼複雜,主要是因為經過我的一些防呆測試後,發現一大堆會出錯的情況,因為必需設法解決。

整的程序最大的問題是,使用者可能啟動程式,但是程式可能產生一些其它的form ,這些 form 的名稱和指定程式的名稱是不一樣的,而且太多樣。

單純 WinActivate("指定的程式名稱", "") ,在一般情況下是沒有問題的,但是一但開了其它的 form ,就錯發生錯誤。

所以整個的對策,就是將指定的程式activate 後,產生一個gui ,然後關閉gui ,讓 指定程式『整個』 on top ,這樣才能用按鍵關閉程式。

(原本的想法是用 alt+tab 來切換,但是後來發現太難搞了,最後才發現用 autoit 產生gui ,再關閉gui ,這樣最簡單)

目前這樣的程序唯一問題就是啟動 vmware 的時候,這時候滑鼠鍵盤會在 沙箱之中,以下的程序就會有機會出包。

Opt("TrayIconHide",1)							;隱藏 AutoIt 常駐圖示
#include <Timers.au3>							;指定時間檔案腳本
#include <GUIConstantsEx.au3>
Dim $_time, $hide_time, $check_time
While 1									;迴圈
	Sleep (5000)							;每5sec 檢查一次
	$_time= _Timer_GetIdleTime()					;鍵盤滑鼠閒置時間

	If WinActive("指定的程式名稱") Then
		$hide_time=0
		$check_time= $_time + $hide_time
	Else
		If WinExists("指定的程式名稱") Then
			$hide_time+=5000				
			$check_time=$hide_time
		Else
			$hide_time=0
			$check_time=$hide_time
		EndIf
	EndIf								;$check_time 計算出 idle time
	
	If WinExists("指定的程式名稱") Then
		If $check_time >480000 Then
			_closeERP()					;程式存在,而且time out
		EndIf
	EndIf
WEnd

Func _closeERP()
	WinActivate("指定的程式名稱", "")				;指定活動視窗
	If WinActive("指定的程式名稱") Then
		GUICreate("Warning", 200, 100)	
		GUICtrlCreateLabel("Program idle for a long time.", 10, 10)
		GUICtrlCreateLabel("Program will exit.!", 10, 25)
		GUISetState(@SW_SHOW)
		Sleep (750)
		GUIDelete("Warning") 					;產生gui 然後消失,讓 程式 on top

		Send("{ESC}")
		Send("{ESC}")
		Send("{ESC}")
		Send("{ESC}")

		Send("{ESC}")
		Send("{ESC}")
		Send("{ESC}")
		Send("{ESC}")

		Send("{ESC}")
		Send("{ESC}")
		Sleep (300)						;暫停0.5秒

		If WinActive("指定的程式名稱") Then
			WinClose("指定的程式名稱", "")		;關閉指定視窗
			Sleep (500)					;暫停0.5秒
			Send("!Y")					;ALT+Y離開
		EndIf
	 EndIf
EndFunc

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言