Что нового

Вставка таймера в скрипт и запуск по кнопке

Hulk777

Новичок
Сообщения
69
Репутация
1
Здравствуйте. Добавьте пожалуйста запуск простенького таймера нормального отсчета чч.мм.сс. по кнопке с отображением в этой форме.

Код:
#include <GUIConstants.au3>

GUICreate('Form1', 280, 70)


$Button1 = GUICtrlCreateButton('Button', 20, 10, 240, 20)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Run()
    EndSwitch
WEnd
 

ra4o

AutoIT Гуру
Сообщения
1,165
Репутация
246
Так ?
Код:
#include <GUIConstants.au3>
#include <Date.au3>
Global $Start = 0
Global $hTimer, $Hour, $Mins, $Secs

GUICreate('Form1', 280, 70)
$LabelTimer = GUICtrlCreateLabel('00:00:00', 20, 30, 240, 30)
GUICtrlSetFont(-1, 20, 600)

$Button1 = GUICtrlCreateButton('Старт', 20, 10, 240, 20)

GUISetState()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button1
			_Run()
	EndSwitch
	If $Start Then
		_TicksToTime(Int(TimerDiff($hTimer)), $Hour, $Mins, $Secs)
		$Timer = StringFormat("%02d:%02d:%02d", $Hour, $Mins, $Secs)
		If $Timer <> GUICtrlRead($LabelTimer) Then GUICtrlSetData($LabelTimer, $Timer)
	EndIf

WEnd

Func _Run()
	$Start = Not $Start
	$hTimer = TimerInit()
	If $Start Then
		GUICtrlSetData($Button1, 'Стоп')
	Else
		GUICtrlSetData($Button1, 'Старт')
	EndIf
EndFunc   ;==>_Run
 
A

Alofa

Гость
Или так :whistle:
Ссылка: Обновление GUI при перетаскивании

Код:
#include <Timers.au3>
#include <GUIConstants.au3>

Local $hGui, $Button1, $iLabel, $hLabel, $iTimerID, $iTimeStart
$hGui = GUICreate('Form1', 280, 70, -1, -1, $WS_OVERLAPPEDWINDOW, $WS_EX_COMPOSITED)
$Button1 = GUICtrlCreateButton('Старт', 20, 10, 240, 20)
$iLabel = GUICtrlCreateLabel('00:00:00', 20, 40, 240, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
$hLabel = GUICtrlGetHandle(-1)
GUICtrlSetFont(-1, 15)
GUISetState()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button1
			_Run()
	EndSwitch
WEnd


Func _Run()
	If $iTimerID Then
		_Timer_KillTimer($hGui, $iTimerID)
		$iTimerID = 0
		$iTimeStart = 0
		GUICtrlSetData($Button1, 'Старт')
	Else
		_Timer_Init()
		$iTimerID = _Timer_SetTimer($hGui, 990, '_UbdateLabel')
		GUICtrlSetData($iLabel, '00:00:00')
		GUICtrlSetData($Button1, 'Стоп')
	EndIf
EndFunc   ;==>_Run

Func _UbdateLabel($hWnd, $Msg, $iIDTimer, $dwTime)
	Local Static $iHour, $iMin
	If Not $iTimeStart Then
		$iTimeStart = $dwTime
		$iHour = 0
		$iMin = 0
	EndIf
	Local $iSec = Floor(($dwTime - $iTimeStart) / 1000) + 1
	If $iSec = 60 Then
		$iTimeStart += 60000
		$iSec = 0
		$iMin += 1
	EndIf
	If $iMin = 60 Then
		$iMin = 0
		$iHour += 1
	EndIf
	ControlSetText($hWnd, '', $hLabel, StringFormat("%02d:%02d:%02d", $iHour, $iMin, $iSec))
;~  или
;~  GUICtrlSetData($iLabel, StringFormat("%02d:%02d:%02d", $iHour, $iMin, $iSec))
EndFunc   ;==>_UbdateLabel
 
Автор
H

Hulk777

Новичок
Сообщения
69
Репутация
1
Оба скрипта подошли. Спасибо.

Нужно подправить.

Код:
If $iMin = 24 Then


на

Код:
If $iMin = 60 Then
 
Верх