'-----------------------------------------------------------------------------
' Utilities from http://blog.nkadesign.com/microsoft-access/
' (c) Renaud Bompuis, 2008
' Licensed under the Creative Commons Attribution License
' http://creativecommons.org/licenses/by/3.0/
' http://creativecommons.org/licenses/by/3.0/legalcode
'
' Free for re-use in any application or tutorial providing clear credit
' is made about the origin of the code and a link to the site above
' is prominently displayed where end-user can access it.
'
' updated by arnel gp for 64bit
'-----------------------------------------------------------------------------
Option Compare Database
Option Explicit
Private Type RECT
X1 As Long
Y1 As Long
X2 As Long
Y2 As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
#Else
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, Rectangle As RECT) As Boolean
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
#End If
Private Const WU_LOGPIXELSX = 88
Private Const WU_LOGPIXELSY = 90
Sub CenterForm(F As Form)
Dim FormWidth As Long, FormHeight As Long
Dim maxWidth As Long, maxHeight As Long
Dim screenWidth As Long, screenHeight As Long
Dim formAllMarginsHeight As Long, formAllMarginsWidth As Long
' Compute maximal acceptable dialog box size in twips
GetScreenResolution screenWidth, screenHeight
screenWidth = ConvertPixelsToTwips(screenWidth, 0)
screenHeight = ConvertPixelsToTwips(screenHeight, 0)
maxWidth = screenWidth * 0.6
maxHeight = screenHeight * 0.9
' Calculate the height and width of the area around the textbox
formAllMarginsHeight = F.WindowHeight - F.Section(acDetail).Height
formAllMarginsWidth = F.Width
' Assess proper width and height of the overall dialog box
FormWidth = formAllMarginsWidth
FormHeight = formAllMarginsHeight
' Adjust position of the th box to the middle if there is not much text.
If FormHeight < F.WindowHeight Then
FormHeight = F.WindowHeight
End If
' Redimension the dialog and display the message at the center of the screen
DoCmd.MoveSize (screenWidth - FormWidth) / 2, (screenHeight - FormHeight) / 2, FormWidth, FormHeight
End Sub
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' '
' ' FROM form_Dialog
' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
'-----------------------------------------------------------------------------
' Get the screen resolution
'-----------------------------------------------------------------------------
Private Sub GetScreenResolution(ByRef Width As Long, ByRef Height As Long)
Dim r As RECT
Dim retVal As Long
#If VBA7 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If
hwnd = GetDesktopWindow()
retVal = GetWindowRect(hwnd, r)
Width = r.X2 - r.X1
Height = r.Y2 - r.Y1
End Sub
'-----------------------------------------------------------------------------
' Pixel to Twips conversions
'-----------------------------------------------------------------------------
' cf http://support.microsoft.com/default.aspx?scid=kb;en-us;210590
' To call this function, pass the number of twips you want to convert,
' and another parameter indicating the horizontal or vertical measurement
' (0 for horizontal, non-zero for vertical). The following is a sample call:
'
Function ConvertTwipsToPixels(lngTwips As Long, lngDirection As Long) As Long
'Handle to device
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440
#If VBA7 Then
Dim lngDC As LongPtr
#Else
Dim lngDC As Long
#End If
lngDC = GetDC(0)
If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
End Function
Function ConvertPixelsToTwips(lngPixels As Long, lngDirection As Long) As Long
'Handle to device
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440
#If VBA7 Then
Dim lngDC As LongPtr
#Else
Dim lngDC As Long
#End If
lngDC = GetDC(0)
If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertPixelsToTwips = (lngPixels * nTwipsPerInch) / lngPixelsPerInch
End Function