Results 1 to 3 of 3

Thread: ToolStripItem Highlight renderring

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    ToolStripItem Highlight renderring

    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

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: ToolStripItem Highlight renderring

    There are many, many, many, many, (many) more methods you can override in the ToolStripRenderer class (you may also want to try inheriting the ToolStripProfessionalRenderer class, see which you think is best). I can't remember the name but there's one like OnRenderToolStripButton. Or maybe Item. Or Both. I've no idea, but they are there.

    Now, if you just want to change the color, and not the shape, then it's a better idea to use a ProfessionalColorTable. You can assign an instance of a ProfessionalColorTable to a ToolStripProfessionalRenderer, and it will use those colors. You need to create a new class and let it inherit from ProfessionalColorTable. You can then override all properties and have them return the color you want. Assuming you called your color table "clrTable" then you can use just this code to have your toolstrip use the colors:
    Code:
    ToolStrip1.Renderer = New ToolStripProfessionalRenderer(New clrTable))
    Using a ColorTable is MUCH easier to do, but it's also a little limited. You can only change colors, not shapes.


    I also have a few codebank submissions on toolstrip renders if you want to take a look. There's a few that actually create a new Renderer like you are trying (to emulate VS2008 and Vista styles). There's also one that uses just a ColorTable to emulate Office 2007 style. Finally there's one that uses both, but you don't need to access them. Instead, I created a custom ToolStrip control that exposes all colors via properties. You can just change the properties during design-time and view the results right there. No need to code anything. This last one also includes a preset for each of the standard color schemes. These color schemes are usually chosen by the system to match the system color scheme, but using my ToolStrip you can force it to use the green theme even on Vista or on the blue XP theme for example.

    So, to recap:
    If you just need to change colors and not shapes Then
    ... If you want to create your own to learn something Then
    ... ... Take a look at my Office 2007 ToolStrip.
    ... ElseIf you just want to change the colors without thinking about it Then
    ... ... Use my custom ToolStrip control
    ... End If
    ElseIf you also need to change shapes
    ... Take a look at the VS2008 and vista renders, as they show you how you can do it
    End If

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: ToolStripItem Highlight renderring

    Thank you for the advice. Very kind. I wanna learn so i will be looking at your office 2007 one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width