Good afternoon,
I am learning WPF and one of my first questions was how do I run an app in the system tray. Multiple posts pointed me to the NotifyIcon class which is working fine.
My problem has to do with minimizing / maximizing the application.
My desired behavior is that at startup, the application is minimized to the system tray and hidden from the task bar.
When left clicking the system tray icon, the application should show.
Left clicking the system tray icon again will hide the window.
I have largely achieved what I set out to above, however, after I maximize / minimize the window once by clicking the system tray, when I minimize it again instead of hiding, it is showing the title bar in the lower left hand corner of my screen. This is similar I believe to the collapse behavior. I dont want to see it in the lower left hand of my screen, I just want it to hide, but I cannot figure it out.
The other strange thing is that my current application has no title bar, I used WindowStyle=none. So when it shows in the lower left corner, it is readding the title bar just to show down there.
Very odd. I've been searching but havent found anything, please help.
Adding code:
<Window x:Class="Tagalong.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Tagalong Beta .1" Height="350" Width="500" Icon="content/tagalong_icon.ico" ResizeMode="CanMinimize" Topmost="True" WindowStyle="None" Background="Transparent" AllowsTransparency="True" ShowInTaskbar="true" ><Grid><Grid.RowDefinitions><RowDefinition Height="Auto" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="250" /><ColumnDefinition Width="250" /></Grid.ColumnDefinitions><Border Name="LeftPane" Grid.Row="0" Grid.Column="0" BorderBrush="Black" BorderThickness="1.5" Height="350" Width="250" CornerRadius="10" Background="Blue" ><Grid></Grid></Border><Border Name="RightPane" BorderBrush="Black" Grid.Row="0" Grid.Column="1" BorderThickness="1.5" Height="350" Width="250" CornerRadius="10" Background="Green"><Grid></Grid></Border></Grid></Window>
public MainWindow() { InitializeComponent(); LocateWindowInLowerRight(); InitializeSystemTrayOptions(); } void InitializeSystemTrayOptions() { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new System.Drawing.Icon(@"C:\dev\desktop\Tagalong\Tagalong\tagalong_icon.ico"); notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick); notifyIcon.Visible = true; this.Visibility = Visibility.Hidden; this.WindowState = WindowState.Minimized; } //Move window to lower right... leave small gap between edge of screen and window. void LocateWindowInLowerRight() { var desktopWorkingArea = System.Windows.SystemParameters.WorkArea; this.Left = desktopWorkingArea.Right - this.Width - 5; this.Top = desktopWorkingArea.Bottom - this.Height - 2; } void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (this.WindowState == WindowState.Normal) { this.WindowState = WindowState.Minimized; this.Visibility = Visibility.Hidden; } else if (this.WindowState == WindowState.Minimized) { this.WindowState = WindowState.Normal; this.Visibility = Visibility.Visible; } } } private NotifyIcon notifyIcon;