2008-09-16 25 views
57

我有一個作爲資源嵌入的.ico文件(生成操作設置爲資源)。我正在嘗試創建一個NotifyIcon。我怎樣才能引用我的圖標?如何使用WPF中資源的圖標?

notifyIcon = new NotifyIcon(); 
notifyIcon.Icon = ??  // my icon file is called MyIcon.ico and is embedded 
+0

這裏查看類似的問題: [此處輸入鏈接的描述] [1] [1]:http://stackoverflow.com/questions/1127647/convert-system-drawing- icon-to-system-media-imagesource – PatTheFrog 2013-10-02 16:00:02

回答

88

你的圖標文件應該被添加到您的項目組件及其生成操作之一應設置爲資源。增加了程序集的引用後,您可以創建這樣一個NotifyIcon的:

System.Windows.Forms.NotifyIcon icon = new System.Windows.Forms.NotifyIcon(); 
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico")).Stream; 
icon.Icon = new System.Drawing.Icon(iconStream); 
+11

一旦圖標被創建,不要忘記處理iconStream。 – devios1 2010-07-23 03:25:15

+0

感謝這個例子,我終於得到了如何獲得我的應用程序的引用程序集中的資源: new Uri(「pack:// application:,,,/YourReferencedAssembly; component/YourPossibleSubFolder/YourResourceFile.ico」) – 2015-03-19 11:53:37

+0

這樣做允許圖標有多個分辨率? – tofutim 2016-02-27 00:54:52

9

好吧,你不想使用RESX樣式資源:您只是堅持在項目中ICO文件的文件夾中(可以說「ArtWork」),並在屬性中將Build Action設置爲「Resources」...

然後,您可以使用PACK URI在XAML中引用它...「pack:// application:,,, /藝術品/ Notify.ico」

在這裏看到:http://msdn.microsoft.com/en-us/library/aa970069.aspxsample

如果您想要多一點... WPF-like,您應該查看CodePlex上的WPF Contrib項目,該項目具有NotifyIcon控件,您可以在XAML中創建該控件,並使用標準的WPF菜單(因此您可以將任何「在菜單中)。

1

我在這裏創建了一個項目並使用了一個嵌入式資源(構建操作被設置爲嵌入式資源,而不僅僅是資源)。此解決方案不適用於Resource,但您可能可以操作它。我把它放在OnIntialized()上,但它不必去那裏。

//IconTest = namespace; exclamic.ico = resource 
System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream("IconTest.Resources.exclamic.ico"); 

    if (stream != null) 
    { 
     //Decode the icon from the stream and set the first frame to the BitmapSource 
     BitmapDecoder decoder = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); 
     BitmapSource source = decoder.Frames[0]; 

     //set the source of your image 
     image.Source = source; 
    } 
19

一個常見的使用模式是讓通知圖標與主窗口的圖標相同。該圖標被定義爲PNG文件。

要做到這一點,將圖像添加到項目的資源,然後使用方法如下:

var iconHandle = MyNamespace.Properties.Resources.MyImage.GetHicon(); 
this.notifyIcon.Icon = System.Drawing.Icon.FromHandle(iconHandle); 

在窗口XAML:

<Window x:Class="MyNamespace.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:local="clr-namespace:Seahorse" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="600" 
Icon="images\MyImage.png"> 
0

如果你只是在尋找簡單的答案,我認爲這就是MyApp是你的應用程序名稱的地方,這是你的應用程序的根名稱空間名稱。您必須使用pack URI語法,但不必將圖標從您的嵌入式資源中取出就非常複雜。

<Window x:Class="MyApp.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" 
    mc:Ignorable="d" 
    Height="100" 
    Width="200" 
    Icon="pack://application:,,,/MyApp;component/Resources/small_icon.ico">