简体   繁体   English

如何在点击菜单条项目上突出显示活动菜单项?

[英]How can we highlight active menu-item on clicking menu strip item?

I am working on a desktop application in C# WinForms. 我正在使用C#WinForms的桌面应用程序。 I have used menustrip to navigate between different panels. 我用menustrip在不同的面板之间导航。 The problem which I am facing is I cannot highlight the active color of the menustrip icon. 我面临的问题是我无法突出menustrip图标的活动颜色。 A pictorial description will explain better what I want to achive. 图画描述将更好地解释我想要实现的目标。

This is my menu strip 这是我的菜单条

在此输入图像描述

and on click MenuStripItem I want to achieve this 然后单击MenuStripItem我想实现这一点

在此输入图像描述

In short I want to the menu strip item to stay highlighted when I press Click on it just like Search and Edit in the picture and afterwards if I click on New Customers then it must be highlighted as Search & Edit 总之我要在菜单条项留高亮当我按点击它,就像在图片搜索和编辑 ,之后如果我点击新客户则必须强调的是搜索和编辑

You can use ToolStrip instead and set items Checked property to true. 您可以使用ToolStrip ,并将Checked属性设置为true。 To do so, you can handle ItemClicked event of ToolStrip and check items this way: 为此,您可以处理ToolStrip ItemClicked事件并以这种方式检查项目:

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    foreach (ToolStripButton item in ((ToolStrip)sender).Items)
    {
        if (item != e.ClickedItem)
            item.Checked = false;
        else
            item.Checked = true;
    }
}

This way it shows a border around checked item. 这样它就会显示检查项目周围的边框。 If for any reason you are not satisfied with appearance, you can simply customize the appearance of checked item by creating a custom renderer and assigning it as renderer of the ToolStrip this way: 如果由于任何原因您对外观不满意,您可以通过创建自定义渲染器并以此方式将其指定为ToolStrip渲染器来自定义选中项目的外观:

public class MyRenderer : ToolStripProfessionalRenderer
{
    public MyRenderer() : base(new MyColorTable())
    {
    }
}

public class MyColorTable : ProfessionalColorTable
{
    public override Color ButtonCheckedGradientBegin
    {
        get { return ButtonPressedGradientBegin; }
    }
    public override Color ButtonCheckedGradientEnd
    {
        get { return ButtonPressedGradientEnd; }
    }
    public override Color ButtonCheckedGradientMiddle
    {
        get { return ButtonPressedGradientMiddle; }
    }
}

And assign the renderer in Load event of in constructor of your form after initialize components this way: 并以这种方式初始化组件后,在表单的构造函数中的Load事件中指定渲染器:

toolStrip1.Renderer = new MyRenderer();

This way, it shows the checked item as highlighted. 这样,它会将选中的项目显示为突出显示。

The selected item can be changed on Paint (not sure if there is more appropriate event): 可以在Paint上更改所选项目(不确定是否有更合适的事件):

public Form1()
{
    InitializeComponent();

    ToolStripItem activeToolStripItem = null;
    menuStrip1.Paint += (o, e) => activeToolStripItem?.Select();
    menuStrip1.ItemClicked += (o, e) => activeToolStripItem = e.ClickedItem;
}

Set the background colour of the clicked item on the MenuStrip as follows: 在MenuStrip上设置所单击项目的背景颜色,如下所示:

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    foreach (ToolStripMenuItem item in ((ToolStrip)sender).Items)
    {
        if (item != e.ClickedItem)
            item.BackColor = Color.White;
        else
            item.BackColor = Color.Cyan;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM