1. 程式人生 > >【WPF】Xaml設定圖示

【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;
}