shadowed text

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
What is the most elegant solution we have for shadowed text in VB2005 ? Is it just a duplicate label underneath with zorder offset or is there any more sophisticated shape shadow effects anyone can point me to ?
TIA !
 
You would normally use GDI+. In the Paint event handler of the control you want to draw on you would call DrawString twice, slightly offset from each other and with two different colours. If you wanted to create your own custom control with shadowed text then you'd override the OnPaint method and do the same there, then you could create instances of this control wherever you liked and they would all draw their own shadowed text themselves.
 
Just in case you don't fancy translating that into vb.net. I had a bit of spare time, so i did it for you.....



VB.NET:
Imports System
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
 
Namespace textdropshadow
 
    ''' <summary>
    ''' Summary description for Form1.
    ''' </summary>
    Public Class Form1 : Inherits System.Windows.Forms.Form
 
     ''' <summary>
     ''' Required designer variable.
     ''' </summary>
     Private components As System.ComponentModel.Container = Nothing
 
     Public Sub New()
 
      '
      ' Required for Windows Form Designer support
      '
      InitializeComponent()
 
      Me.SetStyle(ControlStyles.ResizeRedraw,True)
     End Sub
 
     ''' <summary>
     ''' Clean up any resources being used.
     ''' </summary>
     Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
 
      If disposing Then
 
         If Not components Is Nothing Then
 
          components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
     End Sub
 
     #Region "Windows Form Designer generated code"
     ''' <summary>
     ''' Required method for Designer support - do not modify
     ''' the contents of this method with the code editor.
     ''' </summary>
     Private Sub InitializeComponent()
 
      ' 
      ' Form1
      ' 
      Me.AutoScaleBaseSize = New System.Drawing.Size(16, 36)
      Me.BackColor = System.Drawing.Color.White
      Me.ClientSize = New System.Drawing.Size(376, 293)
      Me.Font = New System.Drawing.Font("Tahoma", 21.75F, 
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (CByte(0)))
      Me.Name = "Form1"
      Me.Text = "Form1"
'      Me.Paint += New System.Windows.Forms.PaintEventHandler(Me.Form1_Paint);
 
     End Sub
     #End Region
 
     ''' <summary>
     ''' The main entry point for the application.
     ''' </summary>
     <STAThread> _
     Shared Sub Main()
 
      Application.Run(New Form1())
     End Sub
 
     Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
 
      Dim b As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle,
Color.Blue,Color.AliceBlue,90f)
      e.Graphics.FillRectangle(b,Me.ClientRectangle)
      b.Dispose()
     End Sub
 
     Private Sub Form1_Paint(ByVal sender As Object, 
ByVal e As System.Windows.Forms.PaintEventArgs) 
Handles MyBase.Paint
 
      'Make a small bitmap
      Dim bm As Bitmap = New Bitmap(Me.ClientSize.Width/4,Me.ClientSize.Height/4)
      'Get a graphics object for it
      Dim g As Graphics=Graphics.FromImage(bm)
      ' must use an antialiased rendering hint
      g.TextRenderingHint=TextRenderingHint.AntiAlias
      'this matrix zooms the text out to 1/4 size and offsets it by a little right and down
      Dim mx As Matrix = New Matrix(0.25f,0,0,0.25f,3,3)
      g.Transform=mx
      'The shadow is drawn
      g.DrawString("Text with a dropshadow",Font,New SolidBrush(Color.FromArgb(128, Color.Black)),
 10, 10, StringFormat.GenericTypographic)
      'Don’t need this anymore
      g.Dispose()
      'The destination Graphics uses a high quality mode
      e.Graphics.InterpolationMode=InterpolationMode.HighQualityBicubic
      'and draws antialiased text for accurate fitting
      e.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias
      'The small image is blown up to fill the main client rectangle
      e.Graphics.DrawImage(bm,Me.ClientRectangle,0,0,bm.Width,bm.Height,GraphicsUnit.Pixel)
      'finally, the text is drawn on top
      e.Graphics.DrawString("Text with a dropshadow",Font,Brushes.White,10,10,
StringFormat.GenericTypographic)
      bm.Dispose()
     End Sub
    End Class
End Namespace
 
Last edited:
Back
Top