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 / articles / vbdev / source / dtxt1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-02-22  |  10.0 KB  |  278 lines

  1. VERSION 4.00
  2. Begin VB.Form frmDrawText 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "Draw Text Example"
  6.    ClientHeight    =   4305
  7.    ClientLeft      =   585
  8.    ClientTop       =   1770
  9.    ClientWidth     =   7005
  10.    BeginProperty Font 
  11.       name            =   "Arial"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   12
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   4995
  21.    Left            =   525
  22.    LinkTopic       =   "Form1"
  23.    ScaleHeight     =   287
  24.    ScaleMode       =   3  'Pixel
  25.    ScaleWidth      =   467
  26.    Top             =   1140
  27.    Width           =   7125
  28.    Begin VB.CommandButton cmdJustify 
  29.       Appearance      =   0  'Flat
  30.       BackColor       =   &H80000005&
  31.       Caption         =   "Justify"
  32.       BeginProperty Font 
  33.          name            =   "MS Sans Serif"
  34.          charset         =   1
  35.          weight          =   700
  36.          size            =   8.25
  37.          underline       =   0   'False
  38.          italic          =   0   'False
  39.          strikethrough   =   0   'False
  40.       EndProperty
  41.       Height          =   435
  42.       Left            =   5640
  43.       TabIndex        =   6
  44.       Top             =   3720
  45.       Width           =   1215
  46.    End
  47.    Begin VB.CommandButton cmdTest 
  48.       Appearance      =   0  'Flat
  49.       BackColor       =   &H80000005&
  50.       Caption         =   "Adjust Size"
  51.       BeginProperty Font 
  52.          name            =   "MS Sans Serif"
  53.          charset         =   1
  54.          weight          =   700
  55.          size            =   8.25
  56.          underline       =   0   'False
  57.          italic          =   0   'False
  58.          strikethrough   =   0   'False
  59.       EndProperty
  60.       Height          =   495
  61.       Index           =   2
  62.       Left            =   5640
  63.       TabIndex        =   5
  64.       Top             =   1320
  65.       Width           =   1215
  66.    End
  67.    Begin VB.CommandButton cmdTest 
  68.       Appearance      =   0  'Flat
  69.       BackColor       =   &H80000005&
  70.       Caption         =   "Expand Tabs"
  71.       BeginProperty Font 
  72.          name            =   "MS Sans Serif"
  73.          charset         =   1
  74.          weight          =   700
  75.          size            =   8.25
  76.          underline       =   0   'False
  77.          italic          =   0   'False
  78.          strikethrough   =   0   'False
  79.       EndProperty
  80.       Height          =   495
  81.       Index           =   5
  82.       Left            =   5640
  83.       TabIndex        =   4
  84.       Top             =   3120
  85.       Width           =   1215
  86.    End
  87.    Begin VB.CommandButton cmdTest 
  88.       Appearance      =   0  'Flat
  89.       BackColor       =   &H80000005&
  90.       Caption         =   "Right Align"
  91.       BeginProperty Font 
  92.          name            =   "MS Sans Serif"
  93.          charset         =   1
  94.          weight          =   700
  95.          size            =   8.25
  96.          underline       =   0   'False
  97.          italic          =   0   'False
  98.          strikethrough   =   0   'False
  99.       EndProperty
  100.       Height          =   495
  101.       Index           =   4
  102.       Left            =   5640
  103.       TabIndex        =   3
  104.       Top             =   2520
  105.       Width           =   1215
  106.    End
  107.    Begin VB.CommandButton cmdTest 
  108.       Appearance      =   0  'Flat
  109.       BackColor       =   &H80000005&
  110.       Caption         =   "No Clipping"
  111.       BeginProperty Font 
  112.          name            =   "MS Sans Serif"
  113.          charset         =   1
  114.          weight          =   700
  115.          size            =   8.25
  116.          underline       =   0   'False
  117.          italic          =   0   'False
  118.          strikethrough   =   0   'False
  119.       EndProperty
  120.       Height          =   495
  121.       Index           =   3
  122.       Left            =   5640
  123.       TabIndex        =   2
  124.       Top             =   1920
  125.       Width           =   1215
  126.    End
  127.    Begin VB.CommandButton cmdTest 
  128.       Appearance      =   0  'Flat
  129.       BackColor       =   &H80000005&
  130.       Caption         =   "MultiLine"
  131.       BeginProperty Font 
  132.          name            =   "MS Sans Serif"
  133.          charset         =   1
  134.          weight          =   700
  135.          size            =   8.25
  136.          underline       =   0   'False
  137.          italic          =   0   'False
  138.          strikethrough   =   0   'False
  139.       EndProperty
  140.       Height          =   495
  141.       Index           =   1
  142.       Left            =   5640
  143.       TabIndex        =   1
  144.       Top             =   720
  145.       Width           =   1215
  146.    End
  147.    Begin VB.CommandButton cmdTest 
  148.       Appearance      =   0  'Flat
  149.       BackColor       =   &H80000005&
  150.       Caption         =   "Simple"
  151.       BeginProperty Font 
  152.          name            =   "MS Sans Serif"
  153.          charset         =   1
  154.          weight          =   700
  155.          size            =   8.25
  156.          underline       =   0   'False
  157.          italic          =   0   'False
  158.          strikethrough   =   0   'False
  159.       EndProperty
  160.       Height          =   495
  161.       Index           =   0
  162.       Left            =   5640
  163.       TabIndex        =   0
  164.       Top             =   120
  165.       Width           =   1215
  166.    End
  167.    Begin VB.Menu mnuClear 
  168.       Caption         =   "Clear"
  169.    End
  170. Attribute VB_Name = "frmDrawText"
  171. Attribute VB_Creatable = False
  172. Attribute VB_Exposed = False
  173. Option Explicit
  174. Const Sample1$ = "Large blocks of text can be easily displayed anywhere in a window or on the printed page using the powerful Windows DrawText function"
  175. Dim Sample2$
  176. Dim OutputRect As RECT
  177. Private Sub cmdJustify_Click()
  178.     Dim LineWidth%
  179.     Dim CurrentLineWidth%
  180.     Dim StartOfCurrentLine%
  181.     Dim CurrentPosition%
  182.     Dim NextPosition%
  183.     Dim BreakCharCount%
  184.     Dim CurrentYLocation%
  185.     Dim di%
  186.     Dim OutputString$
  187.     ' Determine the maximum length
  188.     LineWidth% = OutputRect.right - OutputRect.left
  189.     StartOfCurrentLine% = 1
  190.     CurrentPosition% = 1
  191.     BreakCharCount% = 0
  192.     CurrentYLocation% = OutputRect.top
  193.     Do
  194.         ' Find the next space
  195.         NextPosition% = InStr(CurrentPosition%, Sample1$, " ")
  196.         ' We're done with the loop
  197.         If NextPosition% <= CurrentPosition% Then
  198.             CurrentLineWidth = TextWidth(Mid$(Sample1$, StartOfCurrentLine%))
  199.         Else
  200.             CurrentLineWidth = TextWidth(Mid$(Sample1$, StartOfCurrentLine%, NextPosition% - StartOfCurrentLine%))
  201.         End If
  202.         
  203.         ' Does the current line fit?
  204.         If CurrentLineWidth < LineWidth Then
  205.             ' This word fit into the line
  206.             ' Add to the count of break characters
  207.             BreakCharCount% = BreakCharCount% + 1
  208.             ' and set the new current position
  209.             If NextPosition > 0 Then
  210.                 CurrentPosition% = NextPosition% + 1
  211.             Else
  212.                 ' The final line fits on one line -
  213.                 ' print it without justification
  214.                 Exit Do
  215.             End If
  216.         Else
  217.             ' The new word does not fit - print the line
  218.             OutputString$ = Mid$(Sample1$, StartOfCurrentLine%, CurrentPosition% - StartOfCurrentLine% - 1)
  219.             
  220.             ' Set the new current position
  221.             StartOfCurrentLine% = CurrentPosition%
  222.             ' If there is at least one break character, set the correct count
  223.             If BreakCharCount% > 1 Then BreakCharCount% = BreakCharCount% - 1
  224.             ' Set the text justification
  225.             di% = SetTextJustification(hDC, LineWidth - TextWidth(OutputString$), BreakCharCount%)
  226.         
  227.             ' And display the line
  228.             di% = TextOut(hDC, OutputRect.left, CurrentYLocation%, OutputString$, Len(OutputString$))
  229.             CurrentYLocation% = CurrentYLocation% + TextHeight(OutputString$)
  230.             BreakCharCount% = 0
  231.             ' Clear the text justification value
  232.             di% = SetTextJustification(hDC, 0, BreakCharCount%)
  233.         End If
  234.     Loop While CurrentPosition% < Len(Sample1$)
  235.     If CurrentPosition% < Len(Sample1$) Then
  236.         ' Print the rest of the last line - no justification
  237.         OutputString$ = Mid$(Sample1$, StartOfCurrentLine%)
  238.         di% = TextOut(hDC, OutputRect.left, CurrentYLocation%, OutputString$, Len(OutputString$))
  239.     End If
  240. End Sub
  241. Private Sub cmdTest_Click(Index As Integer)
  242.     Dim di%
  243.     Select Case Index
  244.         Case 0
  245.             di% = DrawText(hDC, Sample1$, Len(Sample1$), OutputRect, DT_LEFT)
  246.         Case 1
  247.             di% = DrawText(hDC, Sample1$, Len(Sample1$), OutputRect, DT_LEFT Or DT_WORDBREAK)
  248.         Case 2
  249.             di% = DrawText(hDC, Sample1$, Len(Sample1$), OutputRect, DT_LEFT Or DT_CALCRECT Or DT_WORDBREAK)
  250.             Refresh
  251.             di% = DrawText(hDC, Sample1$, Len(Sample1$), OutputRect, DT_LEFT Or DT_WORDBREAK)
  252.         Case 3
  253.             di% = DrawText(hDC, Sample1$, Len(Sample1$), OutputRect, DT_NOCLIP)
  254.         Case 4
  255.             di% = DrawText(hDC, Sample2$, Len(Sample2$), OutputRect, DT_RIGHT)
  256.         Case 5
  257.             di% = DrawText(hDC, Sample2$, Len(Sample2$), OutputRect, DT_EXPANDTABS)
  258.     End Select
  259. End Sub
  260. Private Sub Form_Load()
  261.     ' We start with the upper left corner of the page
  262.     OutputRect.right = ScaleWidth \ 2
  263.     OutputRect.bottom = ScaleHeight \ 2
  264.     ' And shrink it by 1/20 the width of the page to give
  265.     ' it an extra margin
  266.     InflateRect OutputRect, -CInt(ScaleWidth \ 20), -CInt(ScaleHeight \ 20)
  267.     Sample2$ = "Line1" & Chr$(9) & "is 1" & Chr$(13) & Chr$(10)
  268.     Sample2$ = Sample2$ & "Line2" & Chr$(9) & "is 2" & Chr$(13) & Chr$(10)
  269.     Sample2$ = Sample2$ & "Line3" & Chr$(9) & "is 3" & Chr$(13) & Chr$(10)
  270.     Sample2$ = Sample2$ & "Line4" & Chr$(9) & "is 4" & Chr$(13) & Chr$(10)
  271. End Sub
  272. Private Sub Form_Paint()
  273.     Line (OutputRect.left, OutputRect.top)-(OutputRect.right, OutputRect.bottom), QBColor(14), BF
  274. End Sub
  275. Private Sub mnuClear_Click()
  276.     frmDrawText.Refresh
  277. End Sub
  278.