Option Compare Database
Option Explicit
' Module to provide metrics information (from GetSystemMetrics) and conversions -
' twips <-> pixel conversions
' CREATED: 6 march 2004, Paul J. Champion
Public Enum AccessSpecifications
maxSectionHeight = 31680
maxFormWidth = 31680
maxReportWidth = 31680
RecordSelectorWidth = 300
End Enum
Public Const DefaultMargin = 60 '60 twips = 0.1 cm, a nice small but clear margin
'Public Const AccessSpecifications.maxSectionHeight = 31680
Private Type RECT
Left As Long
top As Long
right As Long
bottom As Long
End Type
Public Type Size
Width As Long
Height As Long
End Type
' TWIPS to PIXELS api declares & constants
'changes for 64-bit
'###############################################
'Add PtrSafe - required for 64-bit Office (VBA7)
#If VBA7 Then
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
' GetSystemMetrics() codes
Private Declare PtrSafe Function apiGetSystemMetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#ElseIf Win64 Then 'need datatype LongPtr
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 LongPtr
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, ByVal nIndex As LongPtr) As LongPtr
' GetSystemMetrics() codes
Private Declare PtrSafe Function apiGetSystemMetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As LongPtr) As LongPtr
#Else '32-bit Office
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
' GetSystemMetrics() codes
Private Declare Function apiGetSystemMetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#End If
'###############################################
Private Const HWND_DESKTOP As Long = 0
Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Public Enum SystemConstants
SM_CXSCREEN = 0
SM_CYSCREEN = 1
SM_CXVSCROLL = 2
SM_CYHSCROLL = 3
SM_CYCAPTION = 4
SM_CXBORDER = 5
SM_CYBORDER = 6
SM_CXDLGFRAME = 7
SM_CYDLGFRAME = 8
SM_CYVTHUMB = 9
SM_CXHTHUMB = 10
SM_CXICON = 11
SM_CYICON = 12
SM_CXCURSOR = 13
SM_CYCURSOR = 14
SM_CYMENU = 15
SM_CXFULLSCREEN = 16
SM_CYFULLSCREEN = 17
SM_CYKANJIWINDOW = 18
SM_MOUSEPRESENT = 19
SM_CYVSCROLL = 20
SM_CXHSCROLL = 21
SM_DEBUG = 22
SM_SWAPBUTTON = 23
SM_RESERVED1 = 24
SM_RESERVED2 = 25
SM_RESERVED3 = 26
SM_RESERVED4 = 27
SM_CXMIN = 28
SM_CYMIN = 29
SM_CXSIZE = 30
SM_CYSIZE = 31
SM_CXFRAME = 32
SM_CYFRAME = 33
SM_CXMINTRACK = 34
SM_CYMINTRACK = 35
SM_CXDOUBLECLK = 36
SM_CYDOUBLECLK = 37
SM_CXICONSPACING = 38
SM_CYICONSPACING = 39
SM_MENUDROPALIGNMENT = 40
SM_PENWINDOWS = 41
SM_DBCSENABLED = 42
SM_CMOUSEBUTTONS = 43
SM_CMETRICS = 44
SM_CXSIZEFRAME = SM_CXFRAME
SM_CYSIZEFRAME = SM_CYFRAME
SM_CXFIXEDFRAME = SM_CXDLGFRAME
SM_CYFIXEDFRAME = SM_CYDLGFRAME
SM_TABLETPC = 86
End Enum
'this is do with the mdi client
'###############################################
'Add PtrSafe - required for 64-bit Office (VBA7)
#If VBA7 Then
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare PtrSafe Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
#ElseIf Win64 Then 'need datatype LongPtr
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, lpRect As RECT) As LongPtr
Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hWnd As LongPtr, lpRect As RECT) As LongPtr
Private Declare PtrSafe Function GetParent Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
#Else '32-bit Office
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
#End If
'###############################################
' Error handling
Dim ErrorSource As String
' Returns a size variable containing the width and height of the MDI Client area
' for Microsoft Access at that moment in time.
'
' ARGUMENTS
' InPixels TRUE - return the size in pixels
' FALSE (default) - return the size in twips
Public Function AccessAppInsideSize _
(Optional InPixels As Boolean = False) _
As Size
Dim rectMDIClient As RECT
Dim sizePixels As Size, sizeTwips As Size
Dim WidthInPixels As Long, HeightInPixels As Long
'
' Initialize error handling
On Error Resume Next ' if an error occurs this routine will return a size of 0,0
'
' Open a dummy form or use an existing form so we can grab the parent of its window handle (hWnd)
Dim F As New Form
Dim bCloseForm As Boolean
Dim bFoundNonPopUpForm As Boolean
If Forms.count > 0 Then
' Find non-PopUp form
Dim frm As Access.Form
For Each frm In Forms
If Not frm.PopUp Then
Set F = frm
bCloseForm = False
bFoundNonPopUpForm = True
Exit For
End If
Next
End If
If Not bFoundNonPopUpForm Then
Set F = CreateForm
bCloseForm = True
End If
'
' Get the screen coordinates and window size of the rectMDIClient window
GetWindowRect GetParent(F.hWnd), rectMDIClient
' Close dummy form without saving
If bCloseForm Then DoCmd.Close acForm, F.Name, acSaveNo
'
' Calculate size in pixels
sizePixels.Width = rectMDIClient.right - rectMDIClient.Left - modMetrics.System(SM_CXFRAME, False)
sizePixels.Height = rectMDIClient.bottom - rectMDIClient.top - modMetrics.System(SM_CYFRAME, False)
'
' Return correct values
If InPixels Then
' return result as is
AccessAppInsideSize = sizePixels
Else
' convert result to twips
sizeTwips.Width = modMetrics.converttoTwipsX(sizePixels.Width)
sizeTwips.Height = modMetrics.converttoTwipsY(sizePixels.Height)
AccessAppInsideSize = sizeTwips
End If
End Function
' Converts twips to pixels
'
Public Function converttoPixelX(ByVal TwipsX As Long) As Long
Dim lngDC As Long, intPerPixelX As Integer
lngDC = GetDC(HWND_DESKTOP)
intPerPixelX = 1440 / GetDeviceCaps(lngDC, LOGPIXELSX)
ReleaseDC HWND_DESKTOP, lngDC
converttoPixelX = TwipsX / intPerPixelX
End Function
' Converts pixels to twips
'
'
Public Function converttoPixelY(ByVal TwipsY As Long) As Long
Dim lngDC As Long, intPerPixelY As Integer
lngDC = GetDC(HWND_DESKTOP)
intPerPixelY = 1440 / GetDeviceCaps(lngDC, LOGPIXELSY)
ReleaseDC HWND_DESKTOP, lngDC
converttoPixelY = TwipsY / intPerPixelY
End Function
' Converts pixels to twips
'
Public Function converttoTwipsX(ByVal PixelX As Long) As Long
Dim lngDC As Long, intPerPixelX As Integer
lngDC = GetDC(HWND_DESKTOP)
intPerPixelX = 1440 / GetDeviceCaps(lngDC, LOGPIXELSX)
ReleaseDC HWND_DESKTOP, lngDC
converttoTwipsX = intPerPixelX * PixelX
End Function
' Converts pixels to twips
'
Public Function converttoTwipsY(ByVal PixelY As Long) As Long
Dim lngDC As Long, intPerPixelY As Integer
lngDC = GetDC(HWND_DESKTOP)
intPerPixelY = 1440 / GetDeviceCaps(lngDC, LOGPIXELSY)
ReleaseDC HWND_DESKTOP, lngDC
converttoTwipsY = intPerPixelY * PixelY
End Function
' Converts point size (from a font) to twips
'
Public Function converttoTwipsYFromPoint(PointSize As Long) As Long
Dim lngDC As Long, intPerPixelY As Integer
lngDC = GetDC(HWND_DESKTOP)
intPerPixelY = 1440 / GetDeviceCaps(lngDC, LOGPIXELSY)
converttoTwipsYFromPoint = intPerPixelY * Int(PointSize * GetDeviceCaps(lngDC, LOGPIXELSY) / 72)
ReleaseDC HWND_DESKTOP, lngDC
End Function
' Returns the dimensions of the screen, in twips or pixels
'
Public Function MetricsScreenHeight _
(Optional ConvertToTwips As Boolean = True)
MetricsScreenHeight = System(SM_CYSCREEN, ConvertToTwips)
End Function
Public Function MetricsScreenWidth _
(Optional ConvertToTwips As Boolean = True)
MetricsScreenWidth = System(SM_CXSCREEN, ConvertToTwips)
End Function
' Returns a result of GetSystemMetrics in twips or pixels
'
Public Function System(SystemMetricRequired As SystemConstants, Optional ConvertToTwips As Boolean = True) As Variant
If ConvertToTwips Then
Select Case SystemMetricRequired
Case SM_CYSCREEN, SM_CYHSCROLL, SM_CYCAPTION, _
SM_CYBORDER, SM_CXDLGFRAME, SM_CYDLGFRAME, _
SM_CYVTHUMB, SM_CYICON, SM_CYCURSOR, _
SM_CYMENU, SM_CYFULLSCREEN, SM_CYKANJIWINDOW, _
SM_CYVSCROLL, SM_CYMIN, SM_CYSIZE, _
SM_CYFRAME, SM_CYMINTRACK, SM_CYDOUBLECLK, _
SM_CYICONSPACING, SM_CYSIZEFRAME, SM_CYFIXEDFRAME
System = converttoTwipsY(apiGetSystemMetrics(SystemMetricRequired))
Case Else: System = converttoTwipsX(apiGetSystemMetrics(SystemMetricRequired))
End Select
Else
System = apiGetSystemMetrics(SystemMetricRequired)
End If
End Function