Wie erstellt man eine .NET Windows-Anwendung für das System-Tray?

Antwort

Um eine .NET Windows-Anwendung zu erstellen, die im System-Tray (Infobereich) läuft, gehst du wie folgt vor: **1. Neues Projekt anlegen:** Erstelle ein neues Windows Forms App-Projekt in Visual Studio (z.B. mit C#). **2. NotifyIcon hinzufügen:** Ziehe aus der Toolbox das Steuerelement „NotifyIcon“ auf das Formular. Alternativ kannst du es im Code hinzufügen: ```csharp private NotifyIcon notifyIcon; public Form1() { InitializeComponent(); notifyIcon = new NotifyIcon(); notifyIcon.Icon = SystemIcons.Application; // Oder ein eigenes Icon notifyIcon.Visible = true; notifyIcon.Text = "Meine Tray-Anwendung"; // Optional: Kontextmenü hinzufügen var contextMenu = new ContextMenuStrip(); contextMenu.Items.Add("Beenden", null, (s, e) => Application.Exit()); notifyIcon.ContextMenuStrip = contextMenu; // Optional: Doppelklick-Event notifyIcon.DoubleClick += (s, e) => this.Show(); } ``` **3. Fenster ausblenden statt schließen:** Überschreibe das Schließen-Verhalten, damit das Fenster nur minimiert wird: ```csharp protected override void OnFormClosing(FormClosingEventArgs e) { e.Cancel = true; this.Hide(); base.OnFormClosing(e); } ``` **4. Anwendung starten:** Beim Start kannst du das Hauptfenster ausblenden, wenn du möchtest: ```csharp protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Hide(); } ``` **5. Icon nicht vergessen:** Setze ein eigenes Icon für das NotifyIcon, damit es im Tray angezeigt wird. **6. Anwendung beenden:** Stelle sicher, dass beim Beenden das NotifyIcon entfernt wird: ```csharp protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); notifyIcon.Dispose(); } base.Dispose(disposing); } ``` **Weitere Hinweise:** - Das Tray-Icon funktioniert sowohl unter .NET Framework als auch unter .NET (Core/5/6/7+). - Für WPF-Anwendungen gibt es ähnliche Ansätze, aber meist empfiehlt sich ein zusätzliches NuGet-Paket wie [Hardcodet.NotifyIcon.Wpf](https://github.com/hardcodet/wpf-notifyicon). **Dokumentation:** - [NotifyIcon-Klasse (Microsoft)](https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.notifyicon) Damit hast du eine einfache System-Tray-Anwendung in .NET erstellt.

Frage stellen und sofort Antwort erhalten