2009-05-09 5 views
2

양식보다는 notifyIcon을 사용하여 제어하려는 간단한 응용 프로그램을 작성 중이지만 Google을 통해 찾은 예제가 있지만 알림이 표시되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?NotifyIcon not showing


    static class MainEntryClass 
    { 
     /// 
     /// The main entry point for the application. 
     /// 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      C2F TestApp = new C2F(); 
      Application.Run(); 
      TestApp.Dispose(); 
     } 
    } 

    class C2F 
    { 
     public C2F() 
     { 
      InitializeComponent(); 
      loadSettings(); 
     } 

     private void InitializeComponent() 
     { 
      this.components = new System.ComponentModel.Container(); 
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(C2F)); 
      this.niC2F = new System.Windows.Forms.NotifyIcon(this.components); 
      this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); 
      this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.separatorToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator(); 
      this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.contextMenuStrip1.SuspendLayout(); 
      // 
      // niC2F 
      // 
      this.niC2F.BalloonTipText = "MyApp"; 
      this.niC2F.Icon = ((System.Drawing.Icon)(Clipboard2File.Properties.Resources.ResourceManager.GetObject("MyIcon.ico"))); 
      this.niC2F.Text = "MyApp"; 
      this.niC2F.ContextMenuStrip = this.contextMenuStrip1; 
      this.niC2F.ShowBalloonTip(5); 
      this.niC2F.Visible = true; 
      this.niC2F.MouseClick += new System.Windows.Forms.MouseEventHandler(this.niC2F_MouseClick); 
      // 
      // contextMenuStrip1 
      // 
      this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.settingsToolStripMenuItem, 
      this.separatorToolStripMenuItem, 
      this.exitToolStripMenuItem}); 
      this.contextMenuStrip1.Name = "contextMenuStrip1"; 
      this.contextMenuStrip1.Size = new System.Drawing.Size(153, 76); 
      // 
      // settingsToolStripMenuItem 
      // 
      this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; 
      this.settingsToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
      this.settingsToolStripMenuItem.Text = "Settings"; 
      this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click); 
      // 
      // separatorToolStripMenuItem 
      // 
      this.separatorToolStripMenuItem.Name = "separatorToolStripMenuItem"; 
      this.separatorToolStripMenuItem.Size = new System.Drawing.Size(149, 6); 
      this.separatorToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); 
      // 
      // exitToolStripMenuItem1 
      // 
      this.exitToolStripMenuItem.Name = "exitToolStripMenuItem1"; 
      this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
      this.exitToolStripMenuItem.Text = "Exit"; 
     } 

     private System.ComponentModel.IContainer components = null; 
     private Form1 frmSettings = new Form1(); 
     private Settings C2FSettings = new Settings(); 
     private System.Windows.Forms.NotifyIcon niC2F; 
     private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; 
     private System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem; 
     private System.Windows.Forms.ToolStripSeparator separatorToolStripMenuItem; 
     private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; 
    } 
+0

미래에 가능한 한 많은 관련 태그를 질문에 추가하십시오 (제한이 5라고 생각합니까?) 사람들이 귀하의 질문을 쉽게 찾을 수 있도록 도와줍니다. –

답변

8

저는 실제로 NotifyIcon으로 시작한 프로젝트를 마쳤습니다. 귀하의 코드 (당신이 방금 스 니펫을 제공했다고 추측합니다)는 내 것과 매우 유사합니다.

나는 코드를 확인하고, 유일한 변화는 내가 일을 당신이에있는 아이콘을 불리는 방법이 변경되었다 얻을했습니다 : 오른쪽 클릭 메뉴와 작업 예제 아래

this.niC2F.Icon = new System.Drawing.Icon(@"C:\PathToIcon\iconfile.ico"); 

입니다 더블 클릭 기능 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace TestApp 
{ 
    static class MainEntryClass 
    { 
     /// 
     /// The main entry point for the application. 
     /// 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      C2F TestApp = new C2F(); 
      Application.Run(); 
     } 
    } 

    class C2F 
    { 
     System.ComponentModel.Container component; 
     System.Drawing.Icon icon; 
     ContextMenuStrip rightClickMenu = new ContextMenuStrip(); 
     NotifyIcon trayIcon; 
     ToolStripMenuItem options = new ToolStripMenuItem(); 
     ToolStripMenuItem restore = new ToolStripMenuItem(); 
     ToolStripMenuItem exit = new ToolStripMenuItem(); 
     ToolStripSeparator seperator = new ToolStripSeparator(); 

     public C2F() 
     { 
      InitializeComponent(); 
     } 

     private void InitializeComponent() 
     { 
      icon = new System.Drawing.Icon(@"C:\PathToIcon\iconfile.ico"); 
      component = new System.ComponentModel.Container(); 
      trayIcon = new NotifyIcon(component); 
      trayIcon.Text = "Bill Reminder"; 
      trayIcon.Icon = icon; 
      trayIcon.Visible = true; 
      trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick); 
      trayIcon.ContextMenuStrip = rightClickMenu; 

      rightClickMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] 
      { 
       options, 
       seperator, 
       restore, 
       exit 
      }); 

      options.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      options.Text = "Options"; 
      options.Click += new EventHandler(options_Click); 

      restore.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      restore.Text = "Restore Window"; 
      restore.Click += new EventHandler(restore_Click); 

      exit.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      exit.Text = "Exit"; 
      exit.Click += new EventHandler(exit_Click); 
     } 

     void exit_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     void restore_Click(object sender, EventArgs e) 
     { 
      FormName showWindow = new FormName(); 
      showWindow.Show(); 
     } 

     void options_Click(object sender, EventArgs e) 
     { 
      Settings_Window settings = new Settings_Window(); 
      settings.Show(); 
     } 

     void trayIcon_DoubleClick(object sender, EventArgs e) 
     { 
      FormName showWindow = new FormName(); 
      showWindow.Show(); 
     } 
    } 

} 

당신이 궁금한 점이 있으면 알려주세요.

+0

정말 고맙습니다. 응용 프로그램에 아이콘 파일을 추가하는 방법이 있습니까? 그래서 위치를 알 필요가 없습니까? – Tester101

+0

FormName이란 무엇입니까? – user1709408

5

NotifyIcon이 표시되지 않는 또 다른 이유는 트레이 응용 프로그램이없는 동안에는 Windows 탐색기가 상승 된 권한으로 실행되고있는 것입니다 (물론 UAC가있는 시스템에서만).

explorer.exe가 손상되었거나 죽은 다음 사용자가 상승 된 작업 관리자에서 수동으로 다시 시작한 경우에 발생할 수 있습니다.

NotifyIcon 컨트롤은 내부에서 Shell_NotifyIcon 네이티브 메서드를 사용하지만 반환 값을 확인하지 않습니다. Shell_NotifyIcon이 FALSE를 반환하면 알림 메시지가 표시되지 않습니다.

Shell_NotifyIcon에서 WinDbg와 중단 점을 가져야하고 GetLastError가 ERROR_ACCESS_DENIED를주었습니다. 그래서 권한 문제가 있다는 것을 깨달았습니다. 다시 시작한 탐색기 상승으로 인한 것일 수 있습니다. 추가 테스트를 통해 이러한 가정이 확인되었습니다.

그러나 이것은 드문 경우입니다.

관련 문제