如何在菜单项中设置鼠标离开时的颜色。

时间:2012-09-10 04:42:01

标签: c# winforms menu

WinForm 应用程序中使用菜单工具条。在检查菜单选项时,它会打开子菜单。 当鼠标进入子菜单的边界时,背面颜色变为绿色。现在,当鼠标离开子菜单的边界时,我想将此颜色更改为红色。 有什么建议 ?

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    SolidBrush brush;
    Rectangle r = new Rectangle(this.Bounds.Width - 20, 2, 16, 17);

    // If click on Del(Close Icon)
    if (bOnDel)
    {
        brush = new SolidBrush(Color.LightBlue);
        e.Graphics.FillRectangle(brush, r);
        brush.Color = Color.Blue;
        e.Graphics.DrawRectangle(new Pen(brush, 1), r);
    }
    // If didn't click on Del(Close Icone)
    if (!bOnDel)
    {

        brush = new SolidBrush(Color.FromKnownColor(KnownColor.Transparent));
        e.Graphics.FillRectangle(brush, r);
        brush.Color = Color.FromKnownColor(KnownColor.Transparent);
        e.Graphics.DrawRectangle(new Pen(brush, 1), r);
    }

    //Code for Drawing Cross Lines

    brush = new SolidBrush(Color.Gray);
    Rectangle rCross = new Rectangle(this.Bounds.Width - 15, 8, 6, 6);
    e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Right, rCross.Top), new Point(rCross.Left, rCross.Bottom));
    e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Left, rCross.Top), new Point(rCross.Right, rCross.Bottom));
}

1 个答案:

答案 0 :(得分:1)

使用MouseLeave event ToolStripMenuItem来更改BackColor属性:

private void yourToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
    ((ToolStripMenuItem)sender).BackColor = Color.Red;
}

您可以查看使用MouseMove Event,确保您的矩形在Paint事件之外声明,并使用Rectangle作为区域使Control无效。这是一个基于代码的示例,我在类的开头声明了一个布尔输入和矩形r。您可以在绘画事件中添加任何突出显示更改。这更像是我想要的。

public partial class CustomControl1 : ToolStripMenuItem 
{
    Rectangle r;
    bool entered;

    public CustomControl1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        SolidBrush brush;
        r = new Rectangle(this.Bounds.Width - 20, 2, 16, 17);

        // If MouseEnter Del(Close Icon)
        if (entered)
        {
            brush = new SolidBrush(Color.LightBlue);
            e.Graphics.FillRectangle(brush, r);
            brush.Color = Color.Blue;
            e.Graphics.DrawRectangle(new Pen(brush, 1), r);
        }
        // If Mouse Not Entered Del(Close Icone)
        if (!entered)
        {

            brush = new SolidBrush(Color.FromKnownColor(KnownColor.Transparent));
            e.Graphics.FillRectangle(brush, r);
            brush.Color = Color.FromKnownColor(KnownColor.Transparent);
            e.Graphics.DrawRectangle(new Pen(brush, 1), r);
        }

        //Code for Drawing Cross Lines

        brush = new SolidBrush(Color.Gray);
        Rectangle rCross = new Rectangle(this.Bounds.Width - 15, 8, 6, 6);
        e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Right, rCross.Top), new Point(rCross.Left, rCross.Bottom));
        e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Left, rCross.Top), new Point(rCross.Right, rCross.Bottom));
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (r.Contains(e.X, e.Y) && !entered)
        {
            entered = true;
            Invalidate(r);
        }
        else if (!r.Contains(e.X, e.Y) && entered)
        {
            entered = false;
            Invalidate(r);
        }
    }
}