NotifyIconNotShowing我正在写一个简单的应用程序,我想使用notifyIcon而不是form来控制,我通过Google找到了示例,但我的notifyIcon不会显示。我研究竟做错了什么?staticclassMainEntryClass{//////应用程序的主要入口点。///[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);C2FTestApp=newC2F();应用程序运行();TestApp.Dispose();}}classC2F{publicC2F(){InitializeComponent();加载设置();}privatevoidInitializeComponent(){this.components=newSystem.ComponentModel.Container();System.ComponentModel.ComponentResourceManager资源=newSystem.ComponentModel.ComponentResourceManager(typeof(C2F));this.niC2F=newSystem.Windows.Forms.NotifyIcon(this.components);this.contextMenuStrip1=newSystem.Windows.Forms.ContextMenuStrip(this.components);this.settingsToolStripMenuItem=newSystem.Windows.Forms.ToolStripMenuItem();this.separatorToolStripMenuItem=newSystem.Windows.Forms.ToolStripSeparator();this.exitToolStripMenuItem=newSystem.Windows.Forms.ToolStripMenuItem();这.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+=newSystem.Windows.Forms.MouseEventHandler(this.niC2F_MouseClick);////contextMenuStrip1//this.contextMenuStrip1.Items.AddRange(newSystem.Windows.Forms.ToolStripItem[]{this.settingsToolStripMenuItem,this.separatorToolStripMenuItem,this.exitToolStripMenuItem});this.contextMenuStrip1.Name="contextMenuStrip1";this.contextMenuStrip1.Size=newSystem.Drawing.Size(153,76);////settingsToolStripMenuItem//this.settingsToolStripMenuItem.Name="settingsToolStripMenuItem";this.settingsToolStripMenuItem.Size=newSystem.Drawing.Size(152,22);this.settingsToolStripMenuItem.Text="设置";this.settingsToolStripMenuItem.Click+=newSystem.EventHandler(this.settingsToolStripMenuItem_Click);////separatorToolStripMenuItem//this.separatorToolStripMenuItem.Name="separatorToolStripMenuItem";this.separatorToolStripMenuItem.Size=newSystem.Drawing.Size(149,6);this.separatorToolStripMenuItem.Click+=newSystem.EventHandler(this.exitToolStripMenuItem_Click);////exitToolStripMenuItem1//this.exitToolStripMenuItem.Name="exitToolStripMenuItem1";this.exitToolStripMenuItem.Size=newSystem.Drawing.Size(152,22);this.exitToolStripMenuItem.Text="退出";}privateSystem.ComponentModel.IContainercomponents=null;私有Form1frmSettings=newForm1();私人设置C2FSettings=newSettings();私有System.Windows.Forms.NotifyIconniC2F;私人System.Windows.Forms.ContextMenuStripcontextMenuStrip1;privateSystem.Windows.Forms.ToolStripMenuItemsettingsToolStripMenuItem;私有系统.Windows.Forms.ToolStripSeparatorseparatorToolStripMenuItem;privateSystem.Windows.Forms.ToolStripMenuItemexitToolStripMenuItem;我实际上刚刚完成了一个以NotifyIcon开头的项目你的代码(我猜你只是提供了一个片段)与我的非常相似。我检查了你的代码,我唯一要做的改变就是改变你调用图标的方式:this.niC2F.Icon=newSystem.Drawing.Icon(@"C:PathToIconiconfile.ico");使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Windows.Forms;namespaceTestApp{staticclassMainEntryClass{//////应用程序的主要入口点。///[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);C2FTestApp=newC2F();应用程序运行();}}classC2F{System.ComponentModel.Container组件;System.Drawing.Icon图标;ContextMenuStriprightClickMenu=newContextMenuStrip();NotifyIcon托盘图标;ToolStripMenuItem选项=newToolStripMenuItem();ToolStripMenuItemrestore=newToolStripMenuItem();ToolStripMenuItemexit=newToolStripMenuItem();ToolStripSeparator分隔符=newToolStripSeparator();公共C2F(){InitializeComponent();}privatevoidInitializeComponent(){icon=newSystem.Drawing.Icon(@"C:PathToIconiconfile.ico");component=newSystem.ComponentModel.Container();trayIcon=newNotifyIcon(组件);trayIcon.Text="账单提醒";trayIcon.Icon=图标;trayIcon.Visible=true;trayIcon.DoubleClick+=newEventHandler(trayIcon_DoubleClick);trayIcon.ContextMenuStrip=rightClickMenu;rightClickMenu.Items.AddRange(newSystem.Windows.Forms.ToolStripItem[]{options,seperator,restore,exit});options.Font=newSystem.Drawing.Font("SegoeUI",9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));options.Text="选项";options.Click+=newEventHandler(options_Click);restore.Font=newSystem.Drawing.Font("SegoeUI",9F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((byte)(0)));restore.Text="恢复窗口";restore.Click+=newEventHandler(restore_Click);exit.Font=newSystem.Drawing.Font("SegoeUI",9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));exit.Text="退出";exit.Click+=newEventHandler(exit_Click);}voidexit_Click(objectsender,EventArgse){Application.Exit();}voidrestore_Click(objectsender,EventArgse){FormNameshowWindow=newFormName();showWindow.Show();}voidoptions_Click(objectsender,EventArgse){Settings_Windowsettings=newSettings_Window();设置.显示();}voidtrayIcon_DoubleClick(objectsender,EventArgse){FormNameshowWindow=newFormName();showWindow.Show();希望这对您有所帮助,如果您有任何问题,请联系我!NotifyIcon未显示的另一个原因是当托盘应用程序未运行时Windows资源管理器是否以提升的权限运行(当然仅在具有UAC的系统上)如果explorer.exe崩溃或被杀死,则可能发生这种情况在这种情况下,用户然后从提升的任务管理器手动重新启动它。NotifyIcon控件在内部使用Shell_NotifyIcon本机方法,但不检查返回值。如果Shell_NotifyIcon返回FALSE,您将不会收到通知。我不得不在Shell_NotifyIcon上使用WinDbg断点,GetLastError给了我ERROR_ACCESS_DENIED。所以我意识到存在权限问题,可能是由重新启动的资源管理器提升引起的。进一步的测试证实了这一假设。然而,这是一个相当罕见的案例。以上是C#学习教程:NotifyIcon不显示所有分享的内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
