是否有可能代替 NotifyIcon 的 ContextMenuStrip,显示一个行为类似于 ContextMenuStrip 的表单?
Is it possible that instead ContextMenuStrip of NotifyIcon, display a Form, which will behave like ContextMenuStrip?
我想用更复杂的 Form
替换 NotifyIcon
的 ContextMenuStrip
。当用户单击系统托盘中的 NotifyIcon 时,我可以显示表单,但是当用户单击其他地方时,我无法 hide/close 像 ContextMenuStrip 这样的表单关闭。
这可能吗?
这是示例代码,我是这样显示的:
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();
}
并且FormMenu
是一个带有面板和多个按钮的复杂表单。
您可以使用 ToolStripControlHost
在 ContextMenuStrip
中托管您的复杂控件或表单。
代码:
var c= new MyUserControl();
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(c));
您也可以通过这种方式将表单添加到上下文菜单条中:
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) );
截图:
在上面的屏幕截图中,我向没有其他项目的上下文菜单条添加了一个表单,并将上下文菜单的 ShowImageMargin
属性 设置为 false
。
您还可以有其他项目和子菜单。
我想用更复杂的 Form
替换 NotifyIcon
的 ContextMenuStrip
。当用户单击系统托盘中的 NotifyIcon 时,我可以显示表单,但是当用户单击其他地方时,我无法 hide/close 像 ContextMenuStrip 这样的表单关闭。
这可能吗?
这是示例代码,我是这样显示的:
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();
}
并且FormMenu
是一个带有面板和多个按钮的复杂表单。
您可以使用 ToolStripControlHost
在 ContextMenuStrip
中托管您的复杂控件或表单。
代码:
var c= new MyUserControl();
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(c));
您也可以通过这种方式将表单添加到上下文菜单条中:
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) );
截图:
在上面的屏幕截图中,我向没有其他项目的上下文菜单条添加了一个表单,并将上下文菜单的 ShowImageMargin
属性 设置为 false
。
您还可以有其他项目和子菜单。