0

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));
}
4

1 回答 1

1

使用ToolStripMenuItem的MouseLeave 事件来更改BackColor属性:

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

您可以考虑使用MouseMove 事件,确保您的矩形是在 Paint 事件之外声明的,并使用 Rectangle 作为区域使控件无效。这是一个基于您的代码的示例,我在类的开头声明了一个输入的布尔值和矩形 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);
        }
    }
}
于 2012-09-10T05:09:32.737 回答