2

我正在重写该OnRenderButtonBackground方法以使用自定义着色,但我想知道鼠标当前是否位于按钮上方。

我试图获取父控件(工具条)和父窗体并将所有坐标添加在一起,但这是不正确的:

Private Class MyRenderer
    Inherits ToolStripProfessionalRenderer
    Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
        Dim btn = TryCast(e.Item, ToolStripButton)
        If Not btn Is Nothing AndAlso btn.Checked Then

            Dim bounds As New Rectangle(Point.Empty, e.Item.Size)
            Dim ts As ToolStrip = e.Item.GetCurrentParent
            Dim f As Form = CType(e.Item.GetCurrentParent.GetContainerControl, Form)
            Dim btnRect As New Rectangle(f.Location.X + ts.Location.X + e.Item.Bounds.X, f.Location.Y + ts.Location.Y + e.Item.Bounds.Y, e.Item.Bounds.Width, e.Item.Bounds.Height)

            If btnRect.Contains(MousePosition) Then
                'doesn't reach this path...
                e.Graphics.FillRectangle(New SolidBrush(Color.Blue), bounds)
            Else
                e.Graphics.FillRectangle(New SolidBrush(Color.Red), bounds)
            End If
        Else
            MyBase.OnRenderButtonBackground(e)
        End If
    End Sub
End Class

我确定必须有一种更简单的方法来做到这一点?

4

1 回答 1

3

我认为您不需要为此使用MousePosition

试试这个:

If e.Item.Selected Then
  e.Graphics.FillRectangle(Brushes.Blue, bounds)
Else
  e.Graphics.FillRectangle(Brushes.Red, bounds)
End If

注意:您没有丢弃您的画笔。尝试处置它们或使用 SystemBrushes 作为示例。

于 2012-04-27T14:35:21.970 回答