How can I use the Windows look'n'feel for a system tray context menu?
我正在使用
我不希望使用
我只想使用在无数(可能是非.net)应用程序中看到的标准,正常的Windows"外观" * grumble *:
任何有想法的人如何实现这一目标?
在Vista上,使用所有默认设置时,它可以正确呈现(即,DropBox在我的计算机上呈现的方式相同)。
这是一个对我有用的示例程序。尝试一下,如果它不能为您正确呈现,请尝试取消注释两条注释行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; using System.Windows.Forms; using System.Drawing; public class AC : ApplicationContext { NotifyIcon ni; public void menu_Quit(Object sender, EventArgs args) { ni.Dispose(); ExitThread(); } public AC() { ni = new NotifyIcon(); ni.Icon = SystemIcons.Information; ContextMenu menu = new ContextMenu(); menu.MenuItems.Add("Quit", new EventHandler(menu_Quit)); ni.ContextMenu = menu; ni.Visible = true; } public static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); AC ac = new AC(); Application.Run(ac); } } |
作为参考,这是如何使用Windows样式的上下文菜单获取通知图标的方法:
1)在控件工具箱中,右键单击"所有Windows窗体">"选择项"
2)勾选ContextMenu>确定
3)右键单击"所有Windows窗体">"按字母顺序对项目排序"
4)将新的ContextMenu拖动到表单设计器中
5)右键单击ContextMenu1>"属性">,将其重命名为令人难忘的名称(为将来参考,我将我的重命名为
6)在解决方案资源管理器中,展开Form1.cs(等)>双击Form1.Designer.cs
7)找到您的通知图标-它看起来像这样:
1 2 3 4 5 6 7 | // // notifyIcon1 // this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); this.notifyIcon1.Text ="My Notification Icon"; this.notifyIcon1.Visible = true; |
在这里,我以前有一个ContextMenuStrip。
8)将第一行代码更改为(或添加):
1 | this.notifyIcon1.ContextMenu = this.myNotifyIconContext; |
et voila!