Site icon Bytefreaks.net

On-screen clock/count down/counter — AutoHotkey (AHK) Source Code

Advertisements

Below is the source code for the version that is used for IEEE Region 8 Committee meetings.

It is written using the AutoHotkey scripting language and you will need to download the compiler for it in order for you to use it.

AutoHotkey is a free, open-source custom scripting language for Microsoft Windows, initially aimed at providing easy keyboard shortcuts or hotkeys, fast macro-creation and software automation that allows users of most levels of computer skill to automate repetitive tasks in any Windows application. User interfaces can easily be extended or modified by AutoHotkey (for example, overriding the default Windows control key commands with their Emacs equivalents). The Autohotkey installation includes its own extensive help file with an always updated web based version.From Wikipedia

Download here [download id=”1205″]

#SingleInstance Force
#Persistent
#NoEnv
DetectHiddenWindows On
SetTitleMatchMode 2

st_Configuration_File := "watch.ini"
st_Configuration_Section := "Parameters"
st_Settings_Title := "Watch 2.0 - UI Settings"
st_Mode_Title := "Watch 2.0 - Mode Settings"

;ListVars
;http://www.autohotkey.com/docs/commands/ListVars.htm

;----------------------Check if Settings File Exists-----------------
;IniRead, OutputVar, Filename, Section, Key [, Default]
st_Clock_Width := "Clock_Width"
IniRead, var_Clock_Width, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Width%, 245
st_Clock_Height := "Clock_Height"
IniRead, var_Clock_Height, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Height%, 66
st_Lock_Position := "Lock_Position"
IniRead, var_Lock_Position, %st_Configuration_File%, %st_Configuration_Section%, %st_Lock_Position%, 0
st_Always_On_Top := "Always_On_Top"
IniRead, var_Always_On_Top, %st_Configuration_File%, %st_Configuration_Section%, %st_Always_On_Top%, 1
st_Position_X := "Position_X"
st_Default_Position_X := A_ScreenWidth - var_Clock_Width
IniRead, var_Position_X, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_X%, %st_Default_Position_X%
st_Position_Y := "Position_Y"
st_Default_Position_Y := A_ScreenHeight - var_Clock_Height
IniRead, var_Position_Y, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_Y%, %st_Default_Position_Y%
st_Click_Through_Clock := "Click_Through_Clock"
IniRead, var_Click_Through_Clock, %st_Configuration_File%, %st_Configuration_Section%, %st_Click_Through_Clock%, 0
st_Back_Color := "Back_Color"
IniRead, var_Back_Color, %st_Configuration_File%, %st_Configuration_Section%, %st_Back_Color%, 0x000000
st_Clock_Color := "Clock_Color"
IniRead, var_Clock_Color, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Color%, 0xECECEE
st_Clock_Font_Size := "Clock_Font_Size"
IniRead, var_Clock_Font_Size, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Font_Size%, 28
st_Clock_Mode := "Clock_Mode"
IniRead, var_Clock_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%, 24Hour
st_Timer_Mode := "Timer_Mode"
IniRead, var_Timer_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%, FixedTime
st_Transparency := "Transparency"
IniRead, var_Transparency, %st_Configuration_File%, %st_Configuration_Section%, %st_Transparency%, 230
st_Watch_Mode := "Watch_Mode"
;IniRead, var_Watch_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%, Clock
;Xeirwn -- Hardcoded clock as default mode
var_Watch_Mode := "Clock"
SetTimer, fun_TicTac, 1000
;------------------------------------------------------------

Gui, 1:+ToolWindow -Caption
Gui, 1:Color, %var_Back_Color%, %var_Back_Color%
Gui, 1:Font, S%var_Clock_Font_Size%
Gui, 1:Font, C%var_Clock_Color%, bold, Arial
Gui, 1:Font, bold
Gui, 1:Font, Arial
FormatTime, var_Formatted_Time, , '  'HH:mm:ss
Gui, 1:Add, text, X5 Y5 W%var_Clock_Width% H%var_Clock_Height% GMove vElem_Clock, %var_Formatted_Time%
Gui, 1:Show, X%var_Position_X% Y%var_Position_Y% W%var_Clock_Width% H%var_Clock_Height%, elem_Watch_Window
WinSet, Region, 0-0 W%var_Clock_Width% H%var_Clock_Height% R6-6, elem_Watch_Window
;;WinSet, Region, 0-0 W%var_Clock_Width% H%var_Clock_Height% R6-6, elem_Watch_Window
WinSet, Transparent, %var_Transparency%, elem_Watch_Window

