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 に答える