当前位置: 首页 > article >正文

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.单机托盘显示窗体

 if (!IsVisible)
 {
     Show();
 }

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

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

示例

  • xaml
  • <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
  • 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
        }
    }


http://www.kler.cn/a/506840.html

相关文章:

  • LLMs之RAG:《EdgeRAG: Online-Indexed RAG for Edge Devices》翻译与解读
  • WPF实现动态四宫格布局
  • (01)FreeRTOS移植到STM32
  • 鸿蒙打包发布
  • ros2笔记-6.2 使用urdf创建机器人模型
  • 【AI日记】25.01.15
  • 堆的实现【C++】
  • 解决 Error: Invalid or corrupt jarfile day04_studentManager.jar 报错问题
  • 利用平面进行位姿约束优化
  • .NET MAUI进行UDP通信
  • 华为手机改ip地址能改定位吗
  • [操作系统] 深入理解操作系统的概念及定位
  • 阻塞赋值和非阻塞赋值
  • 初学stm32 --- CAN
  • 在 pom.xml 文件中指定 repositories
  • 论文高级GPT指令推荐
  • HTML学习笔记记录---速预CSS(2) 复合属性、盒子模型、边框线、浮动、定位
  • 50.【8】BUUCTF WEB HardSql
  • knowledge-vue监听传入值变化请求后端数据更新
  • 如何在linux系统上完成定时开机和更新github端口的任务
  • springboot 项目配置https
  • Rust 零大小类型(ZST)
  • 【设计模式-结构型】装饰器模式
  • C++ union 联合(八股总结)
  • 微调神经机器翻译模型全流程
  • 紫光无人机AI飞控平台介绍