-1

我正在制作一个基于 ToolStripButton 控件的 CustomControl,我想知道鼠标何时悬停在按钮上以不同方式绘制它。这是我的代码的快速视图:

    private bool m_IsHover = false;        

    ...

    protected override void OnMouseEnter(EventArgs e)
    {
        m_IsHover = true;
        Debug.WriteLine("Mouse IN");
        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        m_IsHover = false;
        Debug.WriteLine("Mouse OUT");
        base.OnMouseLeave(e);
    }

    ...

    protected override void OnPaint(PaintEventArgs e)
    {
        // Define rectangle used to draw
        Rectangle borderRec = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

        if (m_IsHover)
        {
            // Draw border
            e.Graphics.DrawRectangle(m_BorderPen, borderRec);

            ...
        }
        else
        {
            // Default draw
            base.OnPaint(e);
        }
    }

我的问题是我在调试面板中清楚地看到 Mouse IN 和 Mouse OUT 是正确的,所以应该正确设置变量,但是在 OnPaint 事件中,我从不输入 m_IsHover 条件 ...

我真的不明白问题是什么,看起来很容易......

4

1 回答 1

1

ToolStripItem.Select() 方法在 MouseEnter 上运行。调用 this.Invalidate() 强制重绘。

于 2010-08-16T14:23:47.617 回答