Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, December 11, 2017 2:21 PM
I have an application that shows a balloontip every time a new email arrives for my work account.
I would like those notifications to clear automatically in my code.
I am not sure how to go about doing that and google/bing does not seem to have an answer on that. pinvoke or whatever is fine.
Clarification: In Windows 10, notifications are held in the notification center and you have to either close the program or manually remove them in the notification center. I of course could just restart the application but that is ugly.
Michael J. Hall
All replies (12)
Monday, December 11, 2017 2:27 PM
I have another application that I forcefully kill at login due to just poor programming and their is no alternative to it so I have to deal with it. I created a workaround but the icon for this application still remains in the notification tray.
I have tried other solutions but they don't always work. They move the mouse over the tray and then they are removed but that sometimes doesn't work.
I want a way that works perfectly every time without having to restart explorer.exe.
Michael J. Hall
Monday, December 11, 2017 3:00 PM
Notifications are automatically removed on my Windows 10 OS if I do a basic test :
var ni = new NotifyIcon()
{
Visible = true,
Icon = System.Drawing.SystemIcons.Exclamation,
BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info,
BalloonTipTitle = "Title",
BalloonTipText = "Balloon Tip Text",
};
ni.ShowBalloonTip(10000);
Monday, December 11, 2017 3:16 PM
Yes, if you close the application or even restart it, the notifications for that application are automatically cleared from the notification center.
That is not what I'm wanting to do.
Michael J. Hall
Monday, December 11, 2017 3:21 PM
No, they disappear without closing/restarting the application...
(I tried to add several ones in a loop too)
Monday, December 11, 2017 4:30 PM
I don't believe that to be true.
Take Discord for example. Someone types something in chat and I get a notification. As soon as the notification is gone then the notification is removed from the notify center.
The only thing that would make what you said true is if the notification is coming from another exe file and then closes after 10 seconds or whatever delay is set.
Michael J. Hall
Monday, December 11, 2017 5:21 PM | 1 vote
Do you really mean "balloontip"? Because since Windows 10 all apps and system notifications are supposed to be shown as "toasts".
If the latter is the case, you could check out the Clear() method overloads of the Windows.UI.Notifications.ToastNotificationHistory class.
wizend
Monday, December 11, 2017 7:40 PM
Your answer is the best answer but I am also going to provide a quick solution that I found fiddling around.
notifyIcon1.BalloonTipShown += NotifyIcon1_BalloonTipShown1;
private void NotifyIcon1_BalloonTipShown1(object sender, EventArgs e)
{
Thread th = new Thread(ClearNotifications)
{
IsBackground = true
};
th.Start();
}
private void ClearNotifications()
{
Thread.Sleep(7000);
notifyIcon1.Visible = false;
notifyIcon1.Visible = true;
}
Michael J. Hall
Tuesday, December 12, 2017 8:40 AM
Hello mikeyhalla,
what is the project type, if you are using winform, you could try the below code.
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Call Dispose to remove the icon out of notification area of Taskbar.
notifyIcon1.Dispose();
}
Refer link.
https://stackoverflow.com/questions/46763247/c-sharp-clear-tray-icons-of-closed-applications
Best regards,
Neil Hu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, December 12, 2017 6:12 PM
In your example it should actually be: (thank you nonetheless)
notifyIcon1.Visible = false;
But, the program I'm closing isn't my own so I cannot do that.
What I want is to clear icons from the notification tray that no longer have a running program associated to it.
Or a way to close an application that is strictly running in the notification area and have it remove the icon on its own.
I tried pinvoke quit but that is the same results as process.kill();
Thank you :D
Michael J. Hall
Thursday, December 14, 2017 9:24 AM | 1 vote
Hello mikeyhalla,
Try the below code that Dave write. It works well on window 10.
class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,string lpszWindow);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public static void RefreshTrayArea()
{
IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
if (notificationAreaHandle == IntPtr.Zero)
{
notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32",
"User Promoted Notification Area");
IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero,
"ToolbarWindow32", "Overflow Notification Area");
RefreshTrayArea(overflowNotificationAreaHandle);
}
RefreshTrayArea(notificationAreaHandle);
}
private static void RefreshTrayArea(IntPtr windowHandle)
{
const uint wmMousemove = 0x0200;
RECT rect;
GetClientRect(windowHandle, out rect);
for (var x = 0; x < rect.right; x += 5)
for (var y = 0; y < rect.bottom; y += 5)
SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x);
}
static void Main(string[] args)
{
RefreshTrayArea();
}
}
Result
Best regards,
Neil Hu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Thursday, December 14, 2017 2:17 PM
Thank you, but that is the exact code I'm already using and it doesn't always work. I am not certain as to why it sometimes won't work.
Ultimately I was hoping their was a "real" way of doing this instead of a "hack".
My scenario:
I am using Mouse Without Borders and it only works for one user account at a time. If I try to switch accounts on my second computer then I have to plug in my keyboard to the pc and start the program again.
Instead I wrote a program to kill the instance of Mouse Without Borders on the account I logged out off and start another instance on the account I'm logging into. This leaves a Mouse Without Borders icon in the notification area.
In my code it even waits until the notification area has been created and then refreshes the tray.
My suspicion is that it cant remove them for the other user account like it doesn't have the rights or it just doesn't see it which to me is odd considering that all the program is telling Windows to do it just move the mouse around in that area and nothing more.
Michael J. Hall
Wednesday, December 20, 2017 10:55 AM
Hello mikeyhalla,
How do you kill the instance of mouse? In windows 10 I can't see the mouse or keyboard will leave icons in the notification area. What type of the program you are using? you could implement the program_exit feature rather than force to kill it. The left icons are more related to Windows operate system and they don't expose the relating windows api. The above code achieve it by moving mouse programming. I can't assume the issues when you switch account. Please provide relating screenshots and relating code. It will give a better understanding if you provide video.
Best regards,
Neil Hu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].