Category / Section
Adding Shadow effect over Text for Diagram's Label/TextNode?
2 mins read
Adding shadow effect over text
The diagram control doesn’t have a built-in support for rendering the shadow for Diagram’s Syncfusion.Windows.Forms.Diagram.Label’s/ Syncfusion.Windows.Forms.Diagram.TextNode’s text. However we can customize the Diagram’s Label/TextNode class and override the Render() method in order to achieve that behavior.
The below code example shows the customization of the Diagram’s Label/TextNode class,
[C#]
public class CustomTextNode : TextNode { private PointF m_shadowPosition; private Color m_shadowColor; /// <summary> /// Get/Set Shadow position for the text /// </summary> public PointF ShadowPosition { get { return m_shadowPosition; } set { m_shadowPosition = value; } } /// <summary> /// Get/Set the Shadow color for the Text /// </summary> public Color ShadowColor { get { return m_shadowColor; } set { m_shadowColor = value; } } public CustomTextNode(string strText) : base(strText) { } public CustomTextNode(CustomTextNode src) : base(src) { this.m_shadowColor = src.m_shadowColor; this.m_shadowPosition = src.m_shadowPosition; } public CustomTextNode(string strText, RectangleF rectBounds) : base(strText, rectBounds) { } public override object Clone() { return new CustomTextNode(this); } protected override void Render(Graphics gfx) { //Add logic here... } }
[VB]
Public Class CustomTextNode Inherits TextNode Private m_shadowPosition As PointF Private m_shadowColor As Color ''' <summary> ''' Get/Set Shadow position for the text ''' </summary> Public Property ShadowPosition() As PointF Get Return m_shadowPosition End Get Set(ByVal value As PointF) m_shadowPosition = value End Set End Property ''' <summary> ''' Get/Set the Shadow color for the Text ''' </summary> Public Property ShadowColor() As Color Get Return m_shadowColor End Get Set(ByVal value As Color) m_shadowColor = value End Set End Property Public Sub New(ByVal strText As String) MyBase.New(strText) End Sub Public Sub New(ByVal src As CustomTextNode) MyBase.New(src) Me.m_shadowColor = src.m_shadowColor Me.m_shadowPosition = src.m_shadowPosition End Sub Public Sub New(ByVal strText As String, ByVal rectBounds As RectangleF) MyBase.New(strText, rectBounds) End Sub Public Overrides Function Clone() As Object Return New CustomTextNode(Me) End Function Protected Overrides Sub Render(ByVal gfx As Graphics) 'Add logic here... End Sub End Class
The below code example shows the rendering of shadow for Label’s/TextNode’s Text,
[C#]
protected override void Render(Graphics gfx) { SizeF szSize = this.BoundsInfo.GetSize(MeasureUnits.Pixel); RectangleF bounds = new RectangleF(m_shadowPosition, szSize); using (Font font = this.FontStyle.CreateFont()) { using (var brush = new SolidBrush(m_shadowColor)) { StringFormat fmt = GetStringFormat(); gfx.DrawString(this.Text, font, brush, bounds, fmt); } } base.Render(gfx); }
[VB]
Protected Overrides Sub Render(ByVal gfx As Graphics) Dim szSize As SizeF = Me.BoundsInfo.GetSize(MeasureUnits.Pixel) Dim bounds As New RectangleF(m_shadowPosition, szSize) Using font As Font = Me.FontStyle.CreateFont() Using brush = New SolidBrush(m_shadowColor) Dim fmt As StringFormat = GetStringFormat() gfx.DrawString(Me.Text, font, brush, bounds, fmt) End Using End Using MyBase.Render(gfx) End Sub
Sample: