Skip to content

Commit

Permalink
add .net 8 project
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 28, 2024
1 parent 38acf06 commit 212fa6c
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nager.WindowsCalendarWeek", "Nager.WindowsCalendarWeek\Nager.WindowsCalendarWeek.csproj", "{1077449C-969D-4055-B767-990893D4D462}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nager.WindowsCalendarWeekNet4", "Nager.WindowsCalendarWeekNet4\Nager.WindowsCalendarWeekNet4.csproj", "{2FE40F04-C6C4-447D-AF43-3866D2935441}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{1077449C-969D-4055-B767-990893D4D462}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1077449C-969D-4055-B767-990893D4D462}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1077449C-969D-4055-B767-990893D4D462}.Release|Any CPU.Build.0 = Release|Any CPU
{2FE40F04-C6C4-447D-AF43-3866D2935441}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FE40F04-C6C4-447D-AF43-3866D2935441}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FE40F04-C6C4-447D-AF43-3866D2935441}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FE40F04-C6C4-447D-AF43-3866D2935441}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions src/Nager.WindowsCalendarWeek/Nager.WindowsCalendarWeek.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>nagerdate.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<Content Include="nagerdate.ico" />
</ItemGroup>

</Project>
143 changes: 143 additions & 0 deletions src/Nager.WindowsCalendarWeek/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Diagnostics;
using System.Globalization;

namespace Nager.WindowsCalendarWeek;

static class Program
{
private static System.Windows.Forms.Timer RefreshIconTimer;
private static NotifyIcon CalendarWeekNotifyIcon;
private static int OneDayMilliseconds = 86400000;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var firstRefreshInterval = (int)(OneDayMilliseconds - Math.Floor(DateTime.Now.TimeOfDay.TotalMilliseconds));

RefreshIconTimer = new System.Windows.Forms.Timer();
RefreshIconTimer.Interval = firstRefreshInterval;
RefreshIconTimer.Tick += new EventHandler(RefreshIconTimer_Tick);
RefreshIconTimer.Start();

var bmp = GetBitmapOfCurrentCalendarWeek();

CalendarWeekNotifyIcon = new NotifyIcon
{
Icon = Icon.FromHandle(bmp.GetHicon()),
ContextMenuStrip = new ContextMenuStrip
{
Items =
{
new ToolStripMenuItem("Online Kalender", null, new EventHandler(OnlineCalendarItemClickEvent), "ToolStripMenuItem_OnlineCalendar"),
new ToolStripMenuItem("Feiertage", null, new EventHandler(HolidayItemClickEvent), "ToolStripMenuItem_Holiday"),
new ToolStripMenuItem("Quit", null, new EventHandler(QuitItemClickEvent), "ToolStripMenuItem_Quit")
}
},
Visible = true
};

CalendarWeekNotifyIcon.MouseClick += TrayIcon_Click;

Application.Run();
}

private static void RefreshIconTimer_Tick(object sender, EventArgs e)
{
RefreshIconTimer.Interval = OneDayMilliseconds;

var oldIcon = CalendarWeekNotifyIcon.Icon;
if (oldIcon != null)
{
oldIcon.Dispose();
}

var bmp = GetBitmapOfCurrentCalendarWeek();
CalendarWeekNotifyIcon.Icon = Icon.FromHandle(bmp.GetHicon());
}

private static void TrayIcon_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
OpenOnlineCalendar();
}
}

private static void OnlineCalendarItemClickEvent(object sender, EventArgs e)
{
OpenOnlineCalendar();
}

private static void HolidayItemClickEvent(object sender, EventArgs e)
{
OpenHoliday();
}

private static void OpenOnlineCalendar()
{
// show first or second half of the year
var ext = DateTime.Today.Month > 6 ? "-2" : "";

var target = $"https://kalenderwoche.at/kalender/{DateTime.Today.Year}{ext}";

OpenBrowser(target);
}

private static void OpenHoliday()
{
OpenBrowser("https://date.nager.at");
}

private static void OpenBrowser(string url)
{
var processStartInfo = new ProcessStartInfo("cmd", $"/c start {url}")
{
CreateNoWindow = true
};

Process.Start(processStartInfo);
}

private static Bitmap GetBitmapOfCurrentCalendarWeek()
{
var width = 16;
var height = 16;
var text = GetWeekNumber().ToString();

var bitmap = new Bitmap(width, height);
using (var font = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Point))
{
using (var drawBrush = new SolidBrush(Color.White))
{
using (var stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

using (var graphics = Graphics.FromImage(bitmap))
{
var drawingArea = new Rectangle(0, 0, width, height);

graphics.FillRectangle(Brushes.Black, 0, 0, width, height);
graphics.DrawString(text, font, drawBrush, drawingArea, stringFormat);
}
}
}
}

return bitmap;
}

private static int GetWeekNumber()
{
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

private static void QuitItemClickEvent(object sender, EventArgs e)
{
Application.Exit();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1077449C-969D-4055-B767-990893D4D462}</ProjectGuid>
<ProjectGuid>{2FE40F04-C6C4-447D-AF43-3866D2935441}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>Nager.WindowsCalendarWeek</RootNamespace>
<AssemblyName>Nager.WindowsCalendarWeek</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
Expand Down Expand Up @@ -84,13 +85,23 @@ private static void OpenOnlineCalendar()
var ext = DateTime.Today.Month > 6 ? "-2" : "";

var target = $"https://kalenderwoche.at/kalender/{DateTime.Today.Year}{ext}";
System.Diagnostics.Process.Start(target);

OpenBrowser(target);
}

private static void OpenHoliday()
{
var target = $"https://date.nager.at/";
System.Diagnostics.Process.Start(target);
OpenBrowser("https://date.nager.at");
}

private static void OpenBrowser(string url)
{
var processStartInfo = new ProcessStartInfo("cmd", $"/c start {url}")
{
CreateNoWindow = true
};

Process.Start(processStartInfo);
}

private static Bitmap GetBitmapOfCurrentCalendarWeek()
Expand Down
Binary file added src/Nager.WindowsCalendarWeekNet4/nagerdate.ico
Binary file not shown.

0 comments on commit 212fa6c

Please sign in to comment.