WPF Tray Display

Original: WPF Tray Display

This article tells you how to display the tray in WPF, while the tray can right-click to open the menu, double-click to perform the specified code

NotifyIcon WPF

You can quickly do this by installing Nuget Hardcodet.NotifyIcon.Wpf display trays in WPF

Here Insert Picture Description

Because the tray is a tray program, not a window, it is recommended that the code is written on the inside App.xaml.cs

Create a tray interface, the interface is created in App.xaml

Tray icon is required, from Iconfont- Alibaba vector icon library to find good-looking icons. In the tray icon needs to be 16x16 32-bit ico file

Download pictures on solutions, you can modify the Resource

In App.xaml define resource TaskbarIcon see the code

<Application x:Class="HouneaLeabeltezairKayballjachall.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:HouneaLeabeltezairKayballjachall"
             xmlns:tb="http://www.hardcodet.net/taskbar"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <tb:TaskbarIcon x:Key="Taskbar"
                        IconSource="Taskbar.ico">

        </tb:TaskbarIcon>
    </Application.Resources>
</Application>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

The above code needs to be replaced Class="HouneaLeabeltezairKayballjachall.App"for their own projects

App.xaml.cs open access to resources, the resource is created only when acquired, create a TaskbarIcon will appear in the tray

        protected override void OnStartup(StartupEventArgs e)
        {
            _taskbar = (TaskbarIcon) FindResource("Taskbar");
            base.OnStartup(e);
        }

        private TaskbarIcon _taskbar;

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Run the code can see the pictures displayed icon, the image below is my icon

Move the mouse to display text in the tray above, may be added to the code TaskbarIcon

        <tb:TaskbarIcon x:Key="Taskbar"
                        ToolTipText="鼠标移动上显示的文字"
                        IconSource="Taskbar.ico">

        </tb:TaskbarIcon>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5

Double-click the tray to run the code you need to add commands to create a ViewModel bind command

    public class DelegateCommand : ICommand
    {
        public Action CommandAction { get; set; }
        public Func<bool> CanExecuteFunc { get; set; }

        public void Execute(object parameter)
        {
            CommandAction();
        }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null || CanExecuteFunc();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Add some functionality, display windows, hidden window

    public class NotifyIconViewModel
    {
        /// <summary>
        /// 如果窗口没显示,就显示窗口
        /// </summary>
        public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () => Application.Current.MainWindow == null,
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow = new MainWindow();
                        Application.Current.MainWindow.Show();
                    }
                };
            }
        }

        /// <summary>
        /// 隐藏窗口
        /// </summary>
        public ICommand HideWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CommandAction = () => Application.Current.MainWindow.Close(),
                    CanExecuteFunc = () => Application.Current.MainWindow != null
                };
            }
        }


        /// <summary>
        /// 关闭软件
        /// </summary>
        public ICommand ExitApplicationCommand
        {
            get
            {
                return new DelegateCommand { CommandAction = () => Application.Current.Shutdown() };
            }
        }
    }


  
  
  • 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

In the interface binding code

        <tb:TaskbarIcon x:Key="Taskbar"
                        ToolTipText="鼠标移动上显示的文字"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        IconSource="Taskbar.ico">

        </tb:TaskbarIcon>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Yet this time TaskbarIcon ViewModel be by way of the following

        <tb:TaskbarIcon x:Key="Taskbar"
                        ToolTipText="鼠标移动上显示的文字"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        IconSource="Taskbar.ico">
            <tb:TaskbarIcon.DataContext>
                <local:NotifyIconViewModel />
            </tb:TaskbarIcon.DataContext>
        </tb:TaskbarIcon>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Now double-click on it to open the MainWindow MainWindow because the default is open, more difficult to see a double-click. MainWindow removed by default in App.xaml need to find the following code

StartupUri="MainWindow.xaml"

  
  
  • 1

Now try not to open the default MainWindow run the software, you can see the tray icon display, double-click the icon can open interface

