Frage

Possible Duplicate:
how to add icon to context menu in c# windows form application

i've got a context menu attached to a task tray application. The code is as follows.

private NotifyIcon  trayIcon;
private ContextMenu trayMenu;

    trayMenu = new ContextMenu();

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

The problem is that I can't really seem to find any properties to set an image/icon to each menuitem. Is this possible to do? Any help would be greatly appreciated.

War es hilfreich?

Lösung

I think that you can add images within ContextMenuStrip but it's not possible to do this with ContextMenu. Here's a simple example on how to do this

Example

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
}

Notice: When you are ready to show your NotifyIcon to the user, you may use NotifyIcon.Visible = true;

Thanks,
I hope you find this helpful :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top