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 / samples4 / ch17 / textmsgs.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  8.1 KB  |  235 lines

  1. VERSION 4.00
  2. Begin VB.Form TextMsgs 
  3.    Caption         =   "Text Control Messages Demo"
  4.    ClientHeight    =   4020
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1770
  7.    ClientWidth     =   7365
  8.    BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  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.    Height          =   4710
  19.    Left            =   1035
  20.    LinkMode        =   1  'Source
  21.    LinkTopic       =   "Form1"
  22.    ScaleHeight     =   4020
  23.    ScaleWidth      =   7365
  24.    Top             =   1140
  25.    Width           =   7485
  26.    Begin VB.VScrollBar VScroll1 
  27.       Height          =   1935
  28.       LargeChange     =   5
  29.       Left            =   3360
  30.       Max             =   1
  31.       TabIndex        =   4
  32.       Top             =   1500
  33.       Width           =   315
  34.    End
  35.    Begin VB.TextBox Text1 
  36.       Height          =   3015
  37.       Left            =   120
  38.       MultiLine       =   -1  'True
  39.       TabIndex        =   0
  40.       Text            =   "TEXTMSGS.frx":0000
  41.       Top             =   600
  42.       Width           =   3015
  43.    End
  44.    Begin VB.Label LabelShowLine 
  45.       BackColor       =   &H80000005&
  46.       BorderStyle     =   1  'Fixed Single
  47.       Height          =   315
  48.       Left            =   3780
  49.       TabIndex        =   3
  50.       Top             =   2340
  51.       Width           =   3495
  52.    End
  53.    Begin VB.Label LabelLinenum 
  54.       Appearance      =   0  'Flat
  55.       ForeColor       =   &H80000008&
  56.       Height          =   315
  57.       Left            =   3780
  58.       TabIndex        =   5
  59.       Top             =   1980
  60.       Width           =   1455
  61.    End
  62.    Begin VB.Label LabelResult 
  63.       BackColor       =   &H80000005&
  64.       BorderStyle     =   1  'Fixed Single
  65.       Height          =   255
  66.       Left            =   4380
  67.       TabIndex        =   2
  68.       Top             =   600
  69.       Width           =   2835
  70.    End
  71.    Begin VB.Label Label1 
  72.       Alignment       =   1  'Right Justify
  73.       Appearance      =   0  'Flat
  74.       Caption         =   "Result:"
  75.       ForeColor       =   &H80000008&
  76.       Height          =   255
  77.       Left            =   3420
  78.       TabIndex        =   1
  79.       Top             =   600
  80.       Width           =   915
  81.    End
  82.    Begin VB.Menu MenuSetup 
  83.       Caption         =   "Setup"
  84.       Begin VB.Menu MenuFillText 
  85.          Caption         =   "FillText"
  86.       End
  87.    End
  88.    Begin VB.Menu MenuTests 
  89.       Caption         =   "Tests"
  90.       Begin VB.Menu MenuLineCount 
  91.          Caption         =   "LineCount"
  92.       End
  93.       Begin VB.Menu MenuFirstVisible 
  94.          Caption         =   "FirstVisible"
  95.       End
  96.       Begin VB.Menu MenuSelected 
  97.          Caption         =   "Selected"
  98.       End
  99.       Begin VB.Menu MenuLinesVisible 
  100.          Caption         =   "LinesVisible"
  101.       End
  102.    End
  103. Attribute VB_Name = "TextMsgs"
  104. Attribute VB_Creatable = False
  105. Attribute VB_Exposed = False
  106. Option Explicit
  107. ' copyright 
  108.  1997 by Desaware Inc. All Rights Reserved
  109. Private Sub Form_Load()
  110.     ' Initialize the display line command
  111.     UpdateDisplayLine
  112. End Sub
  113. ' Determines the number of lines actually visible in the
  114. ' text control.
  115. Private Function GetVisibleLines%()
  116.     Dim rc As RECT
  117.     #If Win32 Then
  118.     Dim hDC&
  119.     Dim lfont&, oldfont&
  120.     Dim di&, lc&
  121.     #Else
  122.     Dim hDC%
  123.     Dim lfont%, oldfont%
  124.     Dim di%, lc%
  125.     #End If
  126.     Dim tm As TEXTMETRIC
  127.     ' Get the formatting rectangle - this describes the
  128.     ' rectangle in the control in which text is placed.
  129.     lc = SendMessage(Text1.hwnd, EM_GETRECT, 0, rc)
  130.     ' Get a handle to the logical font used by the control.
  131.     ' The VB font properties are accurately reflected by
  132.     ' this logical font.
  133.     lfont = SendMessageBynum(Text1.hwnd, WM_GETFONT, 0, 0&)
  134.     ' Get a device context to the text control.
  135.     hDC = GetDC(Text1.hwnd)
  136.     ' Select in the logical font to obtain the exact font
  137.     ' metrics.
  138.     If lfont <> 0 Then oldfont = SelectObject(hDC, lfont)
  139.     di = GetTextMetrics(hDC, tm)
  140.     ' Select out the logical font
  141.     If lfont <> 0 Then lfont = SelectObject(hDC, oldfont)
  142.     ' The lines depends on the formatting rectangle and font height
  143.     GetVisibleLines% = (rc.bottom - rc.top) / tm.tmHeight
  144.     ' Release the device context when done.
  145.     di = ReleaseDC(Text1.hwnd, hDC)
  146. End Function
  147. ' Fill the text control with 20 lines of text
  148. Private Sub MenuFillText_Click()
  149.     Dim x%
  150.     Dim t$
  151.     For x% = 0 To 19
  152.         t$ = t$ + "This is line" + Str$(x%) + Chr$(13) + Chr$(10)
  153.     Next x%
  154.     Text1.Text = t$
  155. End Sub
  156. ' Determine the number of the first line visible in the text control
  157. Private Sub MenuFirstVisible_Click()
  158.     Dim lc%
  159.     lc% = SendMessageBynum(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0, 0&)
  160.     LabelResult.Caption = "Line" + Str$(lc%) + " at top"
  161. End Sub
  162. ' Determine the number of lines of text in the text control
  163. Private Sub MenuLineCount_Click()
  164.     Dim lc%
  165.     lc% = SendMessageBynum(Text1.hwnd, EM_GETLINECOUNT, 0, 0&)
  166.     LabelResult.Caption = Str$(lc%) + " lines"
  167. End Sub
  168. ' Determine the number of visibile lines in the text control.
  169. Private Sub MenuLinesVisible_Click()
  170.     LabelResult.Caption = Str$(GetVisibleLines()) + " lines visible"
  171. End Sub
  172. ' Determine the start and end position of the current selection
  173. Private Sub MenuSelected_Click()
  174.     Dim ls&
  175.     ls& = SendMessageBynum&(Text1.hwnd, EM_GETSEL, 0, 0&)
  176.     LabelResult.Caption = "Chars" + Str$(CInt(ls& And &HFFFF&)) + " to" + Str$(CInt(ls& / &H10000))
  177. End Sub
  178. ' Update the display line information on change
  179. Private Sub Text1_Change()
  180.    Dim lc&
  181.     ' Make sure the vertical scroll range matches the number
  182.     ' of lines in the text control
  183.     lc = SendMessageBynum(Text1.hwnd, EM_GETLINECOUNT, 0, 0&)
  184.     VScroll1.Max = lc - 1
  185.     UpdateDisplayLine
  186. End Sub
  187. ' This function updates the line displayed based on the
  188. ' current position of the scroll bar.
  189. Private Sub UpdateDisplayLine()
  190.     Dim linetoshow%, linelength%
  191.     Dim linebuf$
  192.     Dim lc%
  193.     Dim linechar%
  194.     linetoshow% = VScroll1.value
  195.     ' Show the number of the line being displayed
  196.     LabelLinenum.Caption = "Line" + Str$(linetoshow%)
  197.     ' Find out the character offset to the first character
  198.     ' in the specified line
  199.     linechar% = SendMessageBynum(Text1.hwnd, EM_LINEINDEX, linetoshow%, 0&)
  200.     ' The character offset is used to determine the length of the line
  201.     ' containing that character.
  202.     lc% = SendMessageBynum(Text1.hwnd, EM_LINELENGTH, linechar%, 0&) + 1
  203.     ' Now allocate a string long enough to hold the result
  204.     linebuf$ = String$(lc% + 2, 0)
  205.     Mid$(linebuf$, 1, 1) = Chr$(lc% And &HFF)
  206.     Mid$(linebuf$, 2, 1) = Chr$(lc% \ &H100)
  207.     ' Now get the line
  208.     lc% = SendMessageBystring(Text1.hwnd, EM_GETLINE, linetoshow%, linebuf$)
  209.     LabelShowLine.Caption = left$(linebuf$, lc%)
  210. End Sub
  211. ' Whenever the scroll bar changes, display the requested
  212. ' line in the LabelShowLine label box
  213. Private Sub VScroll1_Change()
  214.     Dim lc%
  215.     Dim dl&
  216.     Dim firstvisible%, lastvisible%
  217.     ' Make sure value is in range
  218.     lc% = SendMessageBynum(Text1.hwnd, EM_GETLINECOUNT, 0, 0&)
  219.     If VScroll1.value > lc% - 1 Then
  220.         VScroll1.value = lc% - 1
  221.         Exit Sub
  222.     End If
  223.     UpdateDisplayLine ' Update the display
  224.     ' Get the number of the first and last visible line
  225.     firstvisible% = SendMessageBynum(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0, 0&)
  226.     lastvisible% = GetVisibleLines%() + firstvisible% - 1
  227.     ' Scroll it into view if necessary
  228.     If (VScroll1.value < firstvisible%) Then
  229.         dl& = SendMessageBynum(Text1.hwnd, EM_LINESCROLL, 0, CLng(VScroll1.value - firstvisible%))
  230.     End If
  231.     If (VScroll1.value > lastvisible%) Then
  232.         dl& = SendMessageBynum(Text1.hwnd, EM_LINESCROLL, 0, CLng(VScroll1.value - lastvisible%))
  233.     End If
  234. End Sub
  235.