If you want to display the right-click menu, you need to add definition App.xaml

        <ContextMenu x:Shared="false" x:Key="SysTrayMenu">
            <MenuItem Header="显示窗口" Command="{Binding ShowWindowCommand}" />
            <MenuItem Header="关闭窗口" Command="{Binding HideWindowCommand}" />
            <Separator />
            <MenuItem Header="退出" Command="{Binding ExitApplicationCommand}" />
        </ContextMenu>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Use the menu in TaskbarIcon

        <tb:TaskbarIcon x:Key="Taskbar"
                        ContextMenu="{StaticResource SysTrayMenu}"
                        ToolTipText="鼠标移动上显示的文字"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        IconSource="Taskbar.ico">
            <tb:TaskbarIcon.DataContext>
                <local:NotifyIconViewModel />
            </tb:TaskbarIcon.DataContext>
        </tb:TaskbarIcon>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Because ViewModel already written code, so you can now run

Here Insert Picture Description

The default software settings is the last window closes when the application is closed, it can be closed by setting App.ShutdownMode calling exit, add the code to open the App.xaml

 ShutdownMode="OnExplicitShutdown"

  
  
  • 1

Code for this article

WPF display tray right-click to open the menu, double-click to open the software download -CSDN

There are some advanced use the following

Move mouse tray defined text color

            <tb:TaskbarIcon.TrayToolTip>
                 里面可以添加控件
            </tb:TaskbarIcon.TrayToolTip>

  
  
  • 1
  • 2
  • 3
        <tb:TaskbarIcon x:Key="Taskbar"
                        ContextMenu="{StaticResource SysTrayMenu}"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        IconSource="Taskbar.ico">
            <tb:TaskbarIcon.TrayToolTip>
                <Border
                    Background="White"
                    BorderBrush="Orange"
                    BorderThickness="2"
                    CornerRadius="4"
                    Opacity="0.8"
                    Width="160"
                    Height="40">
                    <TextBlock
                        Text="hello world"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center" />
                </Border>
            </tb:TaskbarIcon.TrayToolTip>
            <tb:TaskbarIcon.DataContext>
                <local:NotifyIconViewModel />
            </tb:TaskbarIcon.DataContext>
        </tb:TaskbarIcon>


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Because it can add controls to play your ideas, such as adding a button

        <tb:TaskbarIcon x:Key="Taskbar"
                        ContextMenu="{StaticResource SysTrayMenu}"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        IconSource="Taskbar.ico">
            <tb:TaskbarIcon.TrayToolTip>
                <Border
                    Background="White"
                    BorderBrush="Orange"
                    BorderThickness="2"
                    CornerRadius="4"
                    Opacity="0.8"
                    Width="160">
                   <Grid>
                       <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="可以放文字"></TextBlock>
                       <Button Margin="10,100,10,10" Content="可以放按钮"></Button>
                   </Grid>
                </Border>
            </tb:TaskbarIcon.TrayToolTip>
            <tb:TaskbarIcon.DataContext>
                <local:NotifyIconViewModel />
            </tb:TaskbarIcon.DataContext>
        </tb:TaskbarIcon>


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Here Insert Picture Description

Displays the Bubble

Bubbles can be displayed by the following code

Taskbar.ShowBalloonTip("标题", "内容", BalloonIcon.Info);

  
  
  • 1

If you need a custom bubble, define a class that inherits UIElement, then you can pass TaskbarIcon.ShowCustomBalloon

As already defined FancyBalloon bubbles, by the following code

 FancyBalloon balloon = new FancyBalloon();
 TaskbarIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000 /*多少时间就隐藏*/);

  
  
  • 1
  • 2

Code: the NotifyIcon WPF - Bitbucket

I set up my own blog https://blog.lindexi.com/ welcome to visit, there are many new blog. Only after I saw the blog writing will mature on csdn or blog garden, but once released will no longer update

If you see any blog do not understand, please share, I built a dotnet Vocational and Technical College welcome to join

Creative Commons License
This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Linde Xi (containing links: http://blog.csdn.net/lindexi_gd ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please feel free to contact .

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12082172.html