Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按键后Pressed属性保持为false_C#_Winforms_Menuitem_Menubar_Menustrip - Fatal编程技术网

C# 按键后Pressed属性保持为false

C# 按键后Pressed属性保持为false,c#,winforms,menuitem,menubar,menustrip,C#,Winforms,Menuitem,Menubar,Menustrip,我正在为MenuStrip控件创建自定义渲染器。在renderer类中,我正在检查特定菜单项(menuStrip.item.pressed)的pressed属性,以检查用户是否单击了该菜单项,以便我可以相应地更改渲染样式。在您尝试使用键盘浏览菜单之前,这一切都很正常。执行此操作后,对于使用键盘导航到的所有项目,pressed属性保持为false。当用鼠标点击时,键盘没有碰到的那些仍然有效。我知道我可能会创建一个使用单击事件处理程序并设置bool值的变通方法,但是当这个pressed属性似乎是专门

我正在为MenuStrip控件创建自定义渲染器。在renderer类中,我正在检查特定菜单项(menuStrip.item.pressed)的pressed属性,以检查用户是否单击了该菜单项,以便我可以相应地更改渲染样式。在您尝试使用键盘浏览菜单之前,这一切都很正常。执行此操作后,对于使用键盘导航到的所有项目,pressed属性保持为false。当用鼠标点击时,键盘没有碰到的那些仍然有效。我知道我可能会创建一个使用单击事件处理程序并设置bool值的变通方法,但是当这个pressed属性似乎是专门为此目的而设计的时候,为什么我必须这样做呢。这是.NET中的错误还是我做错了什么

private class myRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs myMenu)
    {
        if (!myMenu.Item.Selected)
            base.OnRenderMenuItemBackground(myMenu);
        if (myMenu.Item.Pressed)
        {
            //If it is a base menu and the menu is open, then dont't draw it
            if (myMenu.Item.OwnerItem == null)
                base.OnRenderMenuItemBackground(myMenu);
            else
            {
                //When click on sub menuitem
                LinearGradientBrush brush = new LinearGradientBrush(Point.Empty, new Point(0, myMenu.Item.Height), Color.FromArgb(190, 230, 245), Color.White);
                Rectangle menuRectangle = new Rectangle(Point.Empty, myMenu.Item.Size);
                menuRectangle.X += 2;
                menuRectangle.Width -= 2;
                //Fill Color
                myMenu.Graphics.FillRectangle(brush, menuRectangle);
                //Border Color
                Pen pen = new Pen(Brushes.AliceBlue, 1);
                pen.Color = Color.FromArgb(125, 162, 206);
                myMenu.Graphics.DrawRectangle(pen, 2, 0, menuRectangle.Width - 2, menuRectangle.Height - 1);
                myMenu.Graphics.DrawLine(Pens.White, new Point(4, menuRectangle.Height - 2), new Point(menuRectangle.Width - 1, menuRectangle.Height - 2));
            }
        }
        else if (myMenu.Item.Selected)
        {
            if (myMenu.Item.OwnerItem == null)
            {
                //When hover over main menuitem
                LinearGradientBrush brush = new LinearGradientBrush(Point.Empty, new Point(0, myMenu.Item.Height), Color.White, Color.FromArgb(190, 230, 245));
                Rectangle menuRectangle = new Rectangle(Point.Empty, myMenu.Item.Size);
                //Fill Color
                myMenu.Graphics.FillRectangle(brush, menuRectangle);
                //Border Color
                Pen pen = new Pen(Brushes.AliceBlue, 1);
                pen.Color = Color.FromArgb(125, 162, 206);
                myMenu.Graphics.DrawRectangle(pen, 0, 0, menuRectangle.Width - 1, menuRectangle.Height - 1);
                myMenu.Graphics.DrawLine(Pens.White, new Point(1, menuRectangle.Height - 2), new Point(menuRectangle.Width - 2, menuRectangle.Height - 2));
            }
            else
            {
                //When hover over sub menuitem
                LinearGradientBrush brush = new LinearGradientBrush(Point.Empty, new Point(0, myMenu.Item.Height), Color.White, Color.FromArgb(190, 230, 245));
                Rectangle menuRectangle = new Rectangle(Point.Empty, myMenu.Item.Size);
                menuRectangle.X += 2;
                menuRectangle.Width -= 2;
                //Fill Color
                myMenu.Graphics.FillRectangle(brush, menuRectangle);
                //Border Color
                Pen pen = new Pen(Brushes.AliceBlue, 1);
                pen.Color = Color.FromArgb(125, 162, 206);
                myMenu.Graphics.DrawRectangle(pen, 2, 0, menuRectangle.Width - 2, menuRectangle.Height - 1);
                myMenu.Graphics.DrawLine(Pens.White, new Point(4, menuRectangle.Height - 2), new Point(menuRectangle.Width - 1, menuRectangle.Height - 2));
            }
        }
    }
}

在没有看到一些代码的情况下调试有点困难。没有复制,.NET3.5和4.5.1。没想到,渲染器当然不会弄乱菜单项的状态。只是测试了3.5。仍然发生。