Online Book Reader

Home Category

Access Cookbook - Ken Getz [54]

By Root 2160 0
you reopen the form, if the form can find the saved values in the registry, it will reload the last set of coordinates and will size and position itself accordingly.

To use this technique with your own forms, follow these steps:

Import the module basSaveSize from 02-10.MDB into your own application. This module contains the functions necessary to save and restore a form's size and location in the registry.

Add the following code to your form's Load event procedure. This will restore the form's size and location when you load the form:

Private Sub Form_Load ( )

acbRestoreSize Me

End Sub

Add the following code to your form's Unload event procedure. This will save the size and location when you close the form:

Private Sub Form_Unload (Cancel As Integer)

acbSaveSize Me

End Sub

Discussion


Most of the work involved in saving and restoring the form size and location happens in the imported module, basSaveSize. The two event procedures, called from the form's Load and Unload events, simply call procedures in the imported module, passing a reference to the current form.

This solution relies heavily on two built-in functions: SaveSetting and GetSetting. These two functions store and retrieve values from the registry database that's a part of Windows 9x, Windows ME, Windows NT, and Windows 2000. The sample code uses SaveSetting to save each of the four coordinates for a form and GetSetting to retrieve the same information.

SaveSetting and GetSetting make it easy to get and put values in the registry, but they're very limited. They work only with the path HKEY_CURRENT_USER\Software\VB and VBA Program Settings (see Figure 2-17), and they create a new key for each value you save (rather than storing multiple values in one key). If you're interested, investigate their coverage in online help, along with their companion functions, DeleteSetting and GetAllSettings.

Figure 2-17. The registry holds the information about saved form locations

The procedures in basSaveSize also hinge on two Windows API functions. GetWindowRect, aliased as acb_apiGetWindowRect, gets the coordinates of a screen window. MoveWindow, aliased as acb_apiMoveWindow, moves and sizes a window on screen.

WHY USE MOVEWINDOW RATHER THAN MOVESIZE?

You might wonder why you shouldn't use the Access built-in MoveSize macro action: it requires that you select a form first, and this causes the form to display at the time you call the MoveSize action. This looks ugly on screen and makes the procedure less generic. In addition, it requires some work to convert from screen coordinates (pixels), which GetWindowRect uses, to twips, which MoveSize uses. To avoid all these issues, the sample project uses the Windows API method, MoveWindow, instead.

The GetRelativeCoords subroutine in basSaveSize retrieves the coordinates of a given form. Because the MoveWindow function requires a position relative to that of the window's parent to move a window, GetRelativeCoords must find the coordinates of both the requested window and its parent window. It calls the Windows API function GetParent, aliased as acb_apiGetParent, to find the parent and retrieves the coordinates of both. It fills in a user-defined structure with the relative coordinates.

' Store rectangle coordinates.

Type acbTypeRect

lngX1 As Long

lngY1 As Long

lngX2 As Long

lngY2 As Long

End Type

' Windows 95/98/NT4/2000 puts a 2-pixel

' border around the MDI client area, which

' doesn't get taken into account automatically.

' If you're using NT 3.51, you're on your own.

Private Const adhcBorderWidthX = 2

Private Const adhcBorderWidthY = 2

Private Sub GetRelativeCoords(frm As Form, rct As acbTypeRect)

' Fill in rct with the coordinates of the window.

Dim hwndParent As Long

Dim rctParent As acbTypeRect

' Find the position of the window in question, in

' relation to its parent window (the Access desktop, most

' likely, unless the form is modal).

hwndParent = acb_apiGetParent(frm.Hwnd)

' Get the coordinates of the current window and its parent.

acb_apiGetWindowRect

Return Main Page Previous Page Next Page

®Online Book Reader