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
    }
}
相关推荐
沉到海底去吧Go7 分钟前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19728 分钟前
自定义事件wpf
wpf
code bean4 小时前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静15 小时前
WPF可拖拽ListView
c#·wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon2 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars4 天前
WPF中自定义消息弹窗
wpf
Magnum Lehar4 天前
GameEngine游戏引擎前端界面wpf页面实现
前端·游戏引擎·wpf
TA远方4 天前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console
陈奕昆5 天前
2.1HarmonyOS NEXT开发工具链进阶:DevEco Studio深度实践
华为·wpf·harmonyos