欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > WPF 如何添加系统托盘

WPF 如何添加系统托盘

2025/9/23 11:29:31 来源:https://blog.csdn.net/oYangYongJie/article/details/145116879  浏览:    关键词: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;  // importantTopmost = false; // importantFocus();         // 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;}elseWindowState = 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;  // importantTopmost = false; // importantFocus();         // important}
    }

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词