Microsoft KB Archive/210141

= ACC2000: How to Get the Right and Down Measurements of a Form =

Article ID: 210141

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210141



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
This article shows you how to create a Visual Basic for Applications Sub procedure called GetFormDimensions to get a form window's right and down measurements so that you can use them in the MoveSize macro action. This article also demonstrates how to position a form immediately below or to the right of a second form.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:

233324 ACC2000: Microsoft Access 2000 Sample Forms Database Available in Download Center

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.



MORE INFORMATION
In Microsoft Access, you can use the WindowWidth and WindowHeight form properties to determine the current width and height of the window that 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 show you how to create and use the GetFormDimensions Sub procedure to get these measurements and to use them to position a form to the right of, and then below, another form by using the MoveSize macro action.

How to Create the Sub Procedure
 Open the sample database Northwind.mdb.  Create a module and type the following line in the Declarations section: Option Explicit

Type RECT_Type left As Long top As Long right As Long bottom As Long End Type

Declare Function apiGetWindowRect Lib "user32" Alias _ "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT_Type) As Long Declare Function apiGetDC Lib "user32" Alias "GetDC" _ (ByVal hWnd As Long) As Long Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal _  hWnd As Long, ByVal hDC As Long) As Long Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" _ (ByVal hDC As Long, ByVal nIndex As Long) As Long Declare Function apiGetActiveWindow Lib "user32" Alias _ "GetActiveWindow" As Long Declare Function apiGetParent Lib "user32" Alias "GetParent" (ByVal _  hWnd As Long) As Long Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" _ (ByVal hWnd As Long, ByVal lpClassName As String, ByVal _  nMaxCount As Long) As Long

Global Const TWIPSPERINCH = 1440   Type the following procedure: Sub GetFormDimensions(F As Form, FLeft As Long, FTop As Long, _  FWidth As Long, FHeight As Long) '*************************************************************  ' PURPOSE: Returns the left, top, width, and height '         measurements of a form window in tdwips. ' ARGUMENTS: '   F: The form object whose measurements are to be determined. '   FLeft, FTop, 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_Type Dim MDIClient As RECT_Type Dim MDIClientLeft As Long Dim MDIClientTop As Long ' 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. apiGetWindowRect F.hWnd, FormRect FLeft = FormRect.left FTop = FormRect.top FWidth = FormRect.right - FormRect.left FHeight = FormRect.bottom - FormRect.top ' Convert the measurements from pixels to twips. ConvertPIXELSToTWIPS FLeft, FTop 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. apiGetWindowRect apiGetParent(F.hWnd), MDIClient MDIClientLeft = MDIClient.left MDIClientTop = MDIClient.top ConvertPIXELSToTWIPS MDIClientLeft, MDIClientTop ' Adjust the form dimensions from the MDIClient ' measurements. FLeft = FLeft - MDIClientLeft FTop = FTop - MDIClientTop End If End Sub

Sub ConvertPIXELSToTWIPS(X As Long, Y As Long)

'*************************************************************  ' 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 Long, hWnd As Long, RetVal As Long Dim XPIXELSPERINCH, YPIXELSPERINCH Const LOGPIXELSX = 88 Const LOGPIXELSY = 90 ' Retrieve the current number of pixels per inch, which is  ' resolution-dependent. hDC = apiGetDC(0) XPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSX) YPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSY) RetVal = apiReleaseDC(0, hDC) ' Compute and return the measurements in twips. X = (X / XPIXELSPERINCH) * TWIPSPERINCH Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH

End Sub

Function GetWindowClass(hWnd As Long) 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 = apiGetClassName(hWnd, Buff, 255) GetWindowClass = left$(Buff, BuffSize)

End Function



How to Use the GetFormDimensions Sub Procedure
 Open the sample database Northwind.mdb.  Add the following Sub procedure to the module that you created in step 2 of the previous section: Sub MoveEmployeesAround

Dim frmCust As Form Dim frmEmp As Form Dim frmCustLeft As Long Dim frmCustTop As Long Dim frmCustWidth As Long Dim frmCustHeight As Long Dim frmEmpLeft As Long Dim frmEmpTop As Long Dim frmEmpWidth As Long Dim frmEmpHeight As Long Set frmCust = Forms!Customers Set frmEmp = Forms!Employees GetFormDimensions frmCust, frmCustLeft, frmCustTop, _ frmCustWidth, frmCustHeight GetFormDimensions frmEmp, frmEmpLeft, frmEmpTop, _ frmEmpWidth, frmEmpHeight frmEmp.SetFocus MsgBox "Move Employees to the right of Customers!" DoCmd.MoveSize frmCustLeft + frmCustWidth, frmCustTop MsgBox "Move Employees below Customers!" DoCmd.MoveSize frmCustLeft, frmCustTop + frmCustHeight

End Sub

</li> Open the Customers and Employees forms in Form view.</li>  Open the Immediate window by pressing CTRL+G. Type the following line, and then press ENTER: MoveEmployeesAround </li> In the message box that appears, click OK. The Employees form will be positioned immediately to the right of the Customers form, and then another message box will appear.</li> In the second message box, click 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.</li></ol>

<div class="references_section">