From 0183dc6631df7f896373ae762e794fdc3e086427 Mon Sep 17 00:00:00 2001 From: mbnq Date: Wed, 16 Oct 2024 13:05:40 +0200 Subject: [PATCH] notification timer --- Program.cs | 2 +- core/mbGUI.cs | 19 ++++++- core/mbNotification.cs | 118 ++++++++++++++++++++++------------------- 3 files changed, 83 insertions(+), 56 deletions(-) diff --git a/Program.cs b/Program.cs index 2b92ca6..b1adf4b 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,7 @@ internal static class Program static Mutex redunDancerMutex = new Mutex(true, "{readunDancer}"); #region variables and constants - public const string mbVersion = "0.0.1.6"; + public const string mbVersion = "0.0.1.7"; #endregion #region DPI diff --git a/core/mbGUI.cs b/core/mbGUI.cs index f6358c1..529f15e 100644 --- a/core/mbGUI.cs +++ b/core/mbGUI.cs @@ -121,7 +121,7 @@ private void checkIPSettings() LogPingResult($"Settings validated correctly: {isConfigCorrect}", true); } - public void PopUpNotification(string title, string message, int timeout) + public void PopUpNotification_old(string title, string message, int timeout) { if (showNotifications) { @@ -129,6 +129,23 @@ public void PopUpNotification(string title, string message, int timeout) notification.Show(); } } + + public void PopUpNotification(string title, string message, int timeout) + { + if (showNotifications) + { + if (this.InvokeRequired) + { + this.Invoke(new Action(() => PopUpNotification(title, message, timeout))); + } + else + { + mbNotificationForm notification = new mbNotificationForm(title, message, timeout); + notification.Show(); + } + } + } + private void StartCheckboxLock() { if (int.TryParse(mbTestPingIntervalTextBox.Text, out int interval)) diff --git a/core/mbNotification.cs b/core/mbNotification.cs index 765bcd1..576ef98 100644 --- a/core/mbNotification.cs +++ b/core/mbNotification.cs @@ -1,5 +1,4 @@ - -/* +/* www.mbnq.pl 2024 https://mbnq.pl/ @@ -7,69 +6,80 @@ mbnq00 on gmail */ -using redunDancer; -public class mbNotificationForm : Form -{ - private Label mbTitleLabel, mbMessageLabel; +using System; +using System.Drawing; +using System.Windows.Forms; - public mbNotificationForm(string title, string message, int timeout = 3000) +namespace redunDancer +{ + public class mbNotificationForm : Form { + private Label mbTitleLabel, mbMessageLabel; + private System.Windows.Forms.Timer closeTimer; - this.Size = new Size(200, 100); - this.StartPosition = FormStartPosition.Manual; - this.FormBorderStyle = FormBorderStyle.FixedToolWindow; - this.TopMost = true; - this.ShowInTaskbar = false; + public mbNotificationForm(string title, string message, int timeout = 3000) + { + this.Size = new Size(200, 100); + this.StartPosition = FormStartPosition.Manual; + this.FormBorderStyle = FormBorderStyle.FixedToolWindow; + this.TopMost = true; + this.ShowInTaskbar = false; - mbTitleLabel = new Label(); - mbTitleLabel.Text = title; - mbTitleLabel.AutoSize = false; - mbTitleLabel.TextAlign = ContentAlignment.MiddleCenter; - mbTitleLabel.Dock = DockStyle.Top; - mbTitleLabel.Font = new Font("Consolas", 10, FontStyle.Regular); - mbTitleLabel.Height = 20; - mbTitleLabel.BackColor = Color.Gray; + mbTitleLabel = new Label(); + mbTitleLabel.Text = title; + mbTitleLabel.AutoSize = false; + mbTitleLabel.TextAlign = ContentAlignment.MiddleCenter; + mbTitleLabel.Dock = DockStyle.Top; + mbTitleLabel.Font = new Font("Consolas", 10, FontStyle.Regular); + mbTitleLabel.Height = 20; + mbTitleLabel.BackColor = Color.Gray; - mbMessageLabel = new Label(); - mbMessageLabel.Text = message; - mbMessageLabel.AutoSize = false; - mbMessageLabel.TextAlign = ContentAlignment.MiddleCenter; - mbMessageLabel.Dock = DockStyle.Fill; - mbMessageLabel.Font = new Font("Consolas", 10); + mbMessageLabel = new Label(); + mbMessageLabel.Text = message; + mbMessageLabel.AutoSize = false; + mbMessageLabel.TextAlign = ContentAlignment.MiddleCenter; + mbMessageLabel.Dock = DockStyle.Fill; + mbMessageLabel.Font = new Font("Consolas", 10); - // --- - this.Controls.Add(mbTitleLabel); - this.Controls.Add(mbMessageLabel); + this.Controls.Add(mbTitleLabel); + this.Controls.Add(mbMessageLabel); - int screenWidth = Screen.PrimaryScreen.WorkingArea.Width; - int screenHeight = Screen.PrimaryScreen.WorkingArea.Height; - this.Location = new Point(screenWidth - this.Width - 10, screenHeight - this.Height - 10); + int screenWidth = Screen.PrimaryScreen.WorkingArea.Width; + int screenHeight = Screen.PrimaryScreen.WorkingArea.Height; + this.Location = new Point(screenWidth - this.Width - 10, screenHeight - this.Height - 10); - this.Click += (s, e) => this.Hide(); - mbTitleLabel.Click += (s, e) => this.Hide(); - mbMessageLabel.Click += (s, e) => this.Hide(); + this.Click += (s, e) => this.Hide(); + mbTitleLabel.Click += (s, e) => this.Hide(); + mbMessageLabel.Click += (s, e) => this.Hide(); - this.Show(); - mbKillWithDelay(timeout); - } - private async void mbKillWithDelay(int timeout) - { - await Task.Delay(timeout); - this.Close(); - } + closeTimer = new System.Windows.Forms.Timer(); + closeTimer.Interval = timeout; + closeTimer.Tick += CloseTimer_Tick; + closeTimer.Start(); + } - // --- - protected override bool ShowWithoutActivation - { - get { return true; } - } - protected override CreateParams CreateParams - { - get + private void CloseTimer_Tick(object sender, EventArgs e) + { + closeTimer.Stop(); + closeTimer.Dispose(); + this.Close(); + } + + // --- + + protected override bool ShowWithoutActivation + { + get { return true; } + } + protected override CreateParams CreateParams { - CreateParams cp = base.CreateParams; - cp.ExStyle |= 0x08000000; // Prevents the window from activating - return cp; + get + { + const int WS_EX_NOACTIVATE = 0x08000000; + CreateParams cp = base.CreateParams; + cp.ExStyle |= WS_EX_NOACTIVATE; + return cp; + } } } }