So I created a window with a system tray icon. The window starts out minimized and will re-appear when the system tray Icon is clicked. However, it ONLY works when you click on the minimize button. If you click the red exit button the window disappears, the System Tray Icon remains(as it should) but when you click on it the program throws an error.
Cannot set Visibility or call Show, ShowDialog, or
WindowInteropHelper.EnsureHandle after a Window has closed.
Here is the relevant code
public partial class MainWindow : Window
{
public static NotifyIcon icon;
List<string> food = new List<string>();
bool on = false;
public MainWindow()
{
InitializeComponent();
food.Add("Breakfast");
food.Add("Soups");
food.Add("Vegetables");
food.Add("Crab roll");
food.Add("Sushi");
food.Add("Egg rolls");
food.Add("Salad");
MainWindow.icon = new NotifyIcon();
window1.WindowState = WindowState.Minimized;
icon.Icon = new System.Drawing.Icon("favicon.ico");
icon.Visible = true;
icon.Click += new EventHandler(icon_Click);
icon.BalloonTipClicked += new EventHandler(icon_BalloonTipClicked);
icon.DoubleClick += new EventHandler(icon_DoubleClick);
icon.BalloonTipClosed += new EventHandler(icon_BalloonTipClosed);
icon.MouseMove += new System.Windows.Forms.MouseEventHandler(icon_MouseMove);
StateChanged += new EventHandler(MainWindow_StateChanged);
}
void icon_BalloonTipClicked(object sender, EventArgs e)
{
this.Show(); //This is where the error is
window1.WindowState = WindowState.Normal;
}
void icon_DoubleClick(object sender, EventArgs e)
{
this.Show(); //This is where the error is
window1.WindowState = WindowState.Normal;
}
void icon_BalloonTipClosed(object sender, EventArgs e)
{
on = false;
}
void icon_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!on)
{
icon.BalloonTipText = "";
foreach(string item in food){
if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
{
icon.BalloonTipText += item+"\n";
}
}
icon.ShowBalloonTip(10);
on = true;
}
}
void MainWindow_StateChanged(object sender, EventArgs e)
{
if (window1.WindowState == WindowState.Minimized)
{
this.Hide();
}
}
private void icon_Click(Object sender, EventArgs e)
{
icon.BalloonTipText = "";
foreach (string item in food)
{
if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
{
icon.BalloonTipText += item + "\n";
}
}
icon.ShowBalloonTip(10);
on = true;
}
}
Intercept the Closing event of the window, and cancel it (this will prevent the window from closing) - then hide the window instead:
public MainWindow()
{
// Subscribe to closing event (when X is pressed)
this.Closing += MainWindow_Closing;
InitializeComponent();
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Prevent window from closing
e.Cancel = true;
// Hide window
this.Hide();
}
Related
I am working on a .NET WPF project using Visual Studio 2022 and I added tray icon functionality to my app. I also show toast notification whenever my app is minimized to windows tray. Everything works fine except when I click outside border of toast notification, it does not disappear. I think it is a focus issue but can not able to overcome that. Any idea how to solve this?
My code:
public NotifyIcon m_notifyIcon;
public MainWindow()
{
InitializeComponent();
m_notifyIcon = new NotifyIcon();
m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show.";
m_notifyIcon.BalloonTipTitle = "The App";
m_notifyIcon.Text = "The App";
m_notifyIcon.DoubleClick += new EventHandler(m_notifyIcon_Click);
m_notifyIcon.MouseDown += new MouseEventHandler(Notifier_MouseDown);
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
CheckTrayIcon();
}
}
private WindowState m_storedWindowState = WindowState.Normal;
void OnStateChanged(object sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
}
else
m_storedWindowState = WindowState;
}
void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
CheckTrayIcon();
}
void m_notifyIcon_Click(object sender, EventArgs e)
{
Show();
WindowState = m_storedWindowState;
}
void CheckTrayIcon()
{
ShowTrayIcon(true);
}
void ShowTrayIcon(bool show)
{
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
}
void Notifier_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;
IntPtr handle = ((HwndSource)PresentationSource.FromVisual(menu)).Handle;
ApiHelper.SetForegroundWindow(handle);
}
}
I am working on a .NET WPF project using Visual Studio 2022 and I added tray icon functionality to my app. I also show toast notification whenever my app is minimized to windows tray. Everything works fine except when i am on a zoom meeting and screen recording, toast notification does not show up. What could be the reason for this?
My code:
public NotifyIcon m_notifyIcon;
public MainWindow()
{
InitializeComponent();
m_notifyIcon = new NotifyIcon();
m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show.";
m_notifyIcon.BalloonTipTitle = "The App";
m_notifyIcon.Text = "The App";
m_notifyIcon.DoubleClick += new EventHandler(m_notifyIcon_Click);
m_notifyIcon.MouseDown += new MouseEventHandler(Notifier_MouseDown);
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
CheckTrayIcon();
}
}
private WindowState m_storedWindowState = WindowState.Normal;
void OnStateChanged(object sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
}
else
m_storedWindowState = WindowState;
}
void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
CheckTrayIcon();
}
void m_notifyIcon_Click(object sender, EventArgs e)
{
Show();
WindowState = m_storedWindowState;
}
void CheckTrayIcon()
{
ShowTrayIcon(true);
}
void ShowTrayIcon(bool show)
{
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
}
void Notifier_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;
IntPtr handle = ((HwndSource)PresentationSource.FromVisual(menu)).Handle;
ApiHelper.SetForegroundWindow(handle);
}
}
I have a project and I'm locked. I open different forms on Form1 you see below in the code. I did this with "notifyIcon". Instead of opening 5 different forms, I am bringing one by changing the form. I have 5 forms on my screen. But my problem is; I can save the locations of the forms. Technically they all come from the same form, so they all start from the same position. Do you have a recommendation?
Edit :
A new idea came to mind. but I need your help again. when I close the form, I can save the form's position to a TxT file and manually start the positions with the positions from TxT when the forms are opened.
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
string dosyaYolu = #"D:\color\colors.txt";
string[] satirlar = File.ReadAllLines(dosyaYolu);
}
private void tCKToolStripMenuItem_Click(object sender, EventArgs e)
{
string dosyaYolu = #"D:\color\colors.txt";
string[] satirlar = File.ReadAllLines(dosyaYolu);
string tckRenk = satirlar[0];
Color tckColor = System.Drawing.ColorTranslator.FromHtml(tckRenk);
Form2 tckForm = new Form2();
tckForm.Opacity = .50;
tckForm.TopMost = true;
tckForm.BackColor = tckColor;
tckForm.LabelText = "TCK";
tckForm.Show();
}
private void aDAToolStripMenuItem_Click(object sender, EventArgs e)
{
string dosyaYolu = #"D:\color\colors.txt";
string[] satirlar = File.ReadAllLines(dosyaYolu);
string adaRenk = satirlar[1];
Color adaColor = System.Drawing.ColorTranslator.FromHtml(adaRenk);
Form2 adaForm = new Form2();
adaForm.Opacity = .50;
adaForm.TopMost = true;
adaForm.BackColor = adaColor;
adaForm.LabelText = "ADA";
adaForm.Show();
}
You can set From Location
Form frm2 = new Form();
frm2.StartPosition = FormStartPosition.Manual;
frm2.Left = 500;
frm2.Top = 500;
frm2.Show();
public static class extensions
{
public static void SaveFormSizeAndLocation(this Form form)
{
try
{
using (RegistryKey key = Application.UserAppDataRegistry.CreateSubKey(form.Name))
{
if (key != null)
{
if (form.WindowState == FormWindowState.Normal)
{
key.SetValue("Left", form.Left);
key.SetValue("Top", form.Top);
key.SetValue("Width", form.Width);
key.SetValue("Height", form.Height);
}
if (form.ShowInTaskbar)
{
string windowState = Enum.GetName(typeof(FormWindowState), form.WindowState);
key.SetValue("WindowState", form.WindowState);
}
}
}
}
catch
{
// Party on, Garth!
}
}
public static void LoadFormSizeAndLocation(this Form form)
{
try
{
using (RegistryKey key = Application.UserAppDataRegistry.OpenSubKey(form.Name))
{
if (key != null)
{
form.Left = (int)key.GetValue("Left", form.Left);
form.Top = (int)key.GetValue("Top", form.Top);
form.Width = (int)key.GetValue("Width", form.Width);
form.Height = (int)key.GetValue("Height", form.Height);
// Move window into visible screen bounds if outside screen bounds (prevent off-screen hidden windows)
Rectangle screenRect = SystemInformation.VirtualScreen;
if (form.Left < screenRect.Left)
form.Left = screenRect.Left;
if (form.Top < screenRect.Top)
form.Top = screenRect.Top;
if (form.Right > screenRect.Right)
form.Left = screenRect.Right - form.Width;
if (form.Bottom > screenRect.Bottom)
form.Top = screenRect.Bottom - form.Height;
if (form.ShowInTaskbar)
{
string windowState = Enum.GetName(typeof(FormWindowState), form.WindowState);
windowState = key.GetValue("WindowState", windowState).ToString();
form.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState), windowState);
}
}
}
}
catch
{
// Party on, Wayne!
}
}
}
Use this extension methods and call it on form shown, form Closing & form Move events
For Complete Working Sample
public partial class Form3 : Form
{
private int count = 1;
public Form3()
{
InitializeComponent();
}
private void Form2_Move(object sender, EventArgs e)
{
this.SaveFormSizeAndLocation();
}
protected override void OnShown(EventArgs e)
{
Text = Name;
base.OnShown(e);
this.LoadFormSizeAndLocation();
this.Move += Form2_Move;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
this.SaveFormSizeAndLocation();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form3();
frm.Name = "inner" + count.ToString();
frm.Show();
count++;
}
}
i was following the instruction on page
but then, there's no icon attached for the application, so after the form is hidden, i cannot reshow the form, since there's no icon on the system tray,
how do i resolve this ?
here is my code
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
cmd.cetakSukses(ident.judul_App + " Diperkecil ke dalam System Tray");
notifyIcon1.BalloonTipText = ident.judul_App + " Diperkecil ke dalam System Tray";
notifyIcon1.BalloonTipTitle = ident.judul_App;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else
{
}
}
update :
i have attached the icon, and the icon still not showing on the system tray
and i figured how to make the form showing, i need to add the following code to notifyicon
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
You can set the notify icon at design using the Properties sheet:
Or you can add/change the icon property at runtime using the following code:
notifyIcon1.Icon = new Icon("appicon.ico");
This is how i implemented through code behind for a WPF app.
System.Windows.Forms.NotifyIcon m_NotifyIcon;
public StartWindow()
{
InitializeComponent();
m_NotifyIcon = new System.Windows.Forms.NotifyIcon();
m_NotifyIcon.Icon = new System.Drawing.Icon(IconPath);
m_NotifyIcon.Visible = true;
m_NotifyIcon.BalloonTipTitle = "Tip here";
m_NotifyIcon.Text = "Text here";
m_NotifyIcon.DoubleClick += delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
try
{
if (m_NotifyIcon != null)
m_NotifyIcon.Dispose();
}
catch { }
base.OnClosing(e);
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
You need to give you application an icon either by using visual studio or programatically.
You can do it in VS by going to the project properties and selecting the application tab
Or you can set it at runtime if you have icon files attached to your project already.
private NotifyIcon appIcon = new NotifyIcon();
appIcon.Icon = new System.Drawing.Icon("myApp.ico");
Hi In my c# application I am trying to minimize application to systems tray, when the form is closed. Here is the code I have tried.
public void MinimizeToTray()
{
try
{
notifyIcon1.BalloonTipTitle = "Sample text";
notifyIcon1.BalloonTipText = "Form is minimized";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and I am calling the method to form closing event. But the problem is its not minimizing to tray. Its just closing the form.
e.Cancel = true; code will be always cancelling the event even if you shut the computer down, but here is a code that helps you:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
myNotifyIcon.Visible = true;
this.Hide();
e.Cancel = true;
}
}
It will allow closing the form programmaticaly.
Write a event in Form Closing event.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
And write using Custom menu strip for notification icon for to show.
namespace MinimizeTrayNotification
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void MinimzedTray()
{
notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Application;
notifyIcon1.BalloonTipText = "Minimized";
notifyIcon1.BalloonTipTitle = "Your Application is Running in BackGround";
notifyIcon1.ShowBalloonTip(500);
}
private void MaxmizedFromTray()
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = "Maximized";
notifyIcon1.BalloonTipTitle = "Application is Running in Foreground";
notifyIcon1.ShowBalloonTip(500);
}
private void Form1_Resize(object sender, EventArgs e)
{
if(FormWindowState.Minimized==this.WindowState)
{
MinimzedTray();
}
else if (FormWindowState.Normal == this.WindowState)
{
MaxmizedFromTray();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
Form1 frm = new Form1();
frm.Show();
MaxmizedFromTray();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
notifyIcon1.BalloonTipText = "Normal";
notifyIcon1.ShowBalloonTip(500);
}
}
}
You should cancel the FormClosing event and then call your MinimizeToTray() function.
This is done through the Cancel property of the FormClosingEventArgs.
Also, consider using a bool somewhere to allow closing the Form in some conditions, such as if you're using a File > Exit menu or something:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!allowClosing)
{
e.Cancel = true;
MinimizeToTray();
}
}
To minimize when closing set WindowState to Minimized
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
You need to use the FormClosing-Event.
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
e.Cancel = true;
MinimizeToTray();
}
You can handle FormClosing Event such as micsoft Form Closing Event as Following example of C#
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}