Write a Calendar in Autohotkey

 In this case, I want to display the current month with today highlighted.  As I was trying to get my head round writing this in a program using the qHTM.dll (to include HTML in an autohotkey GUI), the calendar will be in a HTML-autohotkey mixed code.  Obviously just omit the HTML rubbish if you want to create a calendar using just autohotkey syntax.

Actually, the following code is an excerpt from my program which checks an online server (for IT Events during this month) and if it can't download the calendar, it had to display an offline version in the GUI.  To display the following code in this website though, I've had to omit a lot of the HTML part.

This is the code in full; apologies for the mess coding but it's a mix between HTML and autohotkey syntax (note we're making a table with 9 columns and 6 rows - there's a spacing column on either side):

htmlText:=""
htmlText:=htmlText "<table>"
htmlText:=htmlText "<tr>"
htmlText:=htmlText "	<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>M</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>T</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>W</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>T</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>F</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>S</font></td>"
htmlText:=htmlText "    	<td><font color='#cccccc'>S</font></td>"
htmlText:=htmlText "    	<td><img src='" A_ScriptDir "\images\s.gif' width='10' height='1' /></td>"
htmlText:=htmlText "</tr>"

; GET NUMBER OF DAYS THIS MONTH
; Haven't found anyone who calculates how many days there are in a month so this is a crude solution
; This checks if this year is a leap year
Check1:=Mod(A_YYYY,4)
Check2:=Mod(A_YYYY,100)
Check3:=Mod(A_YYYY,400)
if (Check1=0) AND (((Check2=0)and(Check3=0)) OR (Check2<>0))
	DaysInMonth=31,29,31,30,31,30,31,31,30,31,30,31
else
	DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
ThisMonthNum:=A_MM * 1	; because we want 01 to equal 1
ThisDateNum:=A_DD * 1	; because we want 01 to equal 1
StringSplit, DaysPerMonth, DaysInMonth, `,
DaysInThisMonth:=DaysPerMonth%ThisMonthNum%

; get the first day-of-week of this month
FormatTime, firstDayBlankNum, %A_YYYY%%A_MM%1, WDay

; add blank spaces in front of this first day
dateToDisplay:=1
If (firstDayBlankNum<3)
{
	If (firstDayBlankNum=2) ; a day reference cannot be 0, and we're about subtract 2 from this
		firstDayBlankNum:=7
	If (firstDayBlankNum=1) ; a day reference cannot be less than 1, and we're about subtract 2 from this
		firstDayBlankNum:=6
} else 
	firstDayBlankNum-=2		; because my week starts with Monday

daysLeftOnFirstRow:=7-firstDayBlankNum

; write the first row
ThisMonth:=ThisMonth "		<tr>"
ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
Loop, %firstDayBlankNum%
	ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
Loop, %daysLeftOnFirstRow%
{
	If (dateToDisplay=ThisDateNum)
		ThisMonth:=ThisMonth "			<td background='" A_ScriptDir "/images/program/bgs/calendar_today.png'><font color='#ffffff'><b>" dateToDisplay "</b></font></td></td>"
	Else
		ThisMonth:=ThisMonth "			<td>" dateToDisplay "</td>"
	dateToDisplay++
}
ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
ThisMonth:=ThisMonth "		</tr>"

; write the remaining rows
Loop, 5
{
	ThisMonth:=ThisMonth "		<tr>"
	ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
	Loop, 7
	{
		If (dateToDisplay<=DaysInThisMonth)
		{
			If (dateToDisplay=ThisDateNum)
				ThisMonth:=ThisMonth "			<td background='" A_ScriptDir "/images/program/bgs/calendar_today.png'><font color='#ffffff'><b>" dateToDisplay "</b></font></td></td>"
			Else
				ThisMonth:=ThisMonth "			<td>" dateToDisplay "</td>"
		} Else
			ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
			dateToDisplay++
	}
	ThisMonth:=ThisMonth "			<td><img src='" A_ScriptDir "\images\s.gif' width='15' height='1' /></td>"
	ThisMonth:=ThisMonth "		</tr>"
}
htmlText:=htmlText "</table>"

Easy-peasy hey???

Category: AutoHotkey :: Article: 234

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Please publish modules in offcanvas position.