2012-10-27 48 views
1

可能重複:
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(); 

問題是,我似乎無法找到任何屬性來爲每個菜單項設置圖像/圖標。這可能嗎?任何幫助將不勝感激。

+0

WinForms或WPF? –

+0

您的答案在這裏http://stackoverflow.com/questions/6555691/how-to-add-icon-to-context-menu-in-c-sharp-windows-form-application –

回答

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;

謝謝,
我希望你覺得這有幫助:)

+1

謝謝!這工作完美:) –

+0

@DerekBrown沒有任何問題,我很高興我可以幫助。祝你有美好的一天 :) –