【WPF】Xaml设置图标



1.WPF程序在XAML文件中设置任务栏图标

<Window x:Class="ShAirportDataSync.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ShAirportDataSync"
        mc:Ignorable="d"
        Title="窗体标题" Icon="XX.ico" Height="500" Width="850">
    <!--使用Window的Icon属性设置图标-->
    <Grid>
    ******
    </Grid>
</Window>


2.WPF程序设置通知栏图标

NotifyIcon notifyIcon;
public MainWindow()
{
    InitializeComponent();
    //图标
    notifyIcon = new NotifyIcon();
    //notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
    notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
    notifyIcon.BalloonTipText = "提示文字****";
    notifyIcon.ShowBalloonTip(3000);
    notifyIcon.Visible = true;
}


3.WPF程序通知栏右键菜单

//图标右键菜单
notifyIcon.ContextMenu = new ContextMenu(new []
{
    new MenuItem("隐藏窗体", (sender, args) =>
    {
        Visibility = System.Windows.Visibility.Hidden;
        ShowInTaskbar = false;
    }),
    new MenuItem("打开窗体", (sender, args) =>
    {
        Visibility = System.Windows.Visibility.Visible;
        ShowInTaskbar = true;
        Activate();
    }),
    new MenuItem("退出程序", (sender, args) => 
    {
        System.Windows.Application.Current.Shutdown();
    })
});
//双击通知栏图标
notifyIcon.MouseDoubleClick += (sender, args) =>
{
    Visibility = System.Windows.Visibility.Visible;
    ShowInTaskbar = true;
    Activate();
};


4.关闭WPF窗体到后台运行

protected override void OnClosing(CancelEventArgs e)
{
    //取消关闭窗体
    e.Cancel = true;
    //将窗体变为最小化
    this.WindowState = WindowState.Minimized;
    //不显示在系统任务栏
    this.ShowInTaskbar = false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值