VB3 Scrolling Text Box Programmatically and Specifing LinesLast reviewed: January 9, 1997Article ID: Q73371 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARYBy 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.
MORE INFORMATIONVisual 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.
ExampleTo 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 for Windows 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% = 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 SubTo 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":
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |