Frage

Ich habe eine ICO-Datei, die als Ressource eingebettet ist (Aktion baut auf Ressourcen). Ich versuche, eine NotifyIcon zu erstellen. Wie kann ich meine Referenz-Symbol?

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

Lösung

sollte Ihre Symboldatei an einen Ihrer Projekt Baugruppen und seine Build Action sollte Ressource eingestellt werden hinzugefügt werden.

: ein Verweis auf die Assembly Nach dem Hinzufügen können Sie eine NotifyIcon wie folgt erstellen
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 );

Andere Tipps

Ein gemeinsames Nutzungsmuster ist das Notify-Symbol das gleiche wie das Hauptfenster des Symbol zu haben. Das Symbol wird als PNG-Datei definiert ist.

Um dies zu tun, fügen Sie das Bild auf die Ressourcen des Projekts und verwenden Sie dann wie folgt vor:

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

Im Fenster 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">

habe ich ein Projekt hier und verwendet, um eine eingebettete Ressource (Aktion bauen wurde auf Eingebettete Ressource, anstatt nur Ressource). Diese Lösung funktioniert nicht mit Ressourcen arbeiten, aber Sie können es manipulieren können. Ich habe dies auf der OnIntialized (), aber es muss nicht dorthin gehen.

//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;
    }
scroll top