Results 1 to 1 of 1

Thread: VB - Create outlined text using transparent labels

Threaded View

  1. #1
    Fanatic Member daydee's Avatar
    Join Date
    Jun 01
    Location
    Canada
    Posts
    560

    VB - Create outlined text using transparent labels

    VB Code:
    1. '*************************************************************************************
    2. '*                    CREATE OUTLINED TEXT USING TRANSPARENT LABELS
    3. '*
    4. '*                (OutlinedTranspLabels Sub)Created by Andre Aylestock
    5. '*                 Original creation date(2002) updated on march 2003
    6. '*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    7. '*  PURPOSE : To create outlined text of different size, color and outline thickness using transparent labels.
    8. '*  eg: When wanting to display dark text over a dark background or picture.
    9. '*  The outline color and thickness can be changed at will so to provide a better contrast with the background area.
    10. '*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    11. '*  You are free to use this code as as needed but if you improve on it,
    12. '*  please let me know by contacting me by email at: [email]bakerywizard@globetrotter.net[/email]
    13. '*  or post comment/repply at the link below located at Visual Basic CodeBank of VBForums.com :
    14. '*  [url]http://www.vbforums.com/showthread.php?s=&threadid=234071&highlight=outlined+text[/url]
    15. '*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    16. '*  INSTRUCTIONS
    17. '*  - Paste this entire code inside a module.
    18. '*  - On a form, place 1 Label and give it a distinct name such as: Outlbl and make it Index 0
    19. '*  - In this case, Outlbl(0) will be your center label. You can also set it up as needed with any of these 5 properties at design time
    20. '*    or prior to calling this routine: FontName, FontBold , FontItalic, FontStrikethru or FontUnderline.
    21. '*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    22. '*  USAGE :
    23. '*  Call the routine to: create or hide outlined text, change the outline color, thickness or hide all text.
    24. '*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    25. '*  EXAMPLES:
    26. '*  Call OutlinedTranspLabels(Outlbl, InnerAndOutLine, vbCyan, vbBlack, Thick, 22, "This is outlined text with a thick border at center of Form!", Me) 'To get a thick outline around text.
    27. '*  Call OutlinedTranspLabels(Outlbl, InnerAndOutLine, vbCyan, vbBlack, Light, 22, "This is outlined text with a light border!") 'To get a thin outline around text.
    28. '*  Call OutlinedTranspLabels(Outlbl, InnerOnly, vbCyan, , , 22, "This is normal text !") 'to get normal text or remove outlined around pre-existing text.
    29. '*  Call OutlinedTranspLabels(Outlbl, None) 'To remove all text inside your labels.
    30. '*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    31. '*  NOTES:
    32. '*  You can use this OutlinedTranspLabels sub as is on forms having scale modes of Twip or Pixel only.
    33. '*  The sub here will create borderless labels, so to have borders showing you need to change Outlbl(0).BorderStyle = 1 within the sub.
    34. '*  ALSO: If you place your label inside a PictureBox (to be used as the container), make sure to set the PictureBox's scale mode the same as the form it is on.
    35. '*  eg: If your form's scalemode is set to twip, set the PictureBox that your label belongs to twip as well.
    36. '*  Some fonts will probably also come through better then others so experiment with them and have fun :-)
    37. '*
    38. '*************************************************************************************
    39. Option Explicit
    40. Enum TextVisible
    41.      InnerAndOutLine
    42.      InnerOnly
    43.      None
    44. End Enum
    45.  
    46. Enum OutlineThickness
    47.      Thick = 10
    48.      Medium = 0
    49.      Light = -10
    50. End Enum
    51.  
    52. Public Sub OutlinedTranspLabels(ByVal labl As Object, Display As TextVisible, Optional TxtColr As ColorConstants, _
    53. Optional OutlnColr As ColorConstants, Optional OutlnWeight As OutlineThickness, Optional FntSize As Integer, _
    54. Optional strCaption As String, Optional CenterToFormOrPictureBox As Object)
    55.  
    56. Dim lblLeft As Long, lblTop As Long
    57. Dim I As Integer
    58. Dim TwipstoPixel As Double
    59.  
    60. 'first we need to hide outline labels (if loaded) to reduce
    61. 'flickering when positioning them later.
    62. If labl.Count > 1 Then
    63.     For I = 1 To 4
    64.       labl(I).Visible = False
    65.     Next I
    66.  End If
    67.  
    68. 'if no text is required then let's erase caption of our main label and exit the sub here.
    69. If Display = None Then
    70. labl(0).Caption = vbNullString
    71. Exit Sub
    72. End If
    73.  
    74. 'text will be required passed this point on so let's prepare our main label
    75. With labl(0)
    76.     .BackStyle = vbTransparent
    77.     .Caption = strCaption
    78.     .ForeColor = TxtColr
    79.      If FntSize = 0 Then
    80.      FntSize = .FontSize
    81.      Else
    82.     .FontSize = FntSize
    83.      End If
    84.     .AutoSize = True
    85.     .BorderStyle = 0
    86.    'optional centering alignment to a Form or PictureBox
    87.    If Not CenterToFormOrPictureBox Is Nothing Then
    88.     .Left = (CenterToFormOrPictureBox.ScaleWidth - .Width) / 2
    89.    End If
    90. End With
    91.  
    92. 'this determines whether the text will be outlined or not.
    93. If Display = InnerOnly Then
    94.     labl(0).Visible = True
    95.     Exit Sub
    96. ElseIf Display = InnerAndOutLine Then
    97.  labl(0).Visible = True
    98.   If labl.Count = 1 Then
    99.     For I = 1 To 4
    100.      Load labl(I)
    101.     Next I
    102.    End If
    103.     For I = 1 To 4
    104.      With labl(I)
    105.      .FontSize = FntSize
    106.      .ForeColor = OutlnColr
    107.      .Caption = strCaption
    108.      .ZOrder (0)
    109.     End With
    110.    Next I
    111. End If
    112. 'past this point means our text needs to be outlined, so...
    113. 'we'll assign these 2 variables the values needed
    114. 'for positioning our outline labels below.
    115. lblLeft = labl(0).Left
    116. lblTop = labl(0).Top
    117.  
    118. 'we also need to determine the form's scalemode in
    119. 'order to positioned our outline labels correctly.
    120. 'THIS WILL ONLY WORK WITH FORMS HAVING SCALEMODE SET AT PIXEL OR TWIP!
    121. If labl(0).Parent.ScaleMode = 3 Then
    122. TwipstoPixel = Screen.TwipsPerPixelX
    123. ElseIf labl(0).Parent.ScaleMode = 1 Then
    124. TwipstoPixel = 1
    125. End If
    126.  
    127. 'this sets the outlined weight(thickness)and adjusts it according to the fontsize used.
    128. OutlnWeight = FntSize + OutlnWeight
    129.  
    130. 'now let's position our outline labels
    131. labl(1).Move lblLeft - OutlnWeight / TwipstoPixel, lblTop - OutlnWeight / TwipstoPixel
    132. labl(2).Move lblLeft + OutlnWeight / TwipstoPixel, lblTop - OutlnWeight / TwipstoPixel
    133. labl(3).Move lblLeft - OutlnWeight / TwipstoPixel, lblTop + OutlnWeight / TwipstoPixel
    134. labl(4).Move lblLeft + OutlnWeight / TwipstoPixel, lblTop + OutlnWeight / TwipstoPixel
    135.  
    136. 'let's finally display our outline labels since
    137. 'they have now been positioned.
    138. 'Note: we must unhide them only after they have been positioned
    139. 'in order to avoid any flickering from the positioning action.
    140. For I = 1 To 4
    141.   With labl(I)
    142.      .Visible = True
    143.   End With
    144. Next I
    145.  
    146. 'makes our main(center) label top most.
    147. labl(0).ZOrder (0)
    148.  
    149. End Sub
    Last edited by daydee; Mar 20th, 2003 at 02:58 AM.
    Give your music collection a whole new life with PartyTime Jukebox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •