Specify custom disabled text color in ToolStripProfessionalRendere

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to specify a custom color for disabled text that is used by a class
that inherits from ToolStripProfessionalRenderer. The code would look
something like this:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventArgs)
If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If
MyBase.OnRenderItemText(e)
End Sub
End Class

The problem is that e.TextColor seems to be ignored if e.Item.Enabled is
false. The only other option that I can think of is to use a method like
Windows.Forms.TextRenderer.DrawText. The problem with that approach is that
then I would need to account for e.TextDirection which can get complex,
especially if the value is ToolStripTextDirection.Inherit.

Any workarounds or ways to simplify drawing the text myself?

Thanks for any help!
Lance
 
Hi Lance,

Based on my understanding, you wanted to create a customized
ToolStripRender inherits from ToolStripProfessionalRenderer to customize
the text display color of each item. However, you find that when the item
is disabled, your setting in ToolStripProfessionalRenderer.OnRenderItemText
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
OnRenderItemText, "MyBase.OnRenderItemText(e)" in your code will call the
ToolStripRenderer.OnRenderItemText method which is implemented by .Net
Framework. Below is the full code of "ToolStripRenderer.OnRenderItemText"
method:

Protected Overridable Sub OnRenderItemText(ByVal e As
ToolStripItemTextRenderEventArgs)
If (Not Me.RendererOverride Is Nothing) Then
Me.RendererOverride.OnRenderItemText(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.Enabled, color1, SystemColors.GrayText)
'<<<this statement will ignore our setting
If (((e.TextDirection <> ToolStripTextDirection.Horizontal)
AndAlso (rectangle1.Width > 0)) AndAlso (rectangle1.Height > 0)) Then
Dim size1 As Size = LayoutUtils.FlipSize(rectangle1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Width,
size1.Height, PixelFormat.Format32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromImage(bitmap1)
graphics2.TextRenderingHint =
TextRenderingHint.AntiAlias
TextRenderer.DrawText(graphics2, text1,
font1, New Rectangle(Point.Empty, size1), color1, flags1)
bitmap1.RotateFlip(IIf((e.TextDirection =
ToolStripTextDirection.Vertical90), RotateFlipType.Rotate90FlipNone,
RotateFlipType.Rotate270FlipNone))
graphics1.DrawImage(bitmap1, rectangle1)
Return
End Using
End Using
End If
TextRenderer.DrawText(graphics1, 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.OnRenderItemText(e)", but simulate
"ToolStripRenderer.OnRenderItemText" code logic in your method, like this:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventArgs)
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.Enabled, color1, SystemColors.GrayText)

TextRenderer.DrawText(graphics1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: I did not include the logic of
ToolStripProfessionalRenderer.OnRenderItemText 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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Lance,

Thanks for your feedback!

To consider the e.TextDirection, I think we may repeat the
ToolStripRenderer.OnRenderItemText() code logic below:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer

Private Shared Function FlipSize(ByVal size As Size) As Size
Dim num1 As Integer = size.Width
size.Width = size.Height
size.Height = num1
Return size
End Function

Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventArgs)

If (TypeOf e.Item Is ToolStripMenuItem AndAlso (e.Item.Selected
OrElse e.Item.Pressed)) Then
e.DefaultTextColor = e.Item.ForeColor
End If

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.Enabled, color1, SystemColors.GrayText)

If (((e.TextDirection <> ToolStripTextDirection.Horizontal)
AndAlso (rectangle1.Width > 0)) AndAlso (rectangle1.Height > 0)) Then
Dim size1 As Size = FlipSize(rectangle1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Width,
size1.Height, PixelFormat.Format32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromImage(bitmap1)
graphics2.TextRenderingHint =
TextRenderingHint.AntiAlias
TextRenderer.DrawText(graphics2, text1,
font1, New Rectangle(Point.Empty, size1), color1, flags1)
bitmap1.RotateFlip(IIf((e.TextDirection =
ToolStripTextDirection.Vertical90), RotateFlipType.Rotate90FlipNone,
RotateFlipType.Rotate270FlipNone))
graphics1.DrawImage(bitmap1, rectangle1)
Return
End Using
End Using
End If

TextRenderer.DrawText(graphics1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: the original LayoutUtils.FlipSize() method is a Framework private
method which we can not use. So I repeat its code as the private method of
MyRenderer. Also, I repeated
ToolStripProfessionalRenderer.OnRenderItemText() method code logic in the
front of our OnRenderItemText() method.

Does this meet your need? If I still misunderstood you, please feel free to
tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke
e.TextFormat = TextFormatFlags.
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke;
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}
 
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke;
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}
 
Back
Top