ifExist, watch.ico
{
	Menu, Tray, Icon, watch.ico
}
Menu, Tray, Tip, Watch 1.0
Menu, Tray, NoStandard
Menu, Tray, add, &Always On Top, fun_Set_Always_On_Top
Menu, Tray, add, Click &Through, fun_Click_Through
;Menu, Tray, add, &Lock Position, fun_Lock_Position
Menu, Tray, add, Clock &Colors, fun_Set_Clock_Colors
Menu, Tray, add, &Set Mode, fun_Set_Mode
Menu, Tray, add
Menu, Tray, add, &Quit Clock, ExitApp
Menu, Tray, add, Hide/&Show, fun_Toggle_Hide
Menu, Tray, add
Menu, Tray, add, About, fun_About
Menu, Tray, default, Hide/&Show

if (var_Click_Through_Clock = "1")
{
	Gui, 1:+E0x20
	Menu, Tray, Check, Click &Through
}

if (var_Always_On_Top = "1")
{
	WinSet, AlwaysOnTop, On, elem_Watch_Window
	Menu, Tray, Check, &Always On Top
}

if (var_Lock_Position = "1")
{
	;TODO -- XEIRWN -- REVIEW
	Gui, 1:-E0x10
	Menu, Tray, Check, &Lock Position
}

;----------------------Set Transparency Using the Alt Key--------------------
#ifWinActive, elem_Watch_Window
{
	!WheelUp::
	if (var_Transparency <= 230) { var_Transparency += 5 WinSet, Transparent, %var_Transparency%, elem_Watch_Window return } !WheelDown:: if (var_Transparency >= 15)
	{
		var_Transparency -= 5
		WinSet, Transparent, %var_Transparency%, elem_Watch_Window
		return
	}

	return
}
;------------------------------------------------------------
return

;;TODO -- XEIRWN -- REVIEW
;----------------------Enable Window Drag-------------------
Move:
	PostMessage, 0xA1, 2,,, A
	if A_GuiEvent = DoubleClick
	{
		GoSub, fun_Show_Hide
	}
return
;------------------------------------------------------------

;----------------------Clock Engine-----------------------
fun_TicTac:
	if (var_Watch_Mode = "Clock")
	{
		FormatTime, var_Formatted_Value, , '  'HH:mm:ss
		if (var_Clock_Mode = "AM/PM")
		{
			FormatTime, var_Formatted_Value, , hh:mm:ss tt
		}
	}
	else if (var_Watch_Mode = "Timer")
	{
		if (var_Timer_Mode = "FixedTime")
		{
			FormatTime, var_Seconds, %var_Set_FixedTime%, s
			FormatTime, var_Minutes, %var_Set_FixedTime%, m
			FormatTime, var_Hours, %var_Set_FixedTime%, H
			if (var_Seconds > "0" or var_Minutes > "0" or var_Hours > "0")
			{
				var_Set_FixedTime += -1, seconds
			}
			else
			{
				SetTimer, fun_TicTac, off
			}
			FormatTime, var_Formatted_Value, %var_Set_FixedTime%, '  'HH:mm:ss
		}
		else
		{
			;TODO Xeirwn -- Implement count to date time
			;http://www.autohotkey.com/docs/commands/EnvSub.htm
			;http://www.autohotkey.com/docs/commands/FormatTime.htm
			;http://www.autohotkey.com/docs/Variables.htm
		}
	}
	GuiControl,, Elem_Clock, %var_Formatted_Value%
	return
;------------------------------------------------------------

;----------------------Clock Engine Maintenance-----------------------
fun_TicTacToc:
	if (var_Watch_Mode = "Clock")
	{
	}
	else if (var_Watch_Mode = "Timer")
	{
		if (var_Timer_Mode = "FixedTime")
		{
			FormatTime, var_Formatted_Value, %var_Set_FixedTime%, '  'HH:mm:ss
		}
	}
	GuiControl,, Elem_Clock, %var_Formatted_Value%
	return
;------------------------------------------------------------

;---------------------Show Or Hide Clock---------------------
fun_Show_Hide:
fun_Toggle_Hide:
	var_Toggle := !var_Toggle
	if (var_Toggle = "1")
	{
		WinHide, elem_Watch_Window
	}
	else
	{
		WinShow, elem_Watch_Window
	}
return
;------------------------------------------------------------

;---------------------Don't Save Position--------------------
fun_Lock_Position:
	if (var_Lock_Position = "1")
	{
		;XEIRWN -- TODO -- REVIEW
		Gui, 1:-E0x10
	}
	else
	{
		;XEIRWN -- TODO -- REVIEW
		Gui, 1:+E0x10
	}
	var_Lock_Position := !var_Lock_Position
	Menu, tray, togglecheck, &Lock Position
	IniWrite, %var_Lock_Position%, %st_Configuration_File%, %st_Configuration_Section%, %st_Lock_Position%
return
;------------------------------------------------------------

;----------------------Set Clock Colors----------------------
fun_Set_Clock_Colors:
	SPosX := (A_ScreenWidth)/2 
	SPosY := (A_ScreenHeight)/2
	Gui, 2:Color, black
	Gui, 2:+ToolWindow +AlwaysOnTop -Caption
	Gui, 2:Font, S7 cwhite, Verdana
	Gui, 2:Add, Text, x6 y6 w130 h20 gMove, %st_Settings_Title%
	Gui, 2:Add, Text, x148 y6 w20 h20 gGui2Close, X
	Gui, 2:Add, GroupBox, x6 y25 w150 h75 , Colors
	Gui, 2:Add, Text, x16 y45 w80 h20 , Background:
	Gui, 2:Add, Button, x96 y45 w55 h17 vBack_Color_Btn gfun_Set_Back_Color, %var_Back_Color%
	Gui, 2:Add, Text, x16 y75 w100 h20 , Clock Color:
	Gui, 2:Add, Button, x96 y75 w55 h17 vClock_Color_Btn gfun_Set_Clock_Color, %var_Clock_Color%
	Gui, 2:Add, GroupBox, x6 y103 w150 h38 Right, Transparency
	Gui, 2:Add, Slider, x16 y118 w130 h18 Range15-230 gfun_Set_Transparency vTrans_Value, %var_Transparency%
	Gui, 2:Show, X%SPosX% Y%SPosY% W163 H152, %st_Settings_Title%
	WinSet, Region, 0-0 w163 h152 R10-10, elem_Colors_Window
	WinSet, Transparent, 237, elem_Colors_Window
return

;----------------------Control Mode----------------------
;TODO -- Check the variables and set the appropriate radio elements as checked
fun_Set_Mode:
	SPosX := (A_ScreenWidth)/2 
	SPosY := (A_ScreenHeight)/2
	Gui, 3:Color, black
	Gui, 3:+ToolWindow +AlwaysOnTop -Caption
	Gui, 3:Font, S7 cwhite, Verdana
	Gui, 3:Add, Text, x6 y6 w140 h20 gMove, %st_Mode_Title%
	Gui, 3:Add, Text, x180 y6 w20 h20 gGui3Close, X
	Gui, 3:Add, Radio, x16 y25 gfun_ClockMode Checked, Clock mode.
	Gui, 3:Add, Radio, x106 y25 gfun_TimerMode, Timer mode.
	;Xeirwn -- When selecting timer mode, set the content to something
	Gui, 3:Add, GroupBox, x6 y55 w177 h75 , Clock
	Gui, 3:Add, Radio, x16 y75 gfun_ClockMode24Hour Checked, 24 Hour mode.
	Gui, 3:Add, Radio, x16 y105 gfun_ClockModeAMPM, AM/PM mode.
	Gui, 3:Add, GroupBox, x6 y133 w177 h200 Right, Timer
	;Gui, 3:Add, Radio, x16 y153 gfun_TimerModeToDateTime, Count to datetime.
	Gui, 3:Add, Radio, x16 y223 gfun_TimerModeFixedTime Checked, Count fixed time.
	;Gui, 3:Add, DateTime, x16 y173 wp20 h20 vToDateTime, d/M/yy HH:mm:ss
	Gui, 3:Add, DateTime, x16 y243 wp20 h20 vFixedTime, HH:mm:ss
	;TODO -- Set Fixed Time to some other value other than the current
	Gui, 3:Add, Button, w55 gfun_Timer_Continue, &Continue
	;Xeirwn -- When pressing many times the continue, it delays the execution as it resets it
	;http://www.autohotkey.com/docs/commands/SetTimer.htm
	Gui, 3:Add, Button, xp55 wp gfun_TimerPause, &Pause
	Gui, 3:Add, Button, xp55 wp gfun_TimerLap Default, &Lap
	Gui, 3:Add, Button, x16 yp30 w55 gfun_TimerReset, &Reset
	Gui, 3:Add, Button, xp60 w55 gfun_TimerZero, &Zero
	Gui, 3:Show, X%SPosX% Y%SPosY% W193 H345, %st_Mode_Title%
	WinSet, Region, 0-0 w193 h345 R10-10, st_Mode_Title
	WinSet, Transparent, 237, Watch 1.0 - Mode Settings
