按键后,按下的属性保持不变

时间:2014-09-19 21:47:01

标签: c# winforms menuitem menubar menustrip

我正在为MenuStrip控件创建自定义渲染器。在渲染器类中,我正在检查特定菜单项(menuStrip.Item.Pressed)的按下属性,以检查用户是否单击了menuitem,以便我可以相应地更改我的渲染样式。这可以正常工作,直到您尝试使用键盘导航菜单。执行此操作后,对于使用键盘导航到的所有项目,按下的属性保持为false。键盘没有触摸的那些在用鼠标点击时仍然可以工作。我知道我可以使用单击的事件处理程序创建一个解决方法并设置bool值,但为什么我必须在看起来这个按下的属性是专门为此目的而设计的。这是.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));
            }
        }
    }
}

0 个答案:

没有答案