Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Clock with Seconds

Description
This is a simple clock, I usally use for mensure performance and times when programming, so the clock need to be light n zippy.
Position is not automatically set, please use a custom fuction/trigger of DisplayFusion for set the clock location.
Language
C#.net
Minimum Version
Created By
Renan Gonçalves Micheletti
Contributors
-
Date Created
Apr 19, 2023
Date Last Modified
Apr 19, 2023

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;

//version 1.0
//Know-Issues:Position is not automatically set, please use a custom fuction of DisplayFusion for set the clock location
public static class DisplayFusionFunction
{
	static Label hour = new Label();
	static Label seconds = new Label();
	static int hh;
	static int MM;
	static int ss;
	
	public static void Run(IntPtr windowHandle)
	{			
		Form form = new Form();
		form.TopMost = true;
		form.StartPosition = FormStartPosition.Manual;
		form.ShowIcon = false;
		form.FormBorderStyle = FormBorderStyle.None;
		form.BackColor = System.Drawing.SystemColors.ControlText;
		form.Width = 48;
		form.Height = 19;
		form.MinimumSize = new System.Drawing.Size(48, 19);
		
		hour.AutoSize = true;
        hour.ForeColor = System.Drawing.SystemColors.ControlLightLight;
        hour.Location = new System.Drawing.Point(-1, 2);
        hour.Size = new System.Drawing.Size(30, 13);
		hour.Parent = seconds;
		hour.BackColor = System.Drawing.Color.Transparent;
		
		seconds.AutoSize = true;
        seconds.ForeColor = System.Drawing.SystemColors.ControlLightLight;
        seconds.Location = new System.Drawing.Point(31, 2);
		seconds.BackColor = System.Drawing.Color.Transparent;
        seconds.Size = new System.Drawing.Size(18, 13);
		
		form.Controls.Add(seconds);
		form.Controls.Add(hour);
		
		 System.Reflection.PropertyInfo aProp = 
         typeof(System.Windows.Forms.Control).GetProperty(
               "DoubleBuffered", 
               System.Reflection.BindingFlags.NonPublic | 
               System.Reflection.BindingFlags.Instance);

   		aProp.SetValue(seconds, true, null); 
		aProp.SetValue(hour, true, null); 
		
		hh = DateTime.Now.Hour;
		MM = DateTime.Now.Minute;
		ss = DateTime.Now.Second;
		
		changeHour();
		
		Task.Factory.StartNew(() =>
		{
			while(true)
			{
				Thread.Sleep(998);
				Elapsed();
			}
			
		});
				
		form.ShowDialog();		
	}
	
	static Action changeHour = () => hour.Text = hh.ToString("00")+":"+MM.ToString("00")+":";
	static Action changeSeconds = () => seconds.Text = ss.ToString();//Need to put a minimum of effort into changing seconds
	
	private static void Elapsed()
	{
		if(++ss == 60)
		{
			ss = 0;
			if(++MM == 60)//Just to make sure, we reset counters here, so ms errors don't turn into seconds, then minutes, and so on
			{
				var date = DateTime.Now;
				MM = date.Minute;
				hh = date.Hour;
				
				//Order is important, because every millisecond counts
				ss = DateTime.Now.Second;
				if (ss == 60)//If it is exactly 60, we need to reset it.
					ss = 0;
			}
			hour.Invoke(changeHour);
		}
		seconds.Invoke(changeSeconds);
	}
}