循环检测全屏窗口,非全屏时如何报错且停止倒计时
目的:循环检测当前全屏窗口,全屏时倒计时3600秒后执行指定程序,非全屏时停止倒计时且提示"非全屏"问题: 开始倒计时无法循环检测并报错?
代码如下
请帮下忙:#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("倒计时", 350, 172, 193, 125)
$Label1 = GUICtrlCreateLabel("即将开始.....", 56, 32, 232, 28)
GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
$Progress1 = GUICtrlCreateProgress(8, 88, 333, 17)
$Button1 = GUICtrlCreateButton("确定(&Y)", 53, 128, 73, 25, 0)
$Button2 = GUICtrlCreateButton("退出(&X)", 210, 128, 73, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Sleep (1500)
Local $bj;定义BJ变量
$bj=0;定义BJ变量
While $bj=0
$var = WinGetPos("")
$w = @DesktopWidth ;检测屏幕宽度
$h = @DesktopHeight;检测屏幕高度
$wd =$var;检测POS指定的窗口宽度
$hd =$var ;检测POS指定的窗口高度
If $w <> $wd And $h <> $hd Then
MsgBox(4096, "状态:", "FAIL!");不相等时报错
exit
ElseIf $w = $wd And $h = $hd Then
$bj=0
;倒计时开始
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $time = 30
AdlibEnable("_timer", 1000)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $Button2
Exit
Case $Button1
ExitLoop
EndSwitch
If $time <= 0 Then ExitLoop
WEnd
main()
Exit
Func _timer()
$time -= 1
GUICtrlSetData($Label1, $time & "秒后将进入!")
GUICtrlSetData($Progress1, (30 - $time) / 0.3)
If $time <= 0 Then AdlibDisable()
EndFunc ;==>_timer
Func main()
MsgBox(0, 'test', '倒计时结束,进入主程序.', 10)
;以下为主程序
Run("地址")
EndFunc ;==>main
EndIf
$bj=0
WEnd
Func terminate()
Exit
EndFunc 本帖最后由 ceoguang 于 2010-11-21 00:53 编辑
方法1:
#include <WinAPI.au3>
Global $WindowsWidth = _WinAPI_GetSystemMetrics(0)
Global $WindowsHeight = _WinAPI_GetSystemMetrics(1)
$Form1 = GUICreate("倒计时", 350, 172, 193, 125)
$Label1 = GUICtrlCreateLabel("即将开始.....", 56, 32, 232, 28)
GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
$Progress1 = GUICtrlCreateProgress(8, 88, 333, 17)
$Button1 = GUICtrlCreateButton("确定(&Y)", 53, 128, 73, 25, 0)
$Button2 = GUICtrlCreateButton("退出(&X)", 210, 128, 73, 25, 0)
GUISetState(@SW_SHOW)
Global $time = 30
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3, $Button2
Exit
Case $Button1
ExitLoop
EndSwitch
If IsFullWindow() Then ;全屏
AdlibRegister("_timer", 1000) ;调试时将时间设置更短效果更明显
EndIf
WEnd
Func _timer()
$time -= 1
GUICtrlSetData($Label1, $time & "秒后将进入!")
GUICtrlSetData($Progress1, (30 - $time) / 0.3)
Select
Case $time <= 0
;GUICtrlSetData($Progress1, 0) 非调试时可去除
$time = 30
Run("执行程序")
Return AdlibUnRegister("_timer")
Case Not (IsFullWindow())
GUICtrlSetData($Label1, "非全屏!")
;GUICtrlSetData($Progress1, 0)
$time = 30
AdlibUnRegister("_timer")
EndSelect
EndFunc ;==>_timer
Func IsFullWindow() ;检测当前窗口是否全屏,是:返回1,否则返回0
Pos()
$hwnd = _WinAPI_GetForegroundWindow()
If $hwnd = _WinAPI_GetDesktopWindow() Then Return 0 ;不检测资源管理器
$tRect = _WinAPI_GetWindowRect($hwnd)
$iLeft = DllStructGetData($tRect, "Left")
$iTop = DllStructGetData($tRect, "Top")
$iRight = DllStructGetData($tRect, "Right")
$iBottom = DllStructGetData($tRect, "Bottom")
If $WindowsWidth <= $iRight - $iLeft And $WindowsHeight <= $iBottom - $iTop Then Return 1
Return 0
EndFunc ;==>IsFullWindow
本帖最后由 ceoguang 于 2010-11-21 00:53 编辑
方法2:
#include <WinAPI.au3>
Global $Struct = DllStructCreate($tagPoint)
Global $WindowsWidth = _WinAPI_GetSystemMetrics(0)
Global $WindowsHeight = _WinAPI_GetSystemMetrics(1)
$Form1 = GUICreate("倒计时", 350, 172, 193, 125)
$Label1 = GUICtrlCreateLabel("即将开始.....", 56, 32, 232, 28)
GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
$Progress1 = GUICtrlCreateProgress(8, 88, 333, 17)
$Button1 = GUICtrlCreateButton("确定(&Y)", 53, 128, 73, 25, 0)
$Button2 = GUICtrlCreateButton("退出(&X)", 210, 128, 73, 25, 0)
GUISetState(@SW_SHOW)
Global $time = 30
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3, $Button2
Exit
Case $Button1
ExitLoop
EndSwitch
If IsFullWindow() Then ;全屏
AdlibRegister("_timer", 1000) ;调试时将时间设置更短效果更明显
EndIf
WEnd
Func _timer()
$time -= 1
GUICtrlSetData($Label1, $time & "秒后将进入!")
GUICtrlSetData($Progress1, (30 - $time) / 0.3)
Select
Case $time <= 0
;GUICtrlSetData($Progress1, 0) 非调试时可去除
$time = 30
Run("执行程序")
Return AdlibUnRegister("_timer")
Case Not (IsFullWindow())
GUICtrlSetData($Label1, "非全屏!")
;GUICtrlSetData($Progress1, 0)
$time = 30
AdlibUnRegister("_timer")
EndSelect
EndFunc ;==>_timer
Func IsFullWindow() ;检测当前窗口是否全屏,是:返回1,否则返回0
Pos()
$hwnd = _WinAPI_WindowFromPoint($Struct)
If $hwnd = _WinAPI_GetDesktopWindow() Then Return 0 ;不检测资源管理器
$tRect = _WinAPI_GetWindowRect($hwnd)
$iLeft = DllStructGetData($tRect, "Left")
$iTop = DllStructGetData($tRect, "Top")
$iRight = DllStructGetData($tRect, "Right")
$iBottom = DllStructGetData($tRect, "Bottom")
If $WindowsWidth <= $iRight - $iLeft And $WindowsHeight <= $iBottom - $iTop Then Return 1
Return 0
EndFunc ;==>IsFullWindow
Func Pos()
DllStructSetData($Struct, "x", MouseGetPos(0))
DllStructSetData($Struct, "y", MouseGetPos(1))
EndFunc ;==>Pos
本帖最后由 yhxhappy 于 2010-11-21 12:56 编辑
楼主的源码结构很另类哦, 把 #include <> 放在While 里面
楼主的问题我理解的是:有全屏窗口时,则显示倒计时窗口并开始倒计时,时间到了则执行相应程序,如果倒计时没结束用户点击 确定 按钮也同样执行相应程序。#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("倒计时", 350, 172, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI")
$Label1 = GUICtrlCreateLabel("即将开始.....", 56, 32, 232, 28)
GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
$Progress1 = GUICtrlCreateProgress(8, 88, 333, 17)
$Button1 = GUICtrlCreateButton("确定(&Y)", 53, 128, 73, 25, 0)
GUICtrlSetOnEvent(-1, "GUI")
$Button2 = GUICtrlCreateButton("退出(&X)", 210, 128, 73, 25, 0)
GUICtrlSetOnEvent(-1, "GUI")
Global $time = 30
Global $SanWindows = 0
While 1
If $SanWindows = 0 Then
$var = WinGetPos("")
If $Var >= @DesktopWidth And $var >= @DesktopHeight Then
ToolTip("")
GUISetState(@SW_SHOW)
WinSetOnTop($Form1, "", 1)
AdlibRegister("_timer", 1000);倒计时开始
$SanWindows = 1
Else
HotKeySet("{ESC}", "Quit")
ToolTip("当前窗口非全屏,请试着打开一个全屏窗口,按 ESC 键可退出本程序")
EndIf
EndIf
WEnd
Func GUI()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE, $Button2
Exit
Case $Button1
AdlibUnRegister()
main()
EndSwitch
EndFunc
Func _timer()
$time -= 1
GUICtrlSetData($Label1, $time & "秒后将进入!")
GUICtrlSetData($Progress1, (30 - $time) / 0.3)
If $time <= 0 Then
AdlibUnRegister()
main()
EndIf
EndFunc ;==>_timer
Func main()
GUISetState(@SW_HIDE)
MsgBox(0, 'test', '倒计时结束,进入主程序.', 10)
;以下为主程序
;Run("地址")
EndFunc ;==>main
Func Quit()
Exit
EndFunc 楼上的写的很好;
但理解有点偏差,需求为:
倒计时开始后检测全屏,非全屏时暂且出现提示
一直全屏时倒计时完成后执行MAIN
要点是:循环检测 本帖最后由 yhxhappy 于 2010-12-1 15:24 编辑
那这样的话就会有个问题:
你给的代码是判断当前窗口是否全屏,可是一旦弹出倒计时窗口后,倒计时窗口就是当前窗口,那永远都不会有全屏窗口了,除非使倒计时窗口失去焦点再开始循环检测,或者楼主指定一个窗口标题和文本,这样最简单了。你不会是任意窗口全屏都触发吧
下面代码的倒计时窗口是失去焦点的,代码有问题,就是倒计时窗口不能点击否则它就会变成当前窗口,就会提示并退出。#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>
#Include <WinAPIEx.au3>
If MsgBox(32+1, "", "测试即将开始,请先将一个窗口全屏,再点击确定") <> 1 Then Exit
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("倒计时", 350, 222, 193, 125, "", $WS_EX_TOPMOST)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI")
$Label1 = GUICtrlCreateLabel("即将开始.....", 56, 32, 232, 28)
GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
$Progress1 = GUICtrlCreateProgress(8, 88, 333, 17)
$Button1 = GUICtrlCreateButton("确定(&Y)", 53, 128, 73, 25, 0)
GUICtrlSetOnEvent(-1, "GUI")
$Button2 = GUICtrlCreateButton("退出(&X)", 210, 128, 73, 25, 0)
GUICtrlSetOnEvent(-1, "GUI")
$g = WinGetHandle("")
GUISetState()
_WinAPI_SwitchToThisWindow($g, False)
Global $time = 30
AdlibRegister("_timer", 1000)
While 1
$var = WinGetPos("")
If $Var < @DesktopWidth Or $var < @DesktopHeight Then
GUIDelete()
MsgBox(0, "", "当前窗口非全屏,请试着打开一个全屏窗口")
Exit
ElseIf $time <= 0 Then
AdlibUnRegister()
GUIDelete()
main()
EndIf
WEnd
Func GUI()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE, $Button2
Exit
Case $Button1
AdlibUnRegister()
main()
EndSwitch
EndFunc
Func _timer()
$time -= 1
GUICtrlSetData($Label1, $time & "秒后将进入!")
GUICtrlSetData($Progress1, (30 - $time) / 0.3)
EndFunc ;==>_timer
Func main()
MsgBox(0, 'test', '倒计时结束,进入主程序.', 10)
Exit
;以下为主程序
;Run("地址")
EndFunc ;==>main
代码2
If MsgBox(32+1, "", "测试即将开始,请先将一个窗口全屏,再点击确定") <> 1 Then Exit
HotKeySet("{a}", "main")
HotKeySet("{ESC}", "Quit")
Global $time = 30
AdlibRegister("_timer", 1000);倒计时开始
While 1
$var = WinGetPos("")
If $Var < @DesktopWidth Or $var < @DesktopHeight Then
ToolTip("")
MsgBox(0,"","当前窗口非全屏,请试着打开一个全屏窗口,")
Exit
ElseIf $time <= 0 Then
main()
EndIf
WEnd
Func _timer()
$time -= 1
ToolTip("程序将在 " & $time & " 秒后将进入! 按 A 键直接进入", 200, 200, "倒计时开始,ESC键退出")
EndFunc ;==>_timer
Func main()
AdlibUnRegister()
ToolTip("")
MsgBox(0, 'test', '倒计时结束,进入主程序.', 10)
Exit
;以下为主程序
;Run("地址")
EndFunc ;==>main
Func Quit()
Exit
EndFunc
页:
[1]