I want to quickly add a tray menu to the WPF program

I want to quickly add a tray menu to the WPF program

The effects of commonly used trays should be satisfactory, right? To achieve these effects, there is really not much code we need to code ourselves.

最后更新 4/25/2020 1:45 PM
沙漠尽头的狼
预计阅读 3 分钟
分类
WPF
标签
.NET WPF tray menu

1 Simple requirement

  • Use open source control libraries
  • Declaring a tray menu in XAML is like adding a ContextMenu to a control
  • Encapsulates common commands, such as: open the main form, exit the application, etc.

I added a tray menu to TerminalMACS, and the final tray menu effect is:

2 How to do it?

[Step 1] Add the HandyControl library to the WPF project you have created and install:

You asked me why I use the HC control library? Let's first look at the Demo effect:

The effects of commonly used trays should be satisfactory, right? To achieve these effects, there is really not much code we need to code ourselves, because HC has been encapsulated and we can use it directly.

[Step 2] Add HandyControl namespace to the form

xmlns:hc="https://handyorg.github.io/handycontrol"

[Step 3] Declare the key 19 lines of code for the tray menu

<hc:NotifyIcon x:Name="NotifyIconContextContent"
            Text="{markup:I18n {x:Static i18NResources:Language.AppTitle}}"
            Visibility="Visible"
            Icon="/Images/logo.ico">
    <hc:NotifyIcon.ContextMenu>
        <ContextMenu>
            <MenuItem Command="hc:ControlCommands.PushMainWindow2Top" Header="{markup:I18n {x:Static i18NResources:Language.PushMainWindow2Top}}"/>
            <MenuItem Command="hc:ControlCommands.ShutdownApp" Header="{markup:I18n {x:Static i18NResources:Language.Exit}}"/>
        </ContextMenu>
    </hc:NotifyIcon.ContextMenu>
    <hc:Interaction.Triggers>
        <hc:EventTrigger EventName="Click">
            <hc:EventToCommand Command="hc:ControlCommands.PushMainWindow2Top"/>
        </hc:EventTrigger>
    </hc:Interaction.Triggers>
</hc:NotifyIcon>

With the code above, a tray menu is basically done. Let me briefly say:

  • NotifyIcon is a tray menu control, Text displays the tray menu name, place the mouse on the tray icon, ToolTip displays the string, and generally displays the application name, as shown in the following figure:

  • hc:NotifyIcon.ContextMenu is used to declare menu items. This project currently has two menus: open the main form and exit the application. The corresponding commands HC for these two menus have been encapsulated for easy use. - In addition, click the tray menu to pop up the main application window

[Step 4] When manually closing the main form, hide it without exiting

After adding the tray menu, when closing the main form, you should override the OnClosing event to hide the main form rather than closing it directly.

protected override void OnClosing(CancelEventArgs e)
{
    this.Hide();
    e.Cancel = true;
}

3 More references

更多 Demo 源码请参考 HandyControl

本文源码TerminalMACS

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读