Hi Lance,
Based on my understanding, you wanted to create a customized
ToolStripRender inherits from ToolStripProfes sionalRenderer to customize
the text display color of each item. However, you find that when the item
is disabled, your setting in ToolStripProfes sionalRenderer. OnRenderItemTex t
method will be ignored.
Yes, I have written a little sample application regarding your scenario and
I can reproduce this behavior.
Further research shows that after invoking your override version of
OnRenderItemTex t, "MyBase.OnRende rItemText(e)" in your code will call the
ToolStripRender er.OnRenderItem Text method which is implemented by .Net
Framework. Below is the full code of "ToolStripRende rer.OnRenderIte mText"
method:
Protected Overridable Sub OnRenderItemTex t(ByVal e As
ToolStripItemTe xtRenderEventAr gs)
If (Not Me.RendererOver ride Is Nothing) Then
Me.RendererOver ride.OnRenderIt emText(e)
Else
Dim item1 As ToolStripItem = e.Item
Dim graphics1 As Graphics = e.Graphics
Dim color1 As Color = e.TextColor
Dim font1 As Font = e.TextFont
Dim text1 As String = e.Text
Dim rectangle1 As Rectangle = e.TextRectangle
Dim flags1 As TextFormatFlags = e.TextFormat
color1 = IIf(item1.Enabl ed, color1, SystemColors.Gr ayText)
'<<<this statement will ignore our setting
If (((e.TextDirect ion <ToolStripTextD irection.Horizo ntal)
AndAlso (rectangle1.Wid th 0)) AndAlso (rectangle1.Hei ght 0)) Then
Dim size1 As Size = LayoutUtils.Fli pSize(rectangle 1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Wi dth,
size1.Height, PixelFormat.For mat32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromIm age(bitmap1)
graphics2.TextR enderingHint =
TextRenderingHi nt.AntiAlias
TextRenderer.Dr awText(graphics 2, text1,
font1, New Rectangle(Point .Empty, size1), color1, flags1)
bitmap1.RotateF lip(IIf((e.Text Direction =
ToolStripTextDi rection.Vertica l90), RotateFlipType. Rotate90FlipNon e,
RotateFlipType. Rotate270FlipNo ne))
graphics1.DrawI mage(bitmap1, rectangle1)
Return
End Using
End Using
End If
TextRenderer.Dr awText(graphics 1, text1, font1, rectangle1,
color1, flags1)
End If
End Sub
Please see the line I marked with "<<<", this code will ignore our color
setting when item is disabled. This means .Net Winform will not use our
provided text color setting if the item is disabled, so that it can keep a
consistent appearance. This behavior is by design.
If you really want to workaround this design behavior, you should not call
the "MyBase.OnRende rItemText(e)", but simulate
"ToolStripRende rer.OnRenderIte mText" code logic in your method, like this:
Public Class MyRenderer
Inherits Windows.Forms.T oolStripProfess ionalRenderer
Protected Overrides Sub OnRenderItemTex t(ByVal e As
System.Windows. Forms.ToolStrip ItemTextRenderE ventArgs)
If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If
Dim item1 As ToolStripItem = e.Item
Dim graphics1 As Graphics = e.Graphics
Dim color1 As Color = e.TextColor
Dim font1 As Font = e.TextFont
Dim text1 As String = e.Text
Dim rectangle1 As Rectangle = e.TextRectangle
Dim flags1 As TextFormatFlags = e.TextFormat
'color1 = IIf(item1.Enabl ed, color1, SystemColors.Gr ayText)
TextRenderer.Dr awText(graphics 1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class
Note: I did not include the logic of
ToolStripProfes sionalRenderer. OnRenderItemTex t method logic in this code
snippet, you may consider add its code in either.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.