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 に答える