ACC2: How to Get Form's Right and Down Measurements

ID: Q121100


The information in this article applies to:


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how to create and use an Access Basic Sub procedure called GetFormDimensions to obtain a form window's right and down measurements for use in the MoveSize macro action. This article also demonstrates how to position a form immediately below or to the right of a second form.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Building Applications" manual.


MORE INFORMATION

In Microsoft Access version 2.0, you can use the WindowWidth and WindowHeight form properties to determine the current width and height of the window a form is contained in. However, there are no properties to determine the window's right and down measurements (these measurements are commonly used in the MoveSize macro action to position a form window).

The right and down measurements, along with the width and height, are useful if you want to position a form side-by-side with another form. The following examples demonstrate how to create and use the GetFormDimensions Sub procedure to obtain these measurements and use them to position a form to the right of, and then below, another form using the MoveSize macro action.

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

How to Create the Sub Procedure


  1. Open the sample database NWIND.MDB.


  2. Create a new module and enter the following lines in the module's Declarations section:
    
          Option Explicit
    
          Type Rect
             x1 As Integer
             y1 As Integer
             x2 As Integer
             y2 As Integer
          End Type 

    
          Declare Sub GetWindowRect Lib "User" (ByVal hWnd As Integer, _
             lpRect As Rect)
          Declare Function GetDC Lib "User" (ByVal hWnd As Integer) _
             As  Integer
          Declare Function ReleaseDC Lib "User" (ByVal hWnd As Integer, _
             ByVal hDC As Integer) As Integer
          Declare Function GetDeviceCaps Lib "GDI" (ByVal hDC As Integer, _
             ByVal nIndex As Integer) As Integer
          Declare Function GetActiveWindow Lib "User" () As Integer
          Declare Function GetParent Lib "User" (ByVal hWnd As Integer) _
             As Integer
          Declare Function GetClassName Lib "User" (ByVal hWnd As Integer, _
             ByVal lpClassName As String, ByVal nMaxCount As Integer) _
             As Integer
    
          Global Const TWIPSPERINCH = 1440 


  3. Create the following procedures in the module:
    
          Sub GetFormDimensions (F As Form, FRight, FDown, FWidth, FHeight)
          '*************************************************************
          ' PURPOSE: Returns the right, down, width, and height
          '          measurements of a form window in twips.
          ' ARGUMENTS:
          '    F: The form object whose measurements are to be determined.
          '    FRight, FDown, FWidth, FHeight: Measurement variables
          '    that will return the dimensions of form F "by reference."
          ' NOTE: The FWidth and FHeight values will be equivalent to
          '    those provided by the form WindowWidth and WindowHeight
          '    properties.
          '*************************************************************
             Dim FormRect As Rect
             Dim MDIClient As Rect
             Dim MDIClientRight, MDIClientDown
    
             ' Get the screen coordinates and window size of the form.
             ' The screen coordinates are returned in pixels measured
             ' from the upper-left corner of the screen.
             GetWindowRect F.hWnd, FormRect
             FRight = FormRect.x1
             FDown = FormRect.y1
             FWidth = FormRect.x2 - FormRect.x1
             FHeight = FormRect.y2 - FormRect.y1
    
             ' Convert the measurements from pixels to twips.
             ConvertPIXELSToTWIPS FRight, FDown
             ConvertPIXELSToTWIPS FWidth, FHeight
    
             ' If the form is not a pop-up form, adjust the screen
             ' coordinates to measure from the top of the Microsoft
             ' Access MDIClient window. Position 0,0 for a pop-up form
             ' is the upper left corner of the screen, whereas position
             ' 0,0 for a normal window is the upper left corner of the
             ' Microsoft Access client window below the menu bar.
             If GetWindowClass(F.hWnd) <> "OFormPopup" Then
                ' Get the screen coordinates and window size of the
                ' MDIClient window.
                GetWindowRect GetParent(F.hWnd), MDIClient
                MDIClientRight = MDIClient.x1
                MDIClientDown = MDIClient.y1
                ConvertPIXELSToTWIPS MDIClientRight, MDIClientDown
    
                ' Adjust the form dimensions from the MDIClient
                ' measurements.
                FRight = FRight - MDIClientRight
                FDown = FDown - MDIClientDown
             End If
          End Sub
    
          Sub ConvertPIXELSToTWIPS (X, Y)
          '*************************************************************
          ' PURPOSE: Converts the two pixel measurements passed as
          '          arguments to twips.
          ' ARGUMENTS:
          '    X, Y: Measurement variables in pixels. These will be
          '          converted to twips and returned through the same
          '          variables "by reference."
          '*************************************************************
             Dim hDC As Integer, hWnd As Integer, RetVal As Integer
             Dim XPIXELSPERINCH, YPIXELSPERINCH
             Const LOGPIXELSX = 88
             Const LOGPIXELSY = 90
    
             ' Retrieve the current number of pixels per inch, which is
             ' resolution-dependent.
             hDC = GetDC(0)
             XPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSX)
             YPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSY)
             RetVal = ReleaseDC(0, hDC)
    
             ' Compute and return the measurements in twips.
             X = (X / XPIXELSPERINCH) * TWIPSPERINCH
             Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH
          End Sub
    
          Function GetWindowClass (hWnd) As String
          '*************************************************************
          ' PURPOSE: Retrieve the class of the passed window handle.
          ' ARGUMENTS:
          '    hWnd: The window handle whose class is to be retrieved.
          ' RETURN:
          '    The window class name.
          '*************************************************************
             Dim Buff As String
             Dim BuffSize As Integer
             Buff = String$(255, " ")
             BuffSize = GetClassName(hWnd, Buff, 255)
             GetWindowClass = Left$(Buff, BuffSize)
          End Function 


