Hello.

I made a class that overrides the onpaint procedure of a menustrip.

I then made a class that overrides procedures of the ToolStripRenderer

This allowed me to get the look of the menu strip and context menu that I wanted.

However highlighting doesn't take place. How can I make it so when I move my mouse over a ToolStripItem i can render my own highlighting colors.

I need to beable to get the item that is currently being hovered over. Then I need to take that item and fill a rectangle. My issue is getting the item that is being hovered over.


Here is my code. I used MSDN examples to help.

vb.net Code:
  1. Public Class cMenuBar
  2.  
  3.     Public Sub New()
  4.  
  5.         InitializeComponent()
  6.         Dim renderer As New cToolStripRenderer
  7.         Me.Renderer = renderer
  8.  
  9.     End Sub
  10.  
  11.     Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
  12.         MyBase.OnPaintBackground(e)
  13.  
  14.         Dim Rect As New Rectangle(0, 0, Me.Width, Me.Height)
  15.         Dim bgGradient As New LinearGradientBrush(Rect, Color.FromArgb(255, 235, 233, 237), Color.FromArgb(255, 251, 250, 251), LinearGradientMode.Horizontal)
  16.         e.Graphics.FillRectangle(bgGradient, Rect)
  17.  
  18.     End Sub
  19.  
  20. End Class
  21.  
  22.  
  23. Friend Class cToolStripRenderer
  24.     Inherits ToolStripRenderer
  25.  
  26.        'The brush that paints the background of
  27.     'the GridStrip control.
  28.     Private backgroundBrush As Brush = Nothing
  29.  
  30.    
  31.  
  32.     'This method initializes an individual ToolStripButton
  33.     'control. It copies a subimage from the ToolStripRenderer's
  34.     'main image, according to the position and size of
  35.     'the ToolStripButton.
  36.     Protected Overrides Sub InitializeItem(ByVal item As System.Windows.Forms.ToolStripItem)
  37.         MyBase.InitializeItem(item)
  38.         Try
  39.  
  40.             Dim ms As MenuStrip = item.Owner
  41.  
  42.             'The empty cell does not receive a subimage.
  43.             If (TypeOf (item) Is ToolStripButton) Then
  44.                 'Copy the subimage from the appropriate
  45.                 'part of the main image.
  46.                 Dim subImage As Bitmap = bmp.Clone(item.Bounds, Imaging.PixelFormat.Undefined)
  47.  
  48.                 'assign the subimage to the ToolStripButton
  49.                 'control's Image property.
  50.                 item.Image = subImage
  51.             End If
  52.         Catch ex As Exception
  53.  
  54.  
  55.         End Try
  56.  
  57.     End Sub
  58.  
  59.  
  60.     'This method renders the MenuStrip control's background.
  61.     Protected Overrides Sub OnRenderToolStripBackground(ByVal e As System.Windows.Forms.ToolStripRenderEventArgs)
  62.         MyBase.OnRenderToolStripBackground(e)
  63.  
  64.         Dim rect As New Rectangle(2, 0, 28, e.ToolStrip.Size.Height)
  65.         Dim b As Brush = New LinearGradientBrush(rect, Color.FromArgb(255, 235, 233, 237), Color.FromArgb(255, 183, 183, 184), LinearGradientMode.Vertical, True)
  66.  
  67.         Dim rec As New Rectangle(29, 0, 1, e.ToolStrip.Size.Height)
  68.         Dim b1 As Brush = Brushes.Black
  69.  
  70.         Dim rec1 As New Rectangle(0, 0, 2, e.ToolStrip.Size.Height)
  71.         Dim b2 As Brush = New LinearGradientBrush(rec1, Color.FromArgb(255, 235, 234, 238), Color.FromArgb(255, 235, 234, 238), LinearGradientMode.Horizontal, True)
  72.  
  73.         Dim r As New Rectangle(30, 0, e.ToolStrip.Size.Width, e.ToolStrip.Size.Height)
  74.  
  75.         'This late initialization is a workaround. The gradient depends on the bounds of the MenuStrip control. The bounds
  76.         'are dependent on the layout engine, which hasn't fully performed layout by the time the Initialize method runs.
  77.         If Me.backgroundBrush Is Nothing Then
  78.             Me.backgroundBrush = New LinearGradientBrush(r, Color.FromArgb(255, 222, 221, 225), Color.FromArgb(255, 251, 250, 251), LinearGradientMode.Horizontal, True)
  79.         End If
  80.  
  81.         'Paint the MenuStrip control's background
  82.         e.Graphics.FillRectangle(b2, rec1)
  83.         e.Graphics.FillRectangle(b, rect)
  84.         e.Graphics.FillRectangle(b1, rec)
  85.         e.Graphics.FillRectangle(Me.backgroundBrush, r)
  86.  
  87.     End Sub
  88. End Class