Wpf Application That Only Has a Tray Icon

WPF Application that only has a tray icon

There's no NotifyIcon for WPF.

A colleague of mine used this freely available library to good effect:

  • http://www.hardcodet.net/wpf-notifyicon (blog post)
  • https://github.com/hardcodet/wpf-notifyicon (source code)
  • https://www.nuget.org/packages/Hardcodet.NotifyIcon.Wpf/ (NuGet package)
  • http://visualstudiogallery.msdn.microsoft.com/aacbc77c-4ef6-456f-80b7-1f157c2909f7/

Sample Image

How to create WPF System Tray Icon when no Main host window exists

Set the application ShutdownMode to OnExplicitShutdown and display the tray icon from the Application.OnStartup. This example uses the NotifyIcon from WinForms, so add a reference to System.Windows.Forms.dll and System.Drawing.dll. Also, add an embedded resource for the Tray Icon.

App.xaml

<Application x:Class="WpfTrayIcon.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown"
>
<Application.Resources>

</Application.Resources>
</Application>

App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;

using NotifyIcon = System.Windows.Forms.NotifyIcon;

namespace WpfTrayIcon
{
public partial class App : Application
{
public static NotifyIcon icon;

protected override void OnStartup(StartupEventArgs e)
{
App.icon = new NotifyIcon();
icon.Click += new EventHandler(icon_Click);
icon.Icon = new System.Drawing.Icon(typeof(App), "TrayIcon.ico");
icon.Visible = true;

base.OnStartup(e);
}

private void icon_Click(Object sender, EventArgs e)
{
MessageBox.Show("Thanks for clicking me");
}
}
}

WPF C# Tray Icon Implementation Issue

It was very simple. I was trying to import WinForms for WPF.
Here is everything explained about Notifyicon in WPF.
Thanks to Andy

Minimizing Application to system tray using WPF ( Not using NotifyIcon )

If you'll reconsider your reluctance to using an external component, I recommend WPF NotifyIcon. I've used it. It's straightforward and works well.

It does not just rely on the corresponding WinForms component, but is a purely independent control which leverages several features of the WPF framework in order to display rich tooltips, popups, context menus, and balloon messages.

wpf c# tray icon app terminates after 30 minutes

This line is causing a memory leak:

hIcon = (bitmapText.GetHicon());

hIcon needs to be destroyed:

    hIcon = (bitmapText.GetHicon());
ni.Icon = System.Drawing.Icon.FromHandle(hIcon);
DestroyIcon(hIcon);

Add this code to the class to define DestroyIcon:

    [DllImport("user32.dll", SetLastError = true)]
static extern bool DestroyIcon(IntPtr hIcon);

see:

https://msdn.microsoft.com/en-us/library/system.drawing.icon.fromhandle(v=vs.110).aspx



Related Topics



Leave a reply



Submit