我正在重写该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
我确定必须有一种更简单的方法来做到这一点?