Wednesday, December 17, 2008

[vb6] Scrolling multiline textboxes in code

The user can scroll a multiline textbox by using the vertical scroll bar (provided you've assigned a scrollbar to the control), but the developer cannot, unless resorting to sending messages to the control.
    Private Const EM_SCROLL& = &HB5
Private Const SB_LINEDOWN& = 1
Private Const SB_LINEUP& = 0
Private Const EM_LINESCROLL& = &HB6
Private Const EM_SCROLLCARET& = &HB7
Private Const SB_PAGEDOWN& = 3
Private Const SB_PAGEUP& = 2
Private Const EM_GETFIRSTVISIBLELINE& = &HCE
Private Declare Function SendMessageBynum& Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long)
Send the message EM_SCROLL to scroll up or down. This example scrolls down one page:
    SendMessageBynum Text1.hwnd, EM_SCROLL, SB_PAGEDOWN, 0
Send the message EM_SCROLLCARET to scroll to where the caret is:
    SendMessageBynum Text1.hwnd, EM_SCROLLCARET, 0, 0
Send the message EM_LINESCROLL to scroll a specified number of lines or to a specific line index. This example scrolls to line 10:
    Dim FLine&
FLine = SendMessageBynum(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0, 0)
SendMessageBynum Text1.hwnd, EM_LINESCROLL, 0, 10 - FLine
First, the index of the first visible line is retrieved into FLine. Then, the text is scrolled a number of lines (negative is down, positive is up).

No comments:

Post a Comment