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.
Try assigning your menu to the ContextMenuStrip property of NotifyIcon rather than showing it in the mouse click handler.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With