How to determine the DPI setting for current user

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to check to see what the DPI setting is using VBA in
Access 2003? I want to be able to determine if the font size is "small" 96
dpi or "Large" 120 dpi and issue a message to the user.

Thanks!
Jerry
 
You can do it with the following API calls. Note, though, that if you change
the DPI setting, the new value can not be detected until Windows is
re-started.

Option Compare Database
Option Explicit

Private Const LOGPIXELSX As Long = 88

Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As Long, _
ByVal nIndex As Long) As Long

Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hwnd As Long) As Long

Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hwnd As Long, _
ByVal hdc As Long) As Long

Public Function GetDpi() As Long

Dim hdcScreen As Long
Dim iDPI As Long

iDPI = -1
hdcScreen = GetDC(0)
If (hdcScreen) Then
iDPI = GetDeviceCaps(hdcScreen, LOGPIXELSX)
ReleaseDC 0, hdcScreen
End If

GetDpi = iDPI

End Function
 
Thanks Brendon - It works great!

Jerry

Brendan Reynolds said:
You can do it with the following API calls. Note, though, that if you change
the DPI setting, the new value can not be detected until Windows is
re-started.

Option Compare Database
Option Explicit

Private Const LOGPIXELSX As Long = 88

Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As Long, _
ByVal nIndex As Long) As Long

Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hwnd As Long) As Long

Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hwnd As Long, _
ByVal hdc As Long) As Long

Public Function GetDpi() As Long

Dim hdcScreen As Long
Dim iDPI As Long

iDPI = -1
hdcScreen = GetDC(0)
If (hdcScreen) Then
iDPI = GetDeviceCaps(hdcScreen, LOGPIXELSX)
ReleaseDC 0, hdcScreen
End If

GetDpi = iDPI

End Function
 
Great, I'm glad it works for you.

I'm not actually sure whether the call to ReleaseDC is required in VBA. I
adapted it from an example in C++, and this *might* be one of those
housekeeping tasks that you have to do in C++ but VB/VBA does automatically
for you. I left it in on the principle of 'better safe than sorry'! :-)
 
Back
Top