home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples5 / ch11 / textdemo.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.2 KB  |  157 lines

  1. VERSION 5.00
  2. Begin VB.Form TextDemo 
  3.    Caption         =   "Text Draw Demo"
  4.    ClientHeight    =   4185
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1485
  7.    ClientWidth     =   8190
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   8.25
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    ForeColor       =   &H80000008&
  18.    LinkMode        =   1  'Source
  19.    LinkTopic       =   "Form1"
  20.    PaletteMode     =   1  'UseZOrder
  21.    ScaleHeight     =   4185
  22.    ScaleWidth      =   8190
  23.    Begin VB.PictureBox PicText 
  24.       Height          =   2115
  25.       Left            =   240
  26.       ScaleHeight     =   2085
  27.       ScaleWidth      =   7725
  28.       TabIndex        =   1
  29.       Top             =   1920
  30.       Width           =   7755
  31.    End
  32.    Begin VB.PictureBox PicFrame 
  33.       BeginProperty Font 
  34.          Name            =   "MS Sans Serif"
  35.          Size            =   9.75
  36.          Charset         =   0
  37.          Weight          =   400
  38.          Underline       =   0   'False
  39.          Italic          =   0   'False
  40.          Strikethrough   =   0   'False
  41.       EndProperty
  42.       Height          =   1575
  43.       Left            =   3120
  44.       ScaleHeight     =   1545
  45.       ScaleWidth      =   4785
  46.       TabIndex        =   2
  47.       Top             =   240
  48.       Width           =   4815
  49.    End
  50.    Begin VB.ListBox FontList 
  51.       Height          =   1590
  52.       Left            =   240
  53.       Sorted          =   -1  'True
  54.       TabIndex        =   0
  55.       Top             =   240
  56.       Width           =   2715
  57.    End
  58. Attribute VB_Name = "TextDemo"
  59. Attribute VB_GlobalNameSpace = False
  60. Attribute VB_Creatable = False
  61. Attribute VB_PredeclaredId = True
  62. Attribute VB_Exposed = False
  63. Option Explicit
  64. ' Copyright 
  65.  1997 by Desaware Inc. All Rights Reserved.
  66. ' Redraw the demo pictures in the new font
  67. Private Sub FontList_Click()
  68.     PicText.FontName = FontList.Text
  69.     PicFrame.FontName = FontList.Text
  70.     PicText.Refresh
  71.     PicFrame.Refresh
  72. End Sub
  73. '   Load the font list dialog box with the available fonts
  74. Private Sub Form_Load()
  75.     Dim x%
  76.     Dim a$
  77.     Screen.MousePointer = 11
  78.     For x% = 1 To Screen.FontCount
  79.         a$ = Screen.Fonts(x%)
  80.         If a$ <> "" Then FontList.AddItem a$
  81.     Next x%
  82.     Screen.MousePointer = 0
  83.     FontList.ListIndex = 0
  84. End Sub
  85. '   Use of drawtext to do some powerful text drawing
  86. Private Sub PicFrame_PAINT()
  87.     Dim demo$, demo2$
  88.     Dim rc As RECT
  89.     Dim atab$
  90.     Dim heightused%
  91.     Dim crlf$
  92.     Dim di&
  93.     atab$ = Chr$(9)
  94.     crlf$ = Chr$(13) + Chr$(10)
  95.     demo$ = "This is a line of text that will show how "
  96.     demo$ = demo$ + "automatic word wrapping can take place while drawing text."
  97.     demo$ = demo$ + crlf$ + "Line breaks also work"
  98.     demo2$ = "And" + atab$ + "Tabs" + atab$ + "Work" + atab$ + "Too" + atab$
  99.     ' Get the dimensions of the control
  100.     di = GetClientRect(PicFrame.hWnd, rc)
  101.     heightused% = DrawText(PicFrame.hDC, demo$, -1, rc, DT_WORDBREAK)
  102.     rc.Top = heightused%
  103.     ' Tabs are set 10 characters apart (based on average char width)
  104.     heightused% = DrawText(PicFrame.hDC, demo2$, -1, rc, DT_EXPANDTABS Or DT_TABSTOP Or &HA00)
  105. End Sub
  106. '   Draw a multiple font demo in the picture control
  107. Private Sub PicText_Paint()
  108.     ReDim demo$(4)
  109.     Dim lf As LOGFONT
  110.     #If Win32 Then
  111.         Dim oldfont&, newfont&
  112.         Dim alignorig&
  113.         Dim vpos&
  114.     #Else
  115.         Dim oldfont%, newfont%
  116.         Dim alignorig%
  117.         Dim vpos%
  118.     #End If
  119.     Dim rc As RECT
  120.     Dim todraw%
  121.     Dim di&
  122.     Dim prevpoint As POINTAPI
  123.     demo$(1) = "Watch "
  124.     demo$(2) = "the "
  125.     demo$(3) = "Fonts "
  126.     demo$(4) = "Grow "
  127.     ' Get the dimensions of the control
  128.     di = GetClientRect(PicText.hWnd, rc)
  129.     ' We get the current logical font by selecting in
  130.     ' temporarily a stock font
  131.     oldfont = SelectObject(PicText.hDC, GetStockObject(SYSTEM_FONT))
  132.     di = GetObjectAPI(oldfont, Len(lf), lf)
  133.     ' Restore the original font
  134.     di = SelectObject(PicText.hDC, oldfont)
  135.     ' Reset current position and alignment
  136.     ' Be sure to keep the original alignment
  137.     ' Since we're changing font sizes, align to the baseline
  138.     ' To make life easier, we use the current position
  139.     alignorig = SetTextAlign(PicText.hDC, TA_LEFT Or TA_BASELINE Or TA_UPDATECP)
  140.     ' Draw the text about 3/4 of the way down.
  141.     vpos = rc.Bottom - rc.Bottom / 4
  142.     di = MoveToEx(PicText.hDC, 0, vpos, prevpoint)
  143.     ' Draw the first word
  144.     di = TextOut(PicText.hDC, 0, 0, demo$(1), Len(demo$(1)))
  145.     ' Now start drawing the rest of the words
  146.     For todraw% = 2 To 4
  147.         ' Debug.Print lf.lfHeight
  148.         lf.lfHeight = lf.lfHeight * 2
  149.         newfont = CreateFontIndirect(lf)
  150.         oldfont = SelectObject(PicText.hDC, newfont)
  151.         di = TextOut(PicText.hDC, 0, 0, demo$(todraw%), Len(demo$(todraw)))
  152.         newfont = SelectObject(PicText.hDC, oldfont)
  153.         di = DeleteObject(newfont)
  154.     Next todraw%
  155.     di = SetTextAlign(PicText.hDC, alignorig)
  156. End Sub
  157.