カスタムカラーを使用するためにメソッドをオーバーライドしています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
これを行うもっと簡単な方法があるに違いないと思いますか?