1

可能重复:
如何在 c# windows 窗体应用程序中将图标添加到上下文菜单

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

private NotifyIcon  trayIcon;
private ContextMenu trayMenu;

    trayMenu = new ContextMenu();

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

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

4

1 回答 1

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;

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

于 2012-10-27T20:09:56.950 回答