home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch4 / Styles.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-02  |  2.9 KB  |  81 lines

  1. VERSION 5.00
  2. Begin VB.Form frmStyles 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Styles"
  6.    ClientHeight    =   4140
  7.    ClientLeft      =   1200
  8.    ClientTop       =   1440
  9.    ClientWidth     =   6690
  10.    LinkTopic       =   "Form1"
  11.    PaletteMode     =   1  'UseZOrder
  12.    ScaleHeight     =   207
  13.    ScaleMode       =   2  'Point
  14.    ScaleWidth      =   334.5
  15. Attribute VB_Name = "frmStyles"
  16. Attribute VB_GlobalNameSpace = False
  17. Attribute VB_Creatable = False
  18. Attribute VB_PredeclaredId = True
  19. Attribute VB_Exposed = False
  20. Option Explicit
  21. ' Draw a string on the form using randomly chosen
  22. ' ForeColor, size, bold, and italic values. Start
  23. ' the text at Y position min_y and keep it
  24. ' between the margins min_x and max_x.
  25. Private Sub RandomStyles(txt As String, min_size As Integer, max_size As Integer, min_x As Single, max_x As Single, min_y As Single)
  26. Dim length As Integer
  27. Dim pos1 As Integer
  28. Dim pos2 As Integer
  29. Dim new_word As String
  30. Dim clr As Long
  31. Dim y As Integer
  32. Dim font_names As Collection
  33.     ' Erase the form.
  34.     Cls
  35.     CurrentX = min_x
  36.     y = 0
  37.     ' Make the list of font names.
  38.     Set font_names = New Collection
  39.     font_names.Add "Times New Roman"
  40.     font_names.Add "Courier New"
  41.     font_names.Add "Arial"
  42.     font_names.Add "MS Sans Serif"
  43.     ' Break the string into words.
  44.     length = Len(txt)
  45.     pos1 = 1
  46.     Do
  47.         ' Get the next word.
  48.         pos2 = InStr(pos1, txt, " ")
  49.         If pos2 = 0 Then
  50.             new_word = Mid$(txt, pos1)
  51.         Else
  52.             new_word = Mid$(txt, pos1, pos2 - pos1)
  53.         End If
  54.         pos1 = pos2 + 1
  55.         ' Randomly select a ForeColor.
  56.         clr = QBColor(Int(16 * Rnd))
  57.         If clr = BackColor Then clr = vbBlack
  58.         ForeColor = clr
  59.         ' Randomly pick Font properties.
  60.         ' (The Underline and Strikethrough
  61.         ' properties make things too cluttered.)
  62.         Font.Name = font_names(Int(font_names.Count * Rnd + 1))
  63.         Font.Size = Int((max_size - min_size + 1) * Rnd + min_size)
  64.         Font.Bold = (Int(2 * Rnd) = 1)
  65.         Font.Italic = (Int(2 * Rnd) = 1)
  66.         ' If the word won't fit, start a new line.
  67.         If CurrentX + TextWidth(new_word) > max_x Then
  68.             CurrentX = min_x
  69.             y = y + 1.25 * max_size
  70.         End If
  71.         ' Display the text.
  72.         CurrentY = y + max_size - Font.Size
  73.         Print new_word; " ";
  74.     Loop While pos2 > 0
  75. End Sub
  76. ' Call RandomStyles to redraw the text string.
  77. Private Sub Form_Resize()
  78. Const txt = "If you draw some text, modify the Font object, and then draw more text, the two pieces of text will be displayed in different styles. Similarly you can change a form or picture box's ForeColor property to produce text of different colors."
  79.     RandomStyles txt, 10, 20, 0, ScaleWidth, 0
  80. End Sub
  81.