ID Number: Q73371
1.00
WINDOWS
Summary:
By making a call to the Windows API function SendMessage, you can
scroll text a specified number of lines or columns within a Visual
Basic text box. By using SendMessage, you can also scroll text
programmatically, without user interaction. This technique extends
Visual Basic's 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.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows.
More Information:
Note that Visual Basic 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 on 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 on the scroll bar.
Furthermore, no built-in Visual Basic method can scroll text without
user interaction. To work around these limitations, you can call the
Windows API function SendMessage, as explained below.
Example
-------
To scroll the text a specified number of lines within a text box
requires a call to the Windows API function SendMessage using the
constant EM_LINESCROLL. You can invoke the SendMessage function from
Visual Basic as follows:
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 SendMessage API function requires the window handle (hWd% above)
of the text box. To get the window handle of the text box, you must
first set the focus on the text box using the SetFocus method from
Visual Basic. Once the focus has been set, call the GetFocus API
function to get the window handle for the text box. Below is an
example of how to get the window handle of a text box.
'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% = GetFocus () 'Store the window handle of the
'control that currently has the
'focus.
Text1.SetFocus
hWd% = GetFocus()
End Sub
To scroll text horizontally, the text box must have a horizontal
scroll bar, and the width of the text must be wider than the text box
width. Calling SendMessage to scroll text vertically does not require
a vertical scroll bar, but the length of text within the text box
should exceed the text box height.
Below are the steps necessary to create a text box that will scroll
five vertical lines or five horizontal columns each time you click the
command buttons labeled "Vertical" and "Horizontal":
1. From the File menu, choose New Project (ALT+F+N).
2. Double-click on Form1 to bring up the code window.
3. Add the following API declaration in the General Declarations
section of Form1. Note that you must put all Declare statements on a
separate and single line. Also note that SetFocus is aliased as
PutFocus because there already exists a SetFocus method within Visual
Basic.
Declare Function GetFocus% Lib "user" ()
Declare Function PutFocus% Lib "user" Alias "SetFocus" (ByVal
hWd%)
Declare Function SendMessage& Lib "user" (ByVal hWd%,
ByVal wMsg%,
ByVal wParam%,
ByVal lParam&)
4. Create a text box called Text1 on Form1. Set the MultiLine
property to True and the ScrollBars property to Horizontal (1).
5. Create a command button called Command1 and change the Caption
to "Vertical".
6. Create a another command button called Command2 and change the
Caption to "Horizontal".
7. From the General Declarations section of Form1, create a procedure
to initialize some text in the text box as follows:
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) linefeed (LF)
Text1.Text = Text1.Text + Chr$(13) + Chr$(10)
x% = DoEvents()
Next i%
End Sub
8. Add the following code to the load event procedure of Form1:
Sub Form_Load ()
Call InitializeTextBox
End Sub
9. Create the actual scroll procedure within the General Declarations
section of Form1 as follows:
'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% = GetFocus%()
'set focus to the passed control (Text control)
TextBox.SetFocus
'get the handle to current focus (Text control)
TextWnd% = GetFocus%()
'scroll the lines
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
10. Add the following code to the click event procedure of Command1
labeled "Vertical":
Sub Command1_Click ()
'Scroll text 5 vertical lines upward
Num& = ScrollText&(Text1, 5, 0)
End Sub
11. Add the following code to the click event procedure of Command2
labeled "Horizontal":
Sub Command2_Click ()
'Scroll text 5 horizontal columns to the left
Num& = ScrollText&(Text1, 0, 5)
End Sub
12. Run the program. Click the command buttons to scroll the text five
lines or columns at a time.
Reference(s):
"Programming Windows: the Microsoft Guide to Writing Applications for
Windows 3," Charles Petzold. Microsoft Press, 1990.
"Microsoft Windows 3.0 Software Development Kit Reference Volume 1,"
Version 3.0.
WINSDK.HLP file shipped with Microsoft Windows 3.0 Software Development Kit.
Additional reference words: 1.00