Laden...

NotifyIcon aber kein Kontext Menu

Erstellt von JMano vor 10 Jahren Letzter Beitrag vor 10 Jahren 1.096 Views
J
JMano Themenstarter:in
38 Beiträge seit 2013
vor 10 Jahren
NotifyIcon aber kein Kontext Menu

Hallo!

Ich habe mir eine Anwendung schrieben die als Tray Icon (NotifyIcon) laufen soll.

Prinzipiell läuft die Anwendung nur ich bekomme kein Kontext Menu auf dem Icon hin.
Ich habe mal den Code Reduziert, damit ich ihn hier übersichtlich Posten kann.
(lauffähig ist er trotzdem)


   public class Anwendung
   {
      public const string PathTo = @"D:\";
      public const string TheFile = @"InfoFile.txt";
      
      public static NotifyIcon ApIcon;
      public static FileSystemWatcher FsWatcher;

      public ContextMenu TrayMenu;
      public MenuItem TrayMenuItem1;

      public static bool Running;

      /// <summary>
      /// Mains this instance.
      /// </summary>
      public static void Main()
      {
         Anwendung myApp = new Anwendung();
         myApp.Run();
         myApp.ExitAppl();
      }

      /// <summary>
      /// Runs this instance.
      /// </summary>
      public void Run()
      {
         Running = true;
         // Console.WriteLine("Press \'q\' to quit the sample.");
         // while (Console.Read() != 'q' && Running) ;
         while (Running) ;
      }

      /// <summary>
      /// Initializes a new instance of the <see cref="Anwendung"/> class.
      /// </summary>
      public Anwendung()
      {
         TrayMenu = new ContextMenu();
         TrayMenuItem1 = new MenuItem();
         TrayMenuItem1.Index = 0;
         TrayMenuItem1.Text = "Exit";
         TrayMenuItem1.Click += new System.EventHandler(OnExit);
         TrayMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { TrayMenuItem1 });

         ApIcon = new NotifyIcon();
         ApIcon.Icon = Properties.Resources.Icon1;
         ApIcon.ContextMenu = TrayMenu;
         ApIcon.DoubleClick += new EventHandler(OnExit);
         ApIcon.MouseDoubleClick += new MouseEventHandler(OnExit);
         ApIcon.Text = "The Notifier";
         ApIcon.Visible = true;

         FsWatcher = new FileSystemWatcher(PathTo);
         FsWatcher.NotifyFilter = NotifyFilters.LastWrite;
         FsWatcher.Filter = TheFile;

         FsWatcher.Changed += new FileSystemEventHandler(OnChanged);
         FsWatcher.EnableRaisingEvents = true;
      }


      public static void OnExit(object source, EventArgs e)
      {
          Running = false;
      }

      ~Anwendung()
      {
         ExitAppl();
      }

      public void ExitAppl()
      {
         ApIcon.Visible = false;
         ApIcon.Dispose();
         FsWatcher.EnableRaisingEvents = false;
         FsWatcher.Dispose();
      }

      private static void OnChanged(object source, FileSystemEventArgs e)
      {
          Anwendung.UpdateInfo(Anwendung.ReadInfoFromFile());
      }

      public static void UpdateInfo(string msg)
      { 
          // Maybe some other changes ....
               ApIcon.BalloonTipTitle = "Title:";
               ApIcon.BalloonTipText = msg;
               ApIcon.ShowBalloonTip(5000);
      }

      public static string ReadInfoFromFile()
      {
          string InfoFromFile;
          string line = File.ReadLines(PathTo + TheFile).Last();
          // Process line --> InfoFromFile
                InfoFromFile = line;


          return InfoFromFile;
      }
   }

Ich habs auch auf verschiedene weißen versucht,
aber kriege das Kontext menu nicht hin. Wenn ich rechts klicke passiert gar nicht 😦

Wenn jemand Infos hat worann es liegen mag ...

D
216 Beiträge seit 2009
vor 10 Jahren

Du musst Application.Run() ausführen, damit das mit dem TrayIcon funktioniert. Als kleines Beispiel kannst du dir [Snippet] Vorlage für Tray-/NotifyIcon-Anwendung angucken.