Minimize to system tray - c#

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");

Related

Toast notification does not disappear after clicking outside of it in .net WPF application

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);
}
}

Toast notification does not pop up when zoom records screen in .net WPF App

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);
}
}

AxMSTSCLib display disappear in taskbar after minimize from full-screen mode

I am using AxMSTSCLib to develop a Windows App for creating RDP connections.
The steps listed below caused my remote desktop display disappear:
Start an RDP connection with full-screen mode
Click "restore down" button from connection bar
Click "maximize" button again to re-enter full-screen mode
Click "minimize" button
Then my app disappears, I can't see it in taskbar (but still be listed / running in taskmanager)
If I skip steps 2 & 3, it won't disappear when I click "minimize" button from connection bar, it's really weird.
I post some part of my code here, hope anyone can help me to find out the problem.
public partial class RdpShowForm : Form
{
public AxMSTSCLib.AxMsRdpClient9NotSafeForScripting oRemote;
public RdpShowForm()
{
InitializeComponent();
oRemote = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(this.oRemote)).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "WindowsVM";
this.Controls.Add(oRemote); // Controls contains 'panel1' and 'oRemote'
((System.ComponentModel.ISupportInitialize)(this.oRemote)).EndInit();
oRemote.CreateControl();
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
}
private void RdpShowForm_Load(object sender, EventArgs e)
{
NewRdpConnectionInstance();
}
private void NewRdpConnectionInstance()
{
oRemote.Server = 'xxx';
...
oRemote.FullScreen = true;
oRemote.AdvancedSettings7.DisplayConnectionBar = true;
oRemote.AdvancedSettings7.ConnectionBarShowMinimizeButton = true;
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = true;
oRemote.OnConnected += rdpClient_OnConnected;
oRemote.OnLeaveFullScreenMode += rdpClient_OnLeaveFullScreenMode;
oRemote.Connect();
oRemote.Show();
oRemote.Focus();
}
private void rdpClient_OnConnected(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.Visible = false;
}
private void rdpClient_OnLeaveFullScreenMode(object sender, EventArgs e)
{
this.Visible = true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == MAXIMIZE)
{
this.Visible = false;
oRemote.FullScreen = true;
oRemote.Show();
}
}
base.WndProc(ref m);
}
}
I've tried some methods, but none of them worked.
oRemote.Bounds = Screen.PrimaryScreen.WorkingArea;
oRemote.IsAccessible = true;
this.ShowInTaskbar = true;
or
this.Visible = ture; // do not hide the main form
or
if (m.WParam.ToInt32() == MAXIMIZE)
{
oRemote.FullScreen = true;
oRemote.Show();
oRemote.Update();
oRemote.Refresh();
}
So far, the only solution that came into my mind is to disable the restore button, like this:
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = false;
This way isn't fix the problem, just avoid the issue to be triggered.
I would appreciate any help.
Since AxMSTSCLib has a restore down issue, I turn to use a form to handle its fullscreen mode. In this way, I can get extensive control over full-screen mode behavior, write code to tell the container what to do when the restore down button is called.
// enable container-handled full-screen mode
oRemote.AdvancedSettings7.ContainerHandledFullScreen = 1;
Then, customize OnRequestGoFullScreen, OnRequestLeaveFullScreen, OnRequestContainerMinimize.
oRemote.OnRequestGoFullScreen += corey_OnRequestGoFullScreen;
oRemote.OnRequestLeaveFullScreen += corey_OnRequestLeaveFullScreen;
oRemote.OnRequestContainerMinimize += corey_OnRequestContainerMinimize;
private void corey_OnRequestGoFullScreen(object sender, EventArgs e)
{
// set the form to fullscreen
}
private void corey_OnRequestLeaveFullScreen(object sender, EventArgs e)
{
// set the form back to non-fullscreen mode
}
private void corey_OnRequestContainerMinimize(object sender, EventArgs e)
{
// minimize the form
}
The method I posted here can be a workaround to this issue.

windows form Notify Icon Not working properly

notify icon disappears after 2 or 3 seconds and i have to restart the application
I have tried this code but not working properly
private void Frm_Dashboard_Resize_1(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
You are not showing the form in the NotifyIcon double click event.
Change your code to -
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show(); //display the form
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
Also, look at the Remarks section of the ShowBalloonTip method

Open a window from System Tray icon

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();
}

Categories

Resources