I want to replace ContextMenuStrip
of NotifyIcon
with some more complex Form
. I can display form when user click on NotifyIcon in SystemTray but I can't hide/close form like ContextMenuStrip close when user click somewhere else.
Is that possible?
Here is example code, I Show that form this way:
private void Mouse_Up_Event(object sender, MouseEventArgs e)
{
FormMenu f = new FormMenu();
f.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
f.SetDesktopLocation(Cursor.Position.X - f.Width / 2,
Cursor.Position.Y - f.Height - 20);
f.Show();
f.Focus();
}
And FormMenu
is a complex form with panels and multiple buttons.
You can host your complex control or form in ContextMenuStrip
using ToolStripControlHost
.
Code:
var c= new MyUserControl();
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(c));
You can also add your form to context menu strip this way:
var f = new YourForm() {
TopLevel = false,
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None,
MinimumSize= new Size(200, 200), /*Your preferred size*/
Visible=true
};
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(f) );
Screenshot:
In above screenshot I added a form to a context menu strip that has not other items and set ShowImageMargin
property of context menu to false
.
You can also have other Items and sub menus.