Answered by:
change ToolsStripMenuItem backcolor

Question
-
Hi All
I have a MenuStrip with several ToolStripMenuItem.
One of them (Elimin) has two sub Items (Elimina Riga Attuale) and (Elimina Righe Selezionate)
When I click on a sub Item the ToolStrip MenuItem backcolor change to white and I do no read the item text any more becouse it has a wihite foregraound color
How to avoid the background color changes ? Which event have I to handle ?
I still use this code for other colors
#Region "Aggiungi eventi mouse ai comandi dei menu" Public Sub SetMenuStripTextColor(ByRef ms As MenuStrip) For Each Item As ToolStripMenuItem In ms.Items Item.ForeColor = MenuStripTextForeColor Next AggiungiEventoMouse(ms) End Sub Private Sub AggiungiEventoMouse(ByRef cntr As Control) Dim ms As New MenuStrip ms = CType(cntr, MenuStrip) ms.Renderer = New MyRenderer() For Each Item As ToolStripMenuItem In ms.Items If Item.Enabled = True Then AddHandler Item.MouseEnter, AddressOf MenuItem_MouseEnter AddHandler Item.MouseLeave, AddressOf MenuItem_MouseLeave End If Next ms = Nothing End Sub #End Region #Region "Eventi mouse" Private Sub MenuItem_MouseLeave(sender As Object, e As EventArgs) sender.ForeColor = MenuStripTextForeColor sender.backcolor = Color.Transparent End Sub Private Sub MenuItem_MouseEnter(sender As Object, e As EventArgs) sender.Forecolor = MenuStripTextHighColor End Sub Private Sub Panel_Paint(sender As Object, e As PaintEventArgs) Dim pnl As Panel = sender Using p As New Pen(PannelloBorderColor, PanelloBorderSize) e.Graphics.DrawRectangle(p, 0, 0, pnl.Width, pnl.Height) End Using End Sub #End Region
And a Renderer
Public Class MyRenderer Inherits ToolStripProfessionalRenderer ''' <summary> ''' cambia il colore dei comandi quando passa sopra il mouse ''' ''' </summary> ''' <param name="e"></param> Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs) Dim GC As New GestioneColori If Not e.Item.Selected Then MyBase.OnRenderMenuItemBackground(e) Else If e.Item.Enabled Then Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size) Dim hc As New SolidBrush(GC.MenuStripHighRectangleColor) e.Graphics.FillRectangle(hc, rc) ' se si vuole un rettangolo intorno all'area si puo usare l'istruzione di seguito ' e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1) End If End If End Sub End Class
Friday, June 22, 2018 9:11 AM
Answers
-
HI, I solved the problem changing MyRenderer Class to the following
Public Class MyRenderer Inherits System.Windows.Forms.ToolStripRenderer Protected Overrides Sub OnRenderMenuItemBackground( e As ToolStripItemRenderEventArgs) Dim GC As New GestioneColori ' name colors If Not e.Item.Selected Then MyBase.OnRenderMenuItemBackground(e) Else If e.Item.Enabled Then Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size) Dim hc As New SolidBrush(GC.MenuStripHighRectangleColor) e.Graphics.FillRectangle(hc, rc) End If End If End Sub End Class
overrading a different Tollstriprenderer method :
OnRenderMenuItemBackground
- Marked as answer by Claudio111 Saturday, June 23, 2018 11:03 AM
Saturday, June 23, 2018 11:03 AM
All replies
-
When you determine the MenuStripTextForeColor variable you use in your example, you should check the backcolor and set the forecolor to the opposite. Here is an example. You will have to work it into your particular code somehow.
Dim b2 As New SolidBrush(Color.Black) 'determine font color based on background color If CInt(b1.Color.R) + CInt(b1.Color.G) + CInt(b1.Color.B) > 128 * 3 Then b2 = CType(Brushes.Black, SolidBrush) Else b2 = CType(Brushes.White, SolidBrush) End If
Friday, June 22, 2018 12:01 PM -
Thanks Tommy, but this is not what I wish.
I do no set the backgrung color to white, so I would like to know which event do it.
Friday, June 22, 2018 2:00 PM -
Thanks Tommy, but this is not what I wish.
I do no set the backgrung color to white, so I would like to know which event do it.
Oh, ok. I am not sure. Maybe someone else knows?
Friday, June 22, 2018 2:15 PM -
HI, I solved the problem changing MyRenderer Class to the following
Public Class MyRenderer Inherits System.Windows.Forms.ToolStripRenderer Protected Overrides Sub OnRenderMenuItemBackground( e As ToolStripItemRenderEventArgs) Dim GC As New GestioneColori ' name colors If Not e.Item.Selected Then MyBase.OnRenderMenuItemBackground(e) Else If e.Item.Enabled Then Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size) Dim hc As New SolidBrush(GC.MenuStripHighRectangleColor) e.Graphics.FillRectangle(hc, rc) End If End If End Sub End Class
overrading a different Tollstriprenderer method :
OnRenderMenuItemBackground
- Marked as answer by Claudio111 Saturday, June 23, 2018 11:03 AM
Saturday, June 23, 2018 11:03 AM -
HI, I solved the problem changing MyRenderer Class to the following
Public Class MyRenderer Inherits System.Windows.Forms.ToolStripRenderer Protected Overrides Sub OnRenderMenuItemBackground( e As ToolStripItemRenderEventArgs) Dim GC As New GestioneColori ' name colors If Not e.Item.Selected Then MyBase.OnRenderMenuItemBackground(e) Else If e.Item.Enabled Then Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size) Dim hc As New SolidBrush(GC.MenuStripHighRectangleColor) e.Graphics.FillRectangle(hc, rc) End If End If End Sub End Class
overrading a different Tollstriprenderer method :
OnRenderMenuItemBackground
Claudio,
That's good.
Looks exactly the same to me?
No matters as long as you are working.
:)
Saturday, June 23, 2018 1:58 PM