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

  1. VERSION 5.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 
  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     =   4020
  22.    ScaleWidth      =   7365
  23.    Begin VB.VScrollBar VScroll1 
  24.       Height          =   1935
  25.       LargeChange     =   5
  26.       Left            =   3360
  27.       Max             =   1
  28.       TabIndex        =   4
  29.       Top             =   1500
  30.       Width           =   315
  31.    End
  32.    Begin VB.TextBox Text1 
  33.       Height          =   3015
  34.       Left            =   120
  35.       MultiLine       =   -1  'True
  36.       TabIndex        =   0
  37.       Text            =   "TEXTMSGS.frx":0000
  38.       Top             =   600
  39.       Width           =   3015
  40.    End
  41.    Begin VB.Label LabelShowLine 
  42.       BackColor       =   &H80000005&
  43.       BorderStyle     =   1  'Fixed Single
  44.       Height          =   315
  45.       Left            =   3780
  46.       TabIndex        =   3
  47.       Top             =   2340
  48.       Width           =   3495
  49.    End
  50.    Begin VB.Label LabelLinenum 
  51.       Appearance      =   0  'Flat
  52.       ForeColor       =   &H80000008&
  53.       Height          =   315
  54.       Left            =   3780
  55.       TabIndex        =   5
  56.       Top             =   1980
  57.       Width           =   1455
  58.    End
  59.    Begin VB.Label LabelResult 
  60.       BackColor       =   &H80000005&
  61.       BorderStyle     =   1  'Fixed Single
  62.       Height          =   255
  63.       Left            =   4380
  64.       TabIndex        =   2
  65.       Top             =   600
  66.       Width           =   2835
  67.    End
  68.    Begin VB.Label Label1 
  69.       Alignment       =   1  'Right Justify
  70.       Appearance      =   0  'Flat
  71.       Caption         =   "Result:"
  72.       ForeColor       =   &H80000008&
  73.       Height          =   255
  74.       Left            =   3420
  75.       TabIndex        =   1
  76.       Top             =   600
  77.       Width           =   915
  78.    End
  79.    Begin VB.Menu MenuSetup 
  80.       Caption         =   "Setup"
  81.       Begin VB.Menu MenuFillText 
  82.          Caption         =   "FillText"
  83.       End
  84.    End
  85.    Begin VB.Menu MenuTests 
  86.       Caption         =   "Tests"
  87.       Begin VB.Menu MenuLineCount 
  88.          Caption         =   "LineCount"
  89.       End
  90.       Begin VB.Menu MenuFirstVisible 
  91.          Caption         =   "FirstVisible"
  92.       End
  93.       Begin VB.Menu MenuSelected 
  94.          Caption         =   "Selected"
  95.       End
  96.       Begin VB.Menu MenuLinesVisible 
  97.          Caption         =   "LinesVisible"
  98.       End
  99.    End
  100. Attribute VB_Name = "TextMsgs"
  101. Attribute VB_GlobalNameSpace = False
  102. Attribute VB_Creatable = False
  103. Attribute VB_PredeclaredId = True
  104. Attribute VB_Exposed = False
  105. Option Explicit
  106. ' copyright 
  107.  1997 by Desaware Inc. All Rights Reserved
  108. Private Sub Form_Load()
  109.     ' Initialize the display line command
  110.     UpdateDisplayLine
  111. End Sub
  112. ' Determines the number of lines actually visible in the
  113. ' text control.
  114. Private Function GetVisibleLines%()
  115.     Dim rc As RECT
  116.     #If Win32 Then
  117.     Dim hDC&
  118.     Dim lfont&, oldfont&
  119.     Dim di&
  120.     #Else
  121.     Dim hDC%
  122.     Dim lfont%, oldfont%
  123.     Dim di%
  124.     #End If
  125.     Dim tm As TEXTMETRIC
  126.     Dim lc&
  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.