Search code examples
c#wpfclickright-clicknotifyicon

Detect right mouse click on NotifyIcon in WPF


I´m trying to open a small WPF application when right clicking on a NotifyIcon.
I added the NotifyIcon by adding the System.Windows.Forms to the resources and this lines:

System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon(@"C:\...\icon.ico");
ni.Visible = true;

The way I´ve tried to implement the right click event:

ni.Click += delegate (object sender, System.EventArgs e)
{
  if (e.Equals(MouseButtons.Right))
  {
    MessageBox.Show("Haha", "haha", MessageBoxButton.OK);
  }
}

Both code snippets are in the MainWindow() function.


Solution

  • It is called the context menu

    private void Form1_Load(object sender, EventArgs e)
    {
        notifyIcon1.ContextMenu = contextMenu1;
    }