为我的menulist添加图标

时间:2012-10-27 20:01:04

标签: c# windows contextmenu notifyicon

  

可能重复:
  how to add icon to context menu in c# windows form application

我有一个附加到任务托盘应用程序的上下文菜单。代码如下。

private NotifyIcon  trayIcon;
private ContextMenu trayMenu;

    trayMenu = new ContextMenu();

    trayMenu.MenuItems.Add("Login", OnLogin);
    trayMenu.MenuItems.Add("LogOut", OnLogOut);
    trayIcon = new NotifyIcon();

问题是我似乎找不到任何属性来为每个菜单项设置图像/图标。这可能吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为您可以在ContextMenuStrip中添加图片,但无法使用ContextMenu执行此操作。这是一个关于如何执行此操作的简单示例

示例

private void Form1_Load(object sender, EventArgs e)
{
    Image ContextMenuStripItemImages = Image.FromFile(@"D:\Resources\International\Picrofo_Logo.png"); //Set the image from the path provided
    NotifyIcon trayIcon;
    ContextMenuStrip trayMenu;
    trayMenu = new ContextMenuStrip();
    trayMenu.Items.Add("Login", ContextMenuStripItemImages).Click += new EventHandler(Login_Click); //Create a new item in the context menu strip and link its Click event with Login_Click
    trayMenu.Items.Add("LogOut", ContextMenuStripItemImages).Click += new EventHandler(LogOut_Click); //Create a new item in the context menu strip and link its Click event with LogOut_Click
    trayIcon = new NotifyIcon();
    trayIcon.ContextMenuStrip = trayMenu; //Set the ContextMenuStrip of trayIcon to trayMenu
}

private void Login_Click(object sender, EventArgs e)
{
    //Do something when Login is clicked
}

private void LogOut_Click(object sender, EventArgs e)
{
    //Do something when LogOut is clicked
}

注意:当您准备好向用户展示NotifyIcon时,您可以使用NotifyIcon.Visible = true;

谢谢, 我希望你觉得这很有帮助:)