我有类似的问题,这是我的解决方案。我创建了常见的 MouseEnter 和 MouseLeave 事件处理程序,并使用计时器在鼠标离开菜单后延迟关闭菜单。
下面是 3 个项目和 1 个分隔符的菜单的示例代码。在示例中,2 个项目与 AutoClose 一起使用,一个(_modeChangingItem)不关闭菜单。您可以很容易地根据您的需要自定义它,例如,不要让任何项目自动关闭。
private Timer _menuTimer = new Timer();
private void MainFrm_Load (object sender, EventArgs e)
{
_menuTimer.Interval = 200;
_menuTimer.Tick += _menuTimer_Tick;
_rootMenuItem.MouseEnter += commonMenu_MouseEnter;
_rootMenuItem.MouseLeave += commonMenu_MouseLeave;
_menuItem1.MouseEnter += commonMenu_MouseEnter;
_menuItem1.MouseLeave += commonMenu_MouseLeave;
_menuItem2.MouseEnter += commonMenu_MouseEnter;
_menuItem2.MouseLeave += commonMenu_MouseLeave;
_separator.MouseEnter += commonMenu_MouseEnter;
_separator.MouseLeave += commonMenu_MouseLeave;
_modeChangingItem.MouseEnter += commonMenu_MouseEnter;
_modeChangingItem.MouseLeave += commonMenu_MouseLeave;
}
private void commonMenu_MouseLeave(object sender, EventArgs e)
{
_menuTimer.Stop();
// Comment this line out if you want none of the items to AutoClose
_rootMenuItem.DropDown.AutoClose = true;
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null) menuItem.Tag = null;
ToolStripSeparator separator = sender as ToolStripSeparator;
if (separator != null) separator.Tag = null;
_menuTimer.Start();
}
private void commonMenu_MouseEnter(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null) menuItem.Tag = new object();
ToolStripSeparator separator = sender as ToolStripSeparator;
if (separator != null) separator.Tag = new object();
}
private void _menuTimer_Tick(object sender, EventArgs e)
{
if (_rootMenuItem.Tag == null && _menuItem1.Tag == null &&
_menuItem2.Tag == null &&
_separator.Tag == null &&
_modeChangingItem.Tag == null)
{
_rootMenuItem.DropDown.Close();
}
_menuTimer.Stop();
}
private void _modeChangingItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem == null) return;
// Move this line to Form_Load if you want none of the items AutoClose
_rootMenuItem.DropDown.AutoClose = false; // Now the menu stays opened
[...]
}
此解决方案为用户节省了额外的点击 - 当您将鼠标移到所有项目之外时,计时器会关闭菜单。