How to Use the GetFormDimensions Sub Procedure


  1. Open the sample database NWIND.MDB.


  2. Add the following Sub procedure to the module that you created in step 2 above:
    
          Sub MoveEmployeesAround ()
             Dim frmCust As Form, frmEmp As Form
             Dim CustRight, CustDown, CustWidth, CustHeight
             Dim EmpRight, EmpDown, EmpWidth, EmpHeight
    
             Set frmCust = Forms!Customers
             Set frmEmp = Forms!Employees
    
             GetFormDimensions frmCust, CustRight, CustDown, _
                CustWidth,  CustHeight
             GetFormDimensions frmEmp, EmpRight, EmpDown, _
                EmpWidth, EmpHeight
    
             frmEmp.SetFocus
    
             MsgBox "Move Employees to the right of Customers!"
             DoCmd MoveSize CustRight + CustWidth, CustDown
    
             MsgBox "Move Employees below Customers!"
             DoCmd MoveSize CustRight, CustDown + CustHeight
          End Sub 


  3. Open the Customers and Employees forms in Form view.


  4. Select the module, and then choose Immediate Window from the View menu.


  5. Type the following line in the Immediate window, and then press ENTER:

    MoveEmployeesAround


  6. In the message box that appears, choose OK. The Employees form will be positioned immediately to the right of the Customers form, and then another message box will appear.


  7. In the second message box, choose OK. The Employees form will be positioned immediately below the Customers form.


NOTE: Depending on the position and size of the Customers form, the Employees form may be positioned so that you cannot see it.


REFERENCES

For more information about how to get form window's right and down measurements in Microsoft Access 7.0 or 97, please see the following article in the Microsoft Knowledge Base:

Q143162 ACC: How to Get Form's Right and Down Measurements (95/97)

For more information about the MoveSize macro action, search for "MoveSize," and then "MoveSize Action" using the Microsoft Access Help menu.

For more information about the WindowHeight and WindowWidth properties, search for "WindowHeight," and then "WindowHeight, WindowWidth Properties" using the Microsoft Access Help menu.


Keywords          : kbusage FmsProp 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 7, 1999