Ghost1372

everything can be handy

SystemTrayIcon

Property

Name
IsVisible
Tooltip

Methods

Name
CloseFlyout
SetIcon

Events

Name
LeftClick
RightClick
LeftDoubleClick
RightDoubleClick

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public SystemTrayIcon trayIcon { get; set; }

public void AddTrayIcon()
{
uint iconId = 123;
if (trayIcon is null)
{
var icon = WindowHelper.GetWindowIcon(this);
trayIcon = new SystemTrayIcon(iconId, icon, "ToolTip");
trayIcon.LeftClick += OnTrayIconLeftClick;
trayIcon.RightClick += OnTrayIconRightClick;
}
trayIcon.IsVisible = true;
}

public void RemoveTrayIcon()
{
if (trayIcon is not null)
{
trayIcon.LeftClick -= OnTrayIconLeftClick;
trayIcon.RightClick -= OnTrayIconRightClick;
trayIcon.IsVisible = false;
trayIcon.Dispose();
trayIcon = null;
}
}

private void OnTrayIconRightClick(SystemTrayIcon sender, SystemTrayIconEventArgs args)
{
var flyout = new MenuFlyout();
flyout.Items.Add(new MenuFlyoutItem() { Text = "DevWinUI", IsEnabled = false });
flyout.Items.Add(new MenuFlyoutItem() { Text = "Open DevWinUI Gallery" });
flyout.Items.Add(new MenuFlyoutSeparator());
flyout.Items.Add(new MenuFlyoutItem() { Text = "Exit" });

((MenuFlyoutItem)flyout.Items.Last()).Click += (s, e) => this.Close();

var submenu = new MenuFlyoutSubItem() { Text = "About" };
submenu.Items.Add(new MenuFlyoutItem() { Text = "GitHub" });
submenu.Items.Add(new MenuFlyoutItem() { Text = "Docs" });
flyout.Items.Add(submenu);

args.Flyout = flyout;
}

private void OnTrayIconLeftClick(SystemTrayIcon sender, SystemTrayIconEventArgs args)
{
AppWindow.Presenter.As<OverlappedPresenter>().Restore();
WindowHelper.SetForegroundWindow(this);
}

DevWinUI

Demo

you can run demo and see this feature.

0%