Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a ContextMenuStrip without it showing in the taskbar

Tags:

c++

.net

winforms

I have found that when I execute the show() method for a contextmenustrip (a right click menu), if the position is outside that of the form it belongs to, it shows up on the taskbar also.

I am trying to create a right click menu for when clicking on the notifyicon, but as the menu hovers above the system tray and not inside the form (as the form can be minimised when right clicking) it shows up on the task bar for some odd reason

Here is my code currently:

private: System::Void notifyIcon1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {

if(e->Button == System::Windows::Forms::MouseButtons::Right) {

        this->sysTrayMenu->Show(Cursor->Position);

    }
}

What other options do I need to set so it doesn't show up a blank process on the task bar.

like image 692
Cetra Avatar asked Sep 25 '08 11:09

Cetra


3 Answers

Try assigning your menu to the ContextMenuStrip property of NotifyIcon rather than showing it in the mouse click handler.

like image 55
Grokys Avatar answered Nov 07 '22 20:11

Grokys


The best and right way, without Reflection is:

{
  UnsafeNativeMethods.SetForegroundWindow(new HandleRef(notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle));
  notifyIcon.ContextMenuStrip.Show(Cursor.Position);
}

where UnsafeNativeMethods.SetForegroundWindow is:

public static class UnsafeNativeMethods
{
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern bool SetForegroundWindow(HandleRef hWnd);
}
like image 28
MaratSh Avatar answered Nov 07 '22 19:11

MaratSh


Let's assume that you have 2 context menu items: ContextMenuLeft and ContextMenuRight. By default, from the NotifyIcon properties you already assigned one of them. Before calling Left Button Click, just change them, show the context menu, and then change them again.

NotifyIcon.ContextMenuStrip = ContextMenuLeft; //let's asign the other one
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(NotifyIcon, null);
NotifyIcon.ContextMenuStrip = ContextMenuRight; //switch back to the default one

Hope this helps.

like image 40
Dicu Alexandru Avatar answered Nov 07 '22 20:11

Dicu Alexandru