NotifyIcon的不显示 [英] NotifyIcon not showing
问题描述
我写一个简单的应用程序,我想用NotifyIcon的,而不是一种形式,我已经follwed例子我通过谷歌发现了控制,但我的NotifyIcon将不会出现。我在做什么错了?
静态类MainEntryClass
{
$ /// b $ b ///的主入口点应用程序。
///
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
C2F TestApp =新C2F();
Application.Run();
TestApp.Dispose();
}
}
类C2F
{
公共C2F()
{
的InitializeComponent();
loadSettings();
}
私人无效的InitializeComponent()
{
this.components =新System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager资源=新System.ComponentModel.ComponentResourceManager(typeof运算(C2F));
this.niC2F =新System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 =新System.Windows.Forms.ContextMenuStrip(this.components);
this.settingsToolStripMenuItem =新System.Windows.Forms.ToolStripMenuItem();
this.separatorToolStripMenuItem =新System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem =新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 + =新System.Windows.Forms.MouseEventHandler(this.niC2F_MouseClick);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem [] {
this.settingsToolStripMenuItem,
this.separatorToolStripMenuItem,
this.exitToolStripMenuItem});
this.contextMenuStrip1.Name =contextMenuStrip1;
this.contextMenuStrip1.Size =新System.Drawing.Size(153,76);
//
// settingsToolStripMenuItem
//
this.settingsToolStripMenuItem.Name =settingsToolStripMenuItem;
this.settingsToolStripMenuItem.Size =新System.Drawing.Size(152,22);
this.settingsToolStripMenuItem.Text =设置;
this.settingsToolStripMenuItem.Click + =新System.EventHandler(this.settingsToolStripMenuItem_Click);
//
// separatorToolStripMenuItem
//
this.separatorToolStripMenuItem.Name =separatorToolStripMenuItem;
this.separatorToolStripMenuItem.Size =新System.Drawing.Size(149,6);
this.separatorToolStripMenuItem.Click + =新System.EventHandler(this.exitToolStripMenuItem_Click);
//
// exitToolStripMenuItem1
//
this.exitToolStripMenuItem.Name =exitToolStripMenuItem1;
this.exitToolStripMenuItem.Size =新System.Drawing.Size(152,22);
this.exitToolStripMenuItem.Text =退出;
}
私人System.ComponentModel.IContainer成分= NULL;
私人Form1中frmSettings =新Form1的();
私人设置C2FSettings =新设置();
私人System.Windows.Forms.NotifyIcon niC2F;
私人System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
私人System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem;
私人System.Windows.Forms.ToolStripSeparator separatorToolStripMenuItem;
私人System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
}
其实我刚刚完成一个项目这开始作为一个NotifyIcon的。您的代码(我猜你只是提供一个片段)是令人难以置信的类似我的。
我检查你的代码,我唯一的变化,使得到它工作是改变了方式,你叫图标:
this.niC2F.Icon =新System.Drawing.Icon(@ C:\PathToIcon\iconfile.ico);
下面是一个带有右键菜单和双击功能,工作示例:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;使用System.Windows.Forms的
;
命名TestApp
{
静态类MainEntryClass
{
///
///的主入口点应用程序。
///
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
C2F TestApp =新C2F();
Application.Run();
}
}
类C2F
{
System.ComponentModel.Container组件;
System.Drawing.Icon图标;
的ContextMenuStrip rightClickMenu =新的ContextMenuStrip();
NotifyIcon的任务栏图标;
ToolStripMenuItem选项=新ToolStripMenuItem();
ToolStripMenuItem恢复=新ToolStripMenuItem();
ToolStripMenuItem退出=新ToolStripMenuItem();
ToolStripSeparator分隔符=新ToolStripSeparator();
公共C2F()
{
的InitializeComponent();
}
私人无效的InitializeComponent()
{
图标=新System.Drawing.Icon(@C:\PathToIcon\iconfile.ico) ;
成分=新System.ComponentModel.Container();
托盘图标=新的NotifyIcon(组件);
trayIcon.Text =比尔提醒;
trayIcon.Icon =图标;
trayIcon.Visible = TRUE;
trayIcon.DoubleClick + =新的EventHandler(trayIcon_DoubleClick);
trayIcon.ContextMenuStrip = rightClickMenu;
rightClickMenu.Items.AddRange(新System.Windows.Forms.ToolStripItem []
{
选项,
分隔符,
恢复,
退出
});
options.Font =新System.Drawing.Font(濑越UI,9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0)) );
options.Text =选项;
options.Click + =新的EventHandler(options_Click);
restore.Font =新System.Drawing.Font(濑越UI,9F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((字节)(0)) );
restore.Text =恢复窗口;
restore.Click + =新的EventHandler(restore_Click);
exit.Font =新System.Drawing.Font(濑越UI,9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0)) );
exit.Text =退出;
exit.Click + =新的EventHandler(exit_Click);
}
无效exit_Click(对象发件人,EventArgs五)
{
Application.Exit();
}
无效restore_Click(对象发件人,EventArgs五)
{
窗体名称的ShowWindow =新的窗体名称();
showWindow.Show();
}
无效options_Click(对象发件人,EventArgs五)
{
Settings_Window设置=新Settings_Window();
settings.Show();
}
无效trayIcon_DoubleClick(对象发件人,EventArgs五)
{
窗体名称的ShowWindow =新的窗体名称();
showWindow.Show();
}
}
}
希望这可以帮助你,让我知道如果您有任何问题!
I am writing a simple application that I would like to control with a notifyIcon rather than a form, I have follwed examples I found through Google, but my notifyIcon will not show up. What am I doing wrong?
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;
}
I actually just finished a project that started as a NotifyIcon. Your code (I'm guessing you just provided a snippet) is incredibly similar to mine.
I checked your code, and the only change I had to make to get it to work was changing the way you called the icon to:
this.niC2F.Icon = new System.Drawing.Icon(@"C:\PathToIcon\iconfile.ico");
Below is a working sample with a right-click menu and double-click functionality:
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();
}
}
}
Hope this helps you, let me know if you have any questions!
这篇关于NotifyIcon的不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!