是否有可能代替NotifyIcon的ContextMenuStrip,显示一个Form,其行为类似于ContextMenuStrip?

时间:2015-10-16 00:22:01

标签: c# winforms notifyicon

我想将ContextMenuStrip的{​​{1}}替换为更复杂的NotifyIcon。我可以在用户点击SystemTray中的NotifyIcon时显示表单,但是当用户点击其他地方时,我无法隐藏/关闭表单,如关闭ContextMenuStrip。

这可能吗?

以下是示例代码,我以这种方式表示:

Form

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(); } 是一个复杂的表单,带有面板和多个按钮。

1 个答案:

答案 0 :(得分:0)

您可以使用ToolStripControlHostContextMenuStrip中托管您的复杂控件或表单。

<强>代码:

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) );

<强>截图:

enter image description here

在上面的屏幕截图中,我将一个表单添加到没有其他项目的上下文菜单条中,并将上下文菜单的ShowImageMargin属性设置为false

您还可以拥有其他项目和子菜单。