Microsoft KB Archive/244312

= How To Add Keyboard Functionality to MDI form Scrollbars =

Article ID: 244312

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q244312



SUMMARY
An MDI form displays scrollbars when a portion of an MDI child form is moved out of the view of the client area of the MDI parent form. In Visual Basic these scrollbars support mouse clicks but not the keyboard navigation keys. This article shows how to add keyboard support to these scrollbars. The MDIForm can also be made to scroll to the bottom automatically when it loads so that hidden forms are visible. This functionality is added by sending messages to the MDIForm window.



MORE INFORMATION
Two API calls are needed to achieve this functionality:
 * GetWindow to obtain the Window Handle of the MDI form's client window so that it can be sent a message telling it to scroll.
 * SendMessage to actually send the message.

Step-by-Step Example:
 Start a new Standard EXE project. Form1 is created by default. Set the MDIChild property of Form1 to True.  Add the following code to the General Declarations section of Form1: Option Explicit

' The child window in the client area generates the scrollbars, sends a ' message to the MDI client to scroll to the bottom of the window, and ' traps the key pressed.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) processKey KeyCode End Sub

Private Sub Form_Load Move 2500, 2500 End Sub  On the Project menu, click Add MDI Form to add an MDI form to the project. On the Project menu click Add Module to add a BAS module to the project.  Add the following code to the General Declarations section of Module1: Option Explicit

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _      lParam As Any) As Long

Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _      ByVal wCmd As Long) As Long

Public Const SB_BOTTOM = 7 Public Const SB_LEFT = 6 Public Const SB_LINEDOWN = 1 Public Const SB_LINELEFT = 0 Public Const SB_LINERIGHT = 1 Public Const SB_LINEUP = 0 Public Const SB_PAGEDOWN = 3 Public Const SB_PAGELEFT = 2 Public Const SB_PAGERIGHT = 3 Public Const SB_PAGEUP = 2 Public Const SB_RIGHT = 7 Public Const SB_THUMBPOSITION = 4 Public Const SB_THUMBTRACK = 5 Public Const SB_TOP = 6 Public Const SB_ENDSCROLL = 8

Public Const WM_HSCROLL = &H114 Public Const WM_VSCROLL = &H115

Public Const GW_CHILD = 5

Public Sub processKey(KeyCode As Integer) Dim mdiclientwin As Long

Select Case KeyCode Case vbKeyDown mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_VSCROLL, SB_LINEDOWN, ByVal 0&

Case vbKeyUp mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_VSCROLL, SB_LINEUP, ByVal 0&

Case vbKeyPageUp mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_VSCROLL, SB_PAGEUP, ByVal 0&

Case vbKeyPageDown mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_VSCROLL, SB_PAGEDOWN, ByVal 0&

Case vbKeyRight mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_HSCROLL, SB_LINERIGHT, ByVal 0&

Case vbKeyLeft mdiclientwin = GetWindow(MDIForm1.hwnd, GW_CHILD) SendMessage mdiclientwin, WM_HSCROLL, SB_LINELEFT, ByVal 0&

End Select

End Sub  Run the project. The MDIForm scrolls down to the end. The UP, DOWN, PAGEUP, PAGEDOWN, LEFT and RIGHT keys are now functional.</ol>

<div class="references_section">