我将MenuStrip
左对接在一个表单中,LayoutStyle = VerticalStackWithOverflow
,并且我编写了这两个事件处理程序,只有当用户将鼠标移到MenuStrip上时才会显示其文本:
Private Sub MenuStripMainMenu_MouseEnter(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MouseEnter
For Each c In MenuStripMainMenu.Items.OfType(Of ToolStripItem)
c.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
Next
End Sub
Private Sub MenuStripMainMenu_MouseLeave(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MouseLeave, MenuStripMainMenu.LostFocus
For Each c In MenuStripMainMenu.Items.OfType(Of ToolStripItem)
c.DisplayStyle = ToolStripItemDisplayStyle.Image
Next
End Sub
虽然这项工作正常,但是一些MenuItems有自己的MenuItems(作为选择树),当用户将鼠标从根MenuStrip移动到选项时,MenuStrip缩小回" icon-only& #34;模式,因此展开的菜单看起来很笨拙。
有没有办法检测MenuStrip何时将其中一个项目扩展到其子项目,这样我可以挂起正确的调整程序?
非常感谢!
修改
这是移植到VB.NET的DonBoitnott's answer,我只在MenuDeactivate
处理程序中添加了一个后续操作,在菜单被停用后立即将条带缩回到只有图像:
Private MenuStripMainMenu_activated As Boolean = False
Private Sub MenuStripMainMenu_MenuActivate(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MenuActivate
MenuStripMainMenu_activated = True
End Sub
Private Sub MenuStripMainMenu_MenuDeactivate(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MenuDeactivate
MenuStripMainMenu_activated = False
MenuStripMainMenu_MouseLeave(sender, e)
End Sub
Private Sub MenuStripMainMenu_MouseEnter(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MouseEnter
For Each c In MenuStripMainMenu.Items.OfType(Of ToolStripItem)
c.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
Next
End Sub
Private Sub MenuStripMainMenu_MouseLeave(sender As Object, e As EventArgs) Handles MenuStripMainMenu.MouseLeave
If Not MenuStripMainMenu_activated Then
For Each c In MenuStripMainMenu.Items.OfType(Of ToolStripItem)
c.DisplayStyle = ToolStripItemDisplayStyle.Image
Next
End If
End Sub
答案 0 :(得分:2)
您目前拥有的活动以及添加MenuActivate
和MenuDeactivate
的组合可以表现为:
public partial class Form1 : Form
{
private Boolean _activated = false;
public Form1()
{
InitializeComponent();
}
private void menuStrip1_MouseEnter(Object sender, EventArgs e)
{
foreach (var c in menuStrip1.Items.OfType<ToolStripItem>())
c.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
}
private void menuStrip1_MouseLeave(Object sender, EventArgs e)
{
if (!_activated)
{
foreach (var c in menuStrip1.Items.OfType<ToolStripItem>())
c.DisplayStyle = ToolStripItemDisplayStyle.Image;
}
}
private void menuStrip1_MenuActivate(Object sender, EventArgs e)
{
_activated = true;
}
private void menuStrip1_MenuDeactivate(Object sender, EventArgs e)
{
_activated = false;
foreach (var c in menuStrip1.Items.OfType<ToolStripItem>())
c.DisplayStyle = ToolStripItemDisplayStyle.Image;
}
}