VB3 Scrolling Text Box Programmatically and Specifing LinesID: Q73371
|
By making a call to the Windows API function SendMessage, you can scroll text a specified number of lines or columns within a Microsoft Visual Basic for Windows text box. By using SendMessage, you can also scroll text programmatically, without user interaction. This technique extends Visual Basic for Windows' scrolling functionality beyond the built-in statements and methods. The sample program below shows how to scroll text vertically and horizontally a specified number of lines.
Visual Basic for Windows itself does not offer a statement for scrolling
text a specified number of lines vertically or horizontally within a text
box. You can scroll text vertically or horizontally by actively clicking
the vertical and horizontal scroll bars for the text box at run time;
however, you do not have any control over how many lines or columns are
scrolled for each click of the scroll bar. Text always scrolls one line or
one column per click the scroll bar. Furthermore, no built-in Visual Basic
for Windows method can scroll text without user interaction. To work around
these limitations, you can call the Windows API function SendMessage, as
explained below.
r& = SendMessage& (hWd%, EM_LINESCROLL, wParam%, lParam&)
hWd% The window handle of the text box.
wParam% Parameter not used.
lParam& The low-order 2 bytes specify the number of vertical
lines to scroll. The high-order 2 bytes specify the
number of horizontal columns to scroll. A positive
value for lParam& causes text to scroll upward or to the
left. A negative value causes text to scroll downward or
to the right.
r& Indicates the number of lines actually scrolled.
' The following appears in the general declarations section of
' the form:
Declare Function GetFocus% Lib "USER" ()
' Assume the following appears in the click event procedure of a
' command button called Scroll.
Sub Command_Scroll_Click ()
OldhWnd% = Screen.ActiveControl.Hwnd
' Store the window handle of the control that currently
' has the focus.
' For Visual Basic 1.0 for Windows use the following line:
' OldhWnd% = GetFocus ()
Text1.SetFocus
hWd% = GetFocus()
End Sub
Declare Function GetFocus% Lib "user" () ' For Visual Basic 1.0 only.
Declare Function PutFocus% Lib "user" Alias "SetFocus" (ByVal
hWd%)
Declare Function SendMessage& Lib "user" (ByVal hWd%,
ByVal wMsg%,
ByVal wParam%,
ByVal lParam&)
Sub InitializeTextBox ()
Text1.Text = ""
For i% = 1 To 50
Text1.Text = Text1.Text + "This is line " + Str$(i%)
' Add 15 words to a line of text.
For j% = 1 to 10
Text1.Text = Text1.Text + " Word "+ Str$(j%)
Next j%
' Force a carriage return (CR) and linefeed (LF).
Text1.Text = Text1.Text + Chr$(13) + Chr$(10)
x% = DoEvents()
Next i%
End Sub
Sub Form_Load ()
Call InitializeTextBox
End Sub
' The following two lines must appear on a single line:
Function ScrollText& (TextBox As Control, vLines As Integer, hLines
As Integer)
Const EM_LINESCROLL = &H406
' Place the number of horizontal columns to scroll in the high-
' order 2 bytes of Lines&. The vertical lines to scroll is
' placed in the low-order 2 bytes.
Lines& = Clng(&H10000 * hLines) + vLines
' Get the window handle of the control that currently has the
' focus, Command1 or Command2.
SavedWnd% = Screen.ActiveControl.Hwnd
' For Visual Basic 1.0 use the following line instead of the one
' used above.
' SavedWnd% = GetFocus%()
' Set the focus to the passed control (text control).
TextBox.SetFocus
' For Visual Basic 1.0, get the handle to current focus (text
' control).
' TextWnd% = GetFocus%()
' Scroll the lines.
Success& = SendMessage(TextBox.HWnd, EM_LINESCROLL, 0, Lines&)
' For Visual Basic 1.0 use the following line instead of the one
' used above.
' Success& = SendMessage(TextWnd%, EM_LINESCROLL, 0, Lines&)
' Restore the focus to the original control, Command1 or
' Command2.
r% = PutFocus% (SavedWnd%)
' Return the number of lines actually scrolled.
ScrollText& = Success&
End Function
Sub Command1_Click ()
' Scroll text 5 vertical lines upward.
Num& = ScrollText&(Text1, 5, 0)
End Sub
Sub Command2_Click ()
' Scroll text 5 horizontal columns to the left.
Num& = ScrollText&(Text1, 0, 5)
End Sub
Additional query words: 2.00 3.00
Keywords : kbcode kbWndw PrgCtrlsStd
Version : 1.00 2.00 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 22, 1999