Hardcodet.Wpf.TaskbarNotification Blank Duplicate system tray icon - c#

When loading my systray icon view xaml which uses Hardcodet.Wpf.TaskbarNotification proxy, a duplicate systray icon renders with a blank icon alongside the correct functional systray icon.
Are there any known bugs with Hardcodet.Wpf.TaskbarNotification that would cause this?

yes sure there is, what actually happend is when you open your app there is icon created but when you close it your app don't remove the icon when he close so...
you just need to open the TaskbarNotification code and make the dispose method public and call it in the Application.Current.Exit event
Application.Current.Exit += new ExitEventHandler(Current_Exit);
void Current_Exit(object sender, ExitEventArgs e)
{
notifyIcon.Dispose();
}

Related

C# Winforms change taskbar icon

I have an old legacy system that uses Winforms and is published with the built in One click,
I look after 3 different installs and each one has its own database it connects to, so its been set up that when a different system is click, it check isCompany1 and will set the Favicon accordingly
so when depoyment is done in the application window the Icon and Manifest is set to the correct ICO, when installed the .exe icon will be what is set there,
However i am trying to bring these all inline as when we debug a drop asks which database we would like to connect to, and depending on that it will set things up differently.
When running in VS the icons in the taskbar will change accoring to the dynamic Favicon however when its depolyed with this new selection it wont change, (it will change once on first load, then wont again)
The issue is some users need access to the different instances so would like different icons at the bottom, but its not changing the task menu, but everything else such as ALT-TAB and the control panel icon is changing,
all forms link into a baseform and call this :
if (App.IsCompany1)
{
this.Icon = new Icon("Resources\\Company1.ico");
}
else if (App.Company2)
{
this.Icon = new Icon("Resources\\Company2.ico");
}
else if (App.Company3)
{
this.Icon = new Icon("Resources\\Company3.ico");
}
AS i said this will change everything but not the Taskbar, but will from VS debugging,
I have made sure the .ico has all sizes, by writing their sizes on each one, and they display correctly the Taskbar and ALT-TAB both use 32x32
If it's any consolation, I couldn't reproduce your complaint.
I put 3 icons in resources, a single button on a form, this code:
private int iconum = 0;
private Icon[] icons = new[] { Properties.Resources.icon1, Properties.Resources.icon2, Properties.Resources.icon3 };
private void button1_Click(object sender, EventArgs e)
{
this.Icon = icons[iconum++ % icons.Length];
}
And it cycled through the icons in the main form title bar and the windows task bar over and over on every button click (made sure to run a release built exe too, not a debug start):
Note: the thing in the top right is my taskbar

Why does a folder dialog immediately close unless a window is displayed in WPF?

I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.

Quit Application when all forms is closed

I want to have a lot of forms in my Gtk# application. I want to quit application when user close all form. I try to use next code:
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
/*Application.Quit ();
a.RetVal = true;*/
if(System.Windows.Forms.Application.OpenForms.Count==0)
{
Gtk.Application.Quit ();
a.RetVal = true;
}
}
But System.Windows.Forms.Application.OpenForms.Count allways return "0" regardless of the number of open forms (OS Ubuntu 12.04). How can I solve this problem and get actual quantity of open forms?
Thanks in advance
also tried to find an answer to that issue.
My current implementation is based on some concept I have seen in MS Visual Studio documentation:
in the project's main class maintain a static list of open windows.
do not use the delete event but the destroy event of the GTK# window (with the OnDelete event handler something did not work, if I remember correctly the windows delete cannot be directly called by a member function).
In the OnDestoy event handler: when the window gets destroyed then remove it from the static list. Then check the list for being empty, then quit the application.
Not sure if this is really ideal for GTK# windows but in my application this concept works.
regards
Harald

How to use a dialog in an Excel shared addin

I'm writing a shared addin for Excel. It adds a CommandBarButton that when clicked opens a WPF window to collect some information from the user.
I wanted to keep the same WPF dialog in memory and reuse it so that if the user clicks the CommandBarButton again their previous values would still be there.
So I made a reference to my WPF dialog as a private member of my addin object that implements Extensibility.IDTExtensibility2.
I created the window during OnStartupComplete(), but for some reason when I run Excel the window immediately opens even though I never called ShowDialog() and when I do call ShowDialog() when the CommandBarButton is clicked to reopen the window it fails to load.
Does anyone know why this happens and what the correct way to handle this is?
Thanks very much for any help.
CODE UPDATE:
public void OnStartupComplete(ref System.Array custom)
{
MyDialog dlg = new MyDlg(); //This will open the dialog ?!?!
}
....
public MyDialog()
{
InitializeComponent();
Loaded += new RoutedEventHandler(OnLoaded);
}
OnLoaded just wires up some event handlers for buttons and sets some ItemSources. Even if I comment it out it still open the window.
I have picked up on the fact that once a WPF window is closed it can't be reoped and this is by design. But why it opens automatically when constructed inside an excel addin is a mystery.
I've been able to reproduce your problem. In the WPF designer, make sure that the form's Visbility property is set to Collapsed. If you have it as Visible, it will automatically show when the dialog is created.

How to prevent leaving an Icon in System Tray on exit?

My program puts an icon in the system tray because the user may minimize to it. However, if the application crashes, or I stop the app from running in VS it leaves the icon in it until I hover over it with the mouse. Sometimes I'll look down there and there will be 10 or so icons.
I can I make sure the icon goes away?
There is no way to do this. Windows does not poll your program to see if it's still alive. Therefore, your icon will stay in the system tray until you either tell it to leave (normal exit), or the user does something that initiates a call to your program (such as mouseover). Only then does the shell know that your program has died and it needs to clean up your icon.
You have several options:
1) Write a root structured exception handler that ensures the destruction of the icon before exit. This one takes some effort - but you can basically add your own unhandled exception handler to Windows which will be called in the case of many forms of "boom" which will then allow you some last-ditch clean up opportunity.
2) Write a monitor application that sleeps on your main processes handle, and kills the icon when it wakes up (the main application has died). To make this latter scenario work well, you'll likely want the main process to NOT kill the icon itself - or you could create a way to tell the monitor app that it should exit w/o destroying the icon (have the monitor app sleep on both the process handle and a named event - and if woken by the event then it simply dies w/o cleaning up after the main app).
Before you exit, set the NotifyIcon Visible property to false.
You need to call Dispose on the NotifyIcon for it to leave the tray.
EDIT: There's an event you can hook into in your Program.cs. For all Windows apps, you'll have something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
The Application class has a few events you can take advantage of to know when your app dies:
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
I don't know what happens when one kills the process, but if it crashes due to an exception, of course one can handle it. The way to do it best, depends on the type of application: Console, Forms, a service, ...
But in all cases, you should be able to use a try / finally structure in your Program.cs, and in the 'finally' section Dispose() the TrayIcon.
For example, in a Forms application, make your NotifyIcon (called TrayIcon in my example below) in your Form class public, and change the "Application.Run(new Form1())" line in Program.cs as follows:
Form form = new Form1();
try { Application.Run(form); }
finally { form.TrayIcon.Dispose(); }
We can hide trayIcon before form closing.
Add FormClosing Event
NotifyIcon ni;
...
...
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
ni.Visible = false;
}

Categories

Resources