WPF 如何添加系统托盘

1.使用Nuget 添加 handycontrol

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

2.窗体添加控件
cs <hc:NotifyIcon x:Name="NotifyIconContextContent" Text="软件名称" ContextMenu="{StaticResource ContextMenu}" Click="NotifyIconContextContent_Click"> </hc:NotifyIcon>

3.最小化隐藏窗体
cs private void btnMinimize_Click(object sender, RoutedEventArgs e) { WindowState = WindowState.Minimized; ShowInTaskbar = false; }

4.单机托盘显示窗体

cs 复制代码
 if (!IsVisible)
 {
     Show();
 }

 if (WindowState == WindowState.Minimized)
 {
     WindowState = WindowState.Normal;
 }

 Activate();
 Topmost = true;  // important
 Topmost = false; // important
 Focus();         // important

示例

  • xaml
cs 复制代码
<Window x:Class="NBApi.Configurator.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        mc:Ignorable="d"
        Title="NBAPI4PO Configurator" Height="720" Width="870"
        prism:ViewModelLocator.AutoWireViewModel="True"
        WindowStartupLocation="CenterScreen" WindowStyle="None"
        Background="Transparent" AllowsTransparency="True"
        ResizeMode="NoResize" WindowState="Normal" >
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="Loaded">
            <b:InvokeCommandAction Command="{Binding LoadedWindowCommand}"/>
        </b:EventTrigger>
    </b:Interaction.Triggers>
    <Window.Resources>
        <ContextMenu x:Key="ContextMenu">

            <MenuItem Click="MenuItem_Click"  Header="开机自启"/>
            <Separator></Separator>
            <MenuItem Click="MenuItem_Click_1"  Header="最小化到托盘" />

            <MenuItem Click="MenuItem_Click_2"  Header="退出软件"/>

        </ContextMenu>
    </Window.Resources>
    <Border CornerRadius="8" Background="{DynamicResource PrimaryBackgroundColor}" BorderThickness="1" BorderBrush="#D8D8D8">
        <Grid>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="78"/>
                    <RowDefinition/>
                    <RowDefinition Height="60"/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0"  
                        BorderThickness="0 0 0 8" BorderBrush="#4DC253" />
                <hc:NotifyIcon x:Name="NotifyIconContextContent"  Text="软件名称"
                ContextMenu="{StaticResource ContextMenu}" Click="NotifyIconContextContent_Click">
                    
                </hc:NotifyIcon>
//your UI elements
            </Grid>
        </Grid>
    </Border>
</Window>
  • cs
cs 复制代码
namespace NBApi.Configurator.Views;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
// ReSharper disable once RedundantExtendsListEntry
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }

    private void btnRestore_Click(object sender, RoutedEventArgs e)
    {
        if (WindowState == WindowState.Normal)
        {
            WindowState = WindowState.Maximized;
            ShowInTaskbar = false;
        }
        else
            WindowState = WindowState.Normal;

    }

    private void btnMinimize_Click(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Minimized;
        ShowInTaskbar = false;
    }


    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {

    }

    private void MenuItem_Click_1(object sender, RoutedEventArgs e)
    {

    }

    private void MenuItem_Click_2(object sender, RoutedEventArgs e)
    {

    }

    private void NotifyIconContextContent_Click(object sender, RoutedEventArgs e)
    {
        if (!IsVisible)
        {
            Show();
        }

        if (WindowState == WindowState.Minimized)
        {
            WindowState = WindowState.Normal;
        }

        Activate();
        Topmost = true;  // important
        Topmost = false; // important
        Focus();         // important
    }
}
相关推荐
界面开发小八哥6 小时前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计8 小时前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss10 小时前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪10 小时前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮1 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥2 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo3 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown4 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔4 天前
WPF8-常用控件
wpf
积跬步---行千里4 天前
记录一次WPF程序进程挂起问题
mongodb·wpf