Notifyicon은 표시되지 않습니다
-
20-08-2019 - |
문제
나는 양식이 아닌 노트 이시콘으로 제어하고 싶은 간단한 응용 프로그램을 작성하고 있습니다. Google을 통해 찾은 Follwed 예제가 있지만 Notifyicon은 표시되지 않습니다. 내가 뭘 잘못하고 있죠?
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;
}
해결책
실제로 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();
}
}
}
이것이 도움이되기를 바랍니다. 궁금한 점이 있으면 알려주세요!
다른 팁
Notifyicon이 표시되지 않는 이유 중 하나는 Tray Application이 아닌 Windows 탐색기가 권한이 높아지는 경우 (물론 UAC가있는 시스템에만) Windows 탐색기가 권한을 높이고 실행 중이기 때문입니다.
Explorer.exe가 추락하거나 죽인 후 사용자가 고가 작업 관리자에서 수동으로 다시 시작한 경우 발생할 수 있습니다.
Notifyicon Control 내부에서 shell_notifyicon 기본 메소드를 사용하지만 반환 값을 확인하지 않습니다. shell_notifyicon이 false를 반환하면 알림을받지 않을 것입니다.
나는 shell_notifyicon에서 Windbg와 함께 중단해야했고 getLasterror는 나에게 error_access_denied를 주었다. 그래서 나는 권한 문제가 있음을 깨달았으며 탐색기 고도를 다시 시작하여 발생할 수 있습니다. 추가 테스트는이 가정을 확인했습니다.
그러나 이것은 다소 드문 경우입니다.