找回密码
 加入
搜索
查看: 5082|回复: 13

求一个计时器代码

[复制链接]
发表于 2008-7-2 18:36:38 | 显示全部楼层 |阅读模式
窗口显示后,就开始计时,并在窗口生成一个标签“已用时间:× 分 × 秒”

[ 本帖最后由 陌上风 于 2008-7-4 22:27 编辑 ]
发表于 2008-7-2 19:11:55 | 显示全部楼层
#include <GuiConstantsEx.au3>
#include <StaticConstants.au3>
Global Enum $timer = 1001
$xb0x = GUICreate("提醒您", 180, 147, @DesktopWidth-300, 50)
$Labela = GUICtrlCreateLabel("系统将在", -20, 1, 106, 115, $SS_CENTER)
$Labelb = GUICtrlCreateLabel(0, 32, 16, 106, 115, $SS_CENTER)
GUICtrlSetFont(-1, 80, 400, 0, "宋体")
GUICtrlSetColor (-1, 0xff99ff)
$Labelc = GUICtrlCreateLabel("秒", 140, 90, 20, 115, $SS_CENTER)
$Labeld = GUICtrlCreateLabel("后强制关机!请尽快保存工作!", -10, 120, 200, 115, $SS_CENTER)
GUISetState(@SW_SHOW)
WinSetOnTop($xb0x,"",1)
$Hourtimer = DllCallbackRegister("_TimerFunc", "none", "")
$timer1 = _SetTimer($xb0x, $timer, 1000, DllCallbackGetPtr($Hourtimer))

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func _SetTimer($hWnd, $iIDEvent, $iElapse, $pTimerFunc = 0)
    Local $iResult = DllCall("user32.dll", "int", "SetTimer", "hwnd", $hWnd, "int", $iIDEvent, "int", $iElapse, "ptr", $pTimerFunc)
    If @error Then Return SetError(-1, -1, 0)
    Return $iResult[0]
EndFunc

Func _TimerFunc()
        $Nub=Number(GUICtrlRead($Labelb))+1
        If $Nub=-1 Then
                        Shutdown(9)
                Exit
        EndIf
        GUICtrlSetData($Labelb,$Nub)
Return
EndFunc

[ 本帖最后由 xb0x 于 2008-7-2 19:25 编辑 ]
发表于 2008-7-2 19:18:41 | 显示全部楼层
哈哈,这论坛什么代码都能求啊。
 楼主| 发表于 2008-7-2 21:06:34 | 显示全部楼层
运行出错

ERROR: DllCallbackRegister(): undefined function.
$Hourtimer = DllCallbackRegister("_TimerFunc", "none", "")

ERROR: DllCallbackGetPtr(): undefined function.
$timer1 = _SetTimer($xb0x, $timer, 1000, DllCallbackGetPtr($Hourtimer)
发表于 2008-7-2 22:36:45 | 显示全部楼层
GUIConstantsEx.au3
StaticConstants.au3
差了这两个文件吧,论坛无法上传,你自己去找找看,还有可能是版本问题
我用的是3.2.10.0,Vista测试成功

[ 本帖最后由 xb0x 于 2008-7-2 22:40 编辑 ]
 楼主| 发表于 2008-7-2 22:41:27 | 显示全部楼层
include 文件夹有这 2 个文件

我的是 3.2.5版。曾经装过 3.2.12版,但好多 3.2.5 版运行正常的脚本,到3.2.12版不能运行,就卸载了 3.2.12版。

[ 本帖最后由 陌上风 于 2008-7-2 22:55 编辑 ]
 楼主| 发表于 2008-7-3 23:36:30 | 显示全部楼层
再顶上去。
发表于 2008-7-4 05:55:46 | 显示全部楼层
再顶也没有翻帮助的快。帮助本身就有。先问下自己看了多少帮助吧。
发表于 2008-7-4 10:32:34 | 显示全部楼层
什么都不服,服的就是楼上的这句话
 楼主| 发表于 2008-7-4 15:31:45 | 显示全部楼层
没找到
发表于 2008-7-4 19:06:30 | 显示全部楼层
AdlibEnable 去查下这个函数就知道怎么做了
 楼主| 发表于 2008-7-4 21:47:58 | 显示全部楼层
谢谢楼上的朋友!

[au3]
; *** Demo to show a timer window
#include <GUIConstantsEx.au3>
#include <Date.au3>

Opt("TrayIconDebug", 1)

Opt("MustDeclareVars", 1)

Global $timer, $Secs, $Mins, $Hour, $Time

_Main()

Func _Main()
        ;Create GUI
        GUICreate("Timer", 120, 50)
        GUICtrlCreateLabel("00:00:00", 10, 10)
        GUISetState()
        ;Start timer
        $timer = TimerInit()
        AdlibEnable("Timer", 50)
        ;
        While 1
                ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> before")
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                                Exit
                EndSwitch
                ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> after")
        WEnd
EndFunc   ;==>_Main

Func Timer()
        _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
        Local $sTime = $Time  ; save current time to be able to test and avoid flicker..
        $Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
        If $sTime <> $Time Then ControlSetText("Timer", "", "Static1", $Time)
EndFunc   ;==>Timer
[/au3]

不过奇怪的是,这段代码运行后具有独占性,无论是在 _main 函数之内还是之外使用 run 语句均没有执行!
 楼主| 发表于 2008-7-4 21:56:39 | 显示全部楼层
找到解决办法了。又发现一个问题,当给他添加了背景图片时,计时器就不动了

[ 本帖最后由 陌上风 于 2008-7-4 22:26 编辑 ]
发表于 2010-6-28 09:33:46 | 显示全部楼层
AdlibRegister  感谢。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-22 04:21 , Processed in 0.079637 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表