当鼠标离开容器时,关闭Winform MenuStrip [英] Closing Winform MenuStrip when mouse leaves the container
问题描述
我整天都在处理这个问题.我有一个带有几个菜单项的菜单条. 每个项目,也有很多项目.现在,如果我单击父菜单项,则一个容器(类似于contextmenu)将保持打开状态,直到我单击其他位置为止.
I´m have been dealing with this issue all day. I have a menustrip with several menu items. Each item, also have many items. Now, if I click the parent menu item, a container(similar to contextmenu) stays opened until I click somewhere else.
在图像示例中,"Clientes"将是父项,并在其"container"中带有子菜单项.
In the image example, "Clientes" will be the parent item, and there its the "container" with their child menu items.
我想做的是,当鼠标离开父项或容器区域时,关闭该容器".我不想单击表格的任何其他部分以将其关闭.当我这么说时,这是因为我实际上是在使用WPF按钮,所以我需要这样做.
What I wanted to do, is to close that "container" when the mouse leaves the parent item or the container area. I dont want to have to click in any other part of the form to close it. When I say that, its because I´m actually using WPF buttons and I need to do it this way.
我在这里需要帮助,希望有人能给我一些建议.
I need help here, hope somebody can give me some advice.
我尝试使用父项("Clientes"等)的MouseLeave事件,并且做到了:
I tried to use the MouseLeave event of the parents items ("Clientes", etc.) and there did:
private void clientesToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
clientesToolStripMenuItem.DropDown.Close();
}
这有效,但是很明显,当鼠标离开菜单项"Clientes"时,它关闭了父级和容器.我需要一种方法来确定鼠标是否位于父菜单项之上或位于其子菜单项之上,以便在我知道鼠标位于其他位置时将其关闭.
This works, but obviously, it closes the parent and also the container, when the mouse leaves menuitem "Clientes". I need a way to know if the mouse is over the parent menuitem or over one of their childs so as to close it if I know that the mouse is somewhere else.
希望有人可以帮助我.
推荐答案
您可以通过处理clientesToolStripMenuItem本身的事件来实现.当鼠标进入您的客户主菜单矩形时,只需显示您的容器并在其离开该矩形时将其隐藏即可.假设picContainer是您的矩形,则可以执行以下操作:
You can achieve this by handling events of clientesToolStripMenuItem itself. When the mouse enters your clients main-menu rectangle, just show your container and hide it when it leaves this rectangle. Assuming picContainer is your rectangle, you may do something like this:
private void clientToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
picContainer.Location = clientToolStripMenuItem.ContentRectangle.Location;
picContainer.Show();
}
private void clientToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
this.picContainer.Hide();
}
这篇关于当鼠标离开容器时,关闭Winform MenuStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!