加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – 如何向System.Windows.Forms.MenuItem添加图标?

发布时间:2020-12-14 02:13:07 所属栏目:Windows 来源:网络整理
导读:我试图在我的一个上下文菜单项中添加一个图标,但我无法做到.有谁能够帮我? 这是我写的代码: private System.Windows.Forms.ContextMenu notifyContextMenu; private void foo() { if (notifyIcon == null) { notifyIcon = new System.Windows.Forms.Notify
我试图在我的一个上下文菜单项中添加一个图标,但我无法做到.有谁能够帮我?

这是我写的代码:

private System.Windows.Forms.ContextMenu notifyContextMenu;
 private void foo() {
            if (notifyIcon == null) {
                notifyIcon = new System.Windows.Forms.NotifyIcon();   
            }

           if (notifyContextMenu == null) {
               notifyContextMenu = new System.Windows.Forms.ContextMenu();
               notifyContextMenu.MenuItems.Add("Exit");
               // How do I add an icon to this context menu item?
             }
            notifyIcon.ContextMenu =  notifyContextMenu;
          }
     }

解决方法

MainMenu / ContextMenu已过时,您应该使用菜单条类.

更改

notifyContextMenu = new System.Windows.Forms.ContextMenu();
notifyContextMenu.MenuItems.Add("Exit");

notifyContextMenu = new System.Windows.Forms.ContextMenuStrip();
var exitMenuItem = notifyContextMenu.Items.Add("Exit");
exitMenuItem.Image = ...;

http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.image.aspx

最后附上上下文菜单条来通知图标,

notifyIcon.ContextMenuStrip = notifyContextMenu;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读