当folderbrowserdialog打开并选择目录时,为什么不触发MouseLeave事件?

时间:2016-03-23 21:57:28

标签: c# winforms mouseevent custom-controls

我有一个自定义按钮,在MouseEnterMouseLeaveMouseDownMouseUp时有不同的颜色。但是当FolderBrowserDialogMouseClick之后打开并从中选择了一个目录时,尽管鼠标不在按钮位置,但颜色仍保持在MouseEnter颜色上。我无法弄清楚为什么会这样。我试图使用Capture来解决问题,但它对我没有用。也许我错误地使用了它。

我的代码事件属于MyButton类:

//.....

protected override void OnPaint(PaintEventArgs e)
    {
        //.....
        #region Drawing
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        roundedRect = new RoundedRectangle(Width, Height, radius);
        e.Graphics.FillRectangle(Brushes.Transparent, this.ClientRectangle);

        int R1 = (active1.R + inactive1.R) / 2;
        int G1 = (active1.G + inactive1.G) / 2;
        int B1 = (active1.B + inactive1.B) / 2;

        int R2 = (active2.R + inactive2.R) / 2;
        int G2 = (active2.G + inactive2.G) / 2;
        int B2 = (active2.B + inactive2.B) / 2;

        Rectangle rect = new Rectangle(0, 0, Width, Height);

        if (this.Enabled)
        {
            if (state == MouseState.Leave)
                using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, inactive1, inactive2, 90f))
                    e.Graphics.FillPath(inactiveGB, roundedRect.Path);
            else if (state == MouseState.Over)
                using (LinearGradientBrush activeGB = new LinearGradientBrush(rect, active1, active2, 90f))
                    e.Graphics.FillPath(activeGB, roundedRect.Path);
            else if (state == MouseState.Down)
                using (LinearGradientBrush downGB = new LinearGradientBrush(rect, Color.FromArgb(R1, G1, B1), Color.FromArgb(R2, G2, B2), 90f))
                    e.Graphics.FillPath(downGB, roundedRect.Path);
            using (LinearGradientBrush BorderGB = new LinearGradientBrush(rect, inactive1, inactive2, 90f))
                e.Graphics.DrawPath(new Pen(BorderGB), roundedRect.Path);
        }
        else
        {
            Color linear1 = Color.FromArgb(190, 190, 190);
            Color linear2 = Color.FromArgb(210, 210, 210);
            using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, linear1, linear2, 90f))
            {
                e.Graphics.FillPath(inactiveGB, roundedRect.Path);
                e.Graphics.DrawPath(new Pen(inactiveGB), roundedRect.Path);
            }
        }
        #endregion
    //.....
    }
    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
    }
    protected override void OnEnabledChanged(EventArgs e)
    {
        Invalidate();
        base.OnEnabledChanged(e);
    }
    protected override void OnMouseEnter(EventArgs e)
    {
        state = MouseState.Over;
        Invalidate();
        base.OnMouseEnter(e);
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        state = MouseState.Leave;
        Invalidate();
        base.OnMouseLeave(e);
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
        state = MouseState.Down;
        Invalidate();
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        state = MouseState.Up;
        Invalidate();
        base.OnMouseUp(e);
    }
    #endregion
    //.....

1 个答案:

答案 0 :(得分:0)

我已解决了更改MouseUp事件代码的问题。此处不需要MouseState.Up

//code
protected override void OnMouseUp(MouseEventArgs e)
    {
        if(state != MouseState.Leave)
           state = MouseState.Over
        Invalidate();
        base.OnMouseUp(e);
    }
//code