陌上风 发表于 2008-7-2 18:36:38

求一个计时器代码

窗口显示后,就开始计时,并在窗口生成一个标签“已用时间:× 分 × 秒”

[ 本帖最后由 陌上风 于 2008-7-4 22:27 编辑 ]

xb0x 发表于 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
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)

xb0x 发表于 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

再顶上去。

sanhen 发表于 2008-7-4 05:55:46

再顶也没有翻帮助的快。帮助本身就有。先问下自己看了多少帮助吧。

跨日 发表于 2008-7-4 10:32:34

什么都不服,服的就是楼上的这句话

陌上风 发表于 2008-7-4 15:31:45

没找到:face (13):

llztt 发表于 2008-7-4 19:06:30

AdlibEnable 去查下这个函数就知道怎么做了

陌上风 发表于 2008-7-4 21:47:58

谢谢楼上的朋友!


; *** 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


不过奇怪的是,这段代码运行后具有独占性,无论是在 _main 函数之内还是之外使用 run 语句均没有执行!

陌上风 发表于 2008-7-4 21:56:39

找到解决办法了。又发现一个问题,当给他添加了背景图片时,计时器就不动了

[ 本帖最后由 陌上风 于 2008-7-4 22:26 编辑 ]

xunzhang22 发表于 2010-6-28 09:33:46

AdlibRegister感谢。。。
页: [1]
查看完整版本: 求一个计时器代码