How to scroll a window automatically

This MWAPI tip was adapted from a contribution by Michael Volke, 100042.1323@compuserve.com

Neither the MWAPI Standard nor Workstation extensions to it allow for automatically scrolling a window. However, if you position a gadget outside the viewport, Workstation will supply a scrollbar for you and let the user manually scroll the window. But there are situations where you might want to do this under program control. Suppose, for instance, that the user navigates with the TAB key through the screen, or, that your program sets the focus directly to a gadget outside the viewport. It makes sense then to scroll the window such that the gadget with focus is visible. The method is to use Workstation's low level Windows API and send a VSCROLL message (vertical scroll) to the window. Workstation and Windows will then scroll the window and reset the scrollbar accordingly. It works similarly with a HSCROLL message (horizontal scroll).

SCROLL(WIN,VPOS)	;scroll window WIN vertically to VPOS (pixels)
	N HWND,WPARAM
	S HWND=$&MWAPI.GetHWND(^$W(WIN,"ID")) ;get handle to window WIN
	I VPOS<0 S VPOS=0 ;in case of an invalid parameter
	;The third parameter of the message (WPARAM) is a 32 bit value
	;containing the subcommand in the low order 16 bits and
	;possibly a parameter for it in the high order 16 bits.
	;Subcommand SB_THUMBPOSITION requires as parameter the absolute
	;position (pixels) to scroll to.
	S WPARAM=(VPOS*65536)+$$THUMBP ;subcommand and parameter
	I $&MWAPI.SendWindowMessage(HWND,$$VSCROLL,WPARAM,0) ;send
	Q  ;done
	;
	;---- MS-Windows API ----
	;See e.g. WINUSER.H, http://msdn.microsoft.com/library/psdk/winui
	;Messages
VSCROLL()	Q 277 ;WM_VSCROLL (vertical scroll)
	;Scrollbar-commands
	;(SB_ENDSCROLL and SB_THUMBTRACK still missing)
LINEUP()	Q 0 ;SB_LINEUP
LINEDOWN()	Q 1 ;SB_LINEDOWN
PAGEUP()	Q 2 ;SB_PAGEUP
PAGEDOWN()	Q 3 ;SB_PAGEDOWN
THUMBP()	Q 5 ;SB_THUMBPOSITION
TOP()	Q 6 ;SB_TOP
BOTTOM()	Q 7 ;SB-BOTTOM