return

;-----------------Continue Timer-----------------
fun_Timer_Continue:
	Gui, Submit, NoHide
	SetTimer, fun_TicTac, on
return

;-----------------Pause Timer-----------------
fun_TimerPause:
	Gui, Submit, NoHide
	SetTimer, fun_TicTac, off
return

;-----------------Timer Lap-----------------
fun_TimerLap:
	Gui, Submit, NoHide
	var_Set_FixedTime = %FixedTime%
	IniWrite, %FixedTime%, %st_Configuration_File%, %st_Configuration_Section%, FixedTime
	SetTimer, fun_TicTac, on
	SetTimer, fun_TicTacToc, -1
return

;-----------------Reset Timer-----------------
fun_TimerReset:
	Gui, Submit, NoHide
	var_Set_FixedTime = %FixedTime%
	IniWrite, %FixedTime%, %st_Configuration_File%, %st_Configuration_Section%, FixedTime
	SetTimer, fun_TicTac, off
	SetTimer, fun_TicTacToc, -1
return

;-----------------Zero Timer-----------------
fun_TimerZero:
	Gui, Submit, NoHide
	var_Set_FixedTime = 19700101000000
	SetTimer, fun_TicTac, off
	SetTimer, fun_TicTacToc, -1
return

;-----------------Set Clock Mode-----------------
fun_ClockMode:
	Gui, Submit, NoHide
	var_Watch_Mode := "Clock"
	IniWrite, %var_Watch_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%
	SetTimer, fun_TicTac, on
return

;-----------------Set Timer Mode-----------------
fun_TimerMode:
	Gui, Submit, NoHide
	var_Watch_Mode := "Timer"
	IniWrite, %var_Watch_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%
	SetTimer, fun_TicTac, off
return

;-----------------Set AM/PM Clock Mode-----------------
fun_ClockModeAMPM:
	Gui, Submit, NoHide
	var_Clock_Mode := "AM/PM"
	IniWrite, %var_Clock_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%
return

;-----------------Set 24Hour Clock Mode-----------------
fun_ClockMode24Hour:
	Gui, Submit, NoHide
	var_Clock_Mode := "24Hour"
	IniWrite, %var_Clock_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%
return

;-----------------Set To DateTime Timer Mode-----------------
fun_TimerModeToDateTime:
	Gui, Submit, NoHide
	var_Timer_Mode := "ToDateTime"
	IniWrite, %var_Timer_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%
return

;-----------------Set To Fixed Time Timer Mode-----------------
fun_TimerModeFixedTime:
	Gui, Submit, NoHide
	var_Timer_Mode := "FixedTime"
	IniWrite, %var_Timer_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%
return

;----------------------Close Settings Window----------------------
Gui2Close:
	Gui, 2:Destroy
return

;----------------------Close Settings Window----------------------
Gui3Close:
	Gui, 3:Destroy
return

;-----------------Set Clock Background Color-----------------
fun_Set_Back_Color:
	Gui, Submit, NoHide
	var_Back_Color := RGB(dlg_color(var_Clock_Color, hwnd))
	if var_Back_Color <>
	{
		Gui, 1:Color, %var_Back_Color%
		GuiControl, 2:, Back_Color_Btn, %var_Back_Color%
		IniWrite, %var_Back_Color%, %st_Configuration_File%, %st_Configuration_Section%, %st_Back_Color%
	}
return

;-----------------------Set Clock Color----------------------
fun_Set_Clock_Color:
	Gui, Submit, NoHide
	var_Clock_Color := RGB(dlg_color(var_Clock_Color, hwnd))
	if var_Clock_Color <>
	{
		IniWrite, %var_Clock_Color%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Color%
		Gui, 1:Font, c%var_Clock_Color%
		GuiControl, 1:Font, Elem_Clock
		GuiControl, 2:, Clock_Color_Btn, %var_Clock_Color%
	}
return
;------------------------------------------------------------

;-----------------------Set Transparency----------------------
fun_Set_Transparency:
	Gui, Submit, NoHide
	var_Transparency := Trans_Value
	WinSet, Transparent, %var_Transparency%, elem_Watch_Window
	IniWrite, %var_Transparency%, %st_Configuration_File%, %st_Configuration_Section%, %st_Transparency%
return
;------------------------------------------------------------

;-----------------------Set AlwaysOnTop----------------------
fun_Set_Always_On_Top:
	if (var_Always_On_Top = "1")
	{
		WinSet, AlwaysOnTop, Off, elem_Watch_Window
	}
	else
	{
		WinSet, AlwaysOnTop, On, elem_Watch_Window
	}
	var_Always_On_Top := !var_Always_On_Top
	Menu, tray, togglecheck, &Always On Top
	IniWrite, %var_Always_On_Top%, %st_Configuration_File%, %st_Configuration_Section%, %st_Always_On_Top%
return
;------------------------------------------------------------

;-----------------------Exit Application----------------------
ExitApp:
	GoSub, fun_Save_Position
	ExitApp
return

;---------------------Save Clock Position--------------------
fun_Save_Position:
	WinGetPos, %var_Position_X%, %var_Position_Y%,,, elem_Watch_Window
	IniWrite, %var_Position_X%, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_X%
	IniWrite, %var_Position_Y%, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_Y%
return
;------------------------------------------------------------

;------------------Click Through Clock Window-----------------
fun_Click_Through:
	var_Click_Through_Clock := !var_Click_Through_Clock
	if (var_Click_Through_Clock == "1")
	{
		Gui, 1: +E0x20
	} else {
		Gui, 1: -E0x20
	}
	Menu, tray, togglecheck, Click &Through
	IniWrite, %var_Click_Through_Clock%, %st_Configuration_File%, %st_Configuration_Section%, %st_Click_Through_Clock%
return
;------------------------------------------------------------

;http://www.autohotkey.com/board/topic/94083-ahk-11-font-and-color-dialogs/
Dlg_Color(Color,hwnd){
	static
	if !cc{
		VarSetCapacity(CUSTOM,16*A_PtrSize,0),cc:=1,size:=VarSetCapacity(CHOOSECOLOR,9*A_PtrSize,0)
		Loop,16{
			IniRead,col,color.ini,color,%A_Index%,0
			NumPut(col,CUSTOM,(A_Index-1)*4,"UInt")
		}
	}
	NumPut(size,CHOOSECOLOR,0,"UInt"),NumPut(hwnd,CHOOSECOLOR,A_PtrSize,"UPtr")
	,NumPut(Color,CHOOSECOLOR,3*A_PtrSize,"UInt"),NumPut(3,CHOOSECOLOR,5*A_PtrSize,"UInt")
	,NumPut(&CUSTOM,CHOOSECOLOR,4*A_PtrSize,"UPtr")
	ret:=DllCall("comdlg32\ChooseColor","UPtr",&CHOOSECOLOR,"UInt")
	if !ret
	exit
	Loop,16
	IniWrite,% NumGet(custom,(A_Index-1)*4,"UInt"),color.ini,color,%A_Index%
	IniWrite,% Color:=NumGet(CHOOSECOLOR,3*A_PtrSize,"UInt"),color.ini,default,color
	return Color
}

rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat,IntegerFast,D
	return c
}

fun_About:
	applicationName=Watch
	Gui,99:Destroy
	Gui,99:Margin,20,20
	Gui,99:Add,Picture,xm Icon1,%applicationName%.exe
	Gui,99:Font,Bold
	Gui,99:Add,Text,x+10 yp+10,%applicationName% v1.0
	Gui,99:Font
	Gui,99:Add,Text,y+10,This application will create a widget that will have the functionality to show the time or operate as a countdown timer

	Gui,99:Add,Picture,xm y+20 Icon5,%applicationName%.exe
	Gui,99:Font,Bold
	Gui,99:Add,Text,x+10 yp+10,Software by George Michael
	Gui,99:Font
	Gui,99:Add,Text,y+10,For more tools and information, please visit 
	Gui,99:Font,CBlue Underline
	Gui,99:Add,Text,y+5 GBYTEFREAKSNET,www.bytefreaks.net
	Gui,99:Font

	Gui,99:Show,,%applicationName% fun_About
	hCurs:=DllCall("LoadCursor","UInt",NULL,"Int",32649,"UInt") ;IDC_HAND
	OnMessage(0x200,"WM_MOUSEMOVE") 
	return

BYTEFREAKSNET:
	Run,http://www.bytefreaks.net,,UseErrorLevel
	return

Key features

This post is also available in: Greek

Exit mobile version