VBA wrapper function to call the GetDeviceCaps API to determine the monitor's pixels/inch. Pixels per inch is ppi.
The api_GetPPI function is called by the CalendarMaker, an Access program that makes calendar reports, which will be posted shortly.
The pixel thickness of a line is divided by pixels/inch, and then multiplied by 1440 twips/inch to get twips. In this case, a light gray box is drawn inside the calendar lines for the days in the first week that are at the end of the previous month.
'*************** Code Start ***************************************************** ' module name: mod_api_GetDeviceCaps_PPI_s4p ' http://msaccessgurus.com/VBA/Code/API_GetDeviceCaps_ppi.htm '------------------------------------------------------------------------------- ' Purpose : use GetDeviceCaps API to get ppi (pixel/inch) ' Author : crystal (strive4peace) ' License : below code ' Code List: www.msaccessgurus.com/code.htm '------------------------------------------------------------------------------- ' used by CalendarMaker ' edit 230214 using Peter Cole's API Viewer ' https://www.thememydatabase.co.uk/access32to64.html 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 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ api_GetPPI Function api_GetPPI(Optional pbHorizontal As Boolean = True) As Long 's4p 191120, 230206 'get pixels per inch. My monitor is 96 ppi. 'PARAMETER ' pbHorizontal=True to return horizontal ppi, else vertical ppi Const LOGPIXELSX = 88 'pixels/inch in x, logical monitor Const LOGPIXELSY = 90 'pixels/inch in y 'dimension Handle Dim hdc As LongPtr 'set handle hdc = GetDC(0) If pbHorizontal <> True Then 'Vertical ppi api_GetPPI = GetDeviceCaps(hdc,LOGPIXELSY) 'LOGPIXELSY=90 Else 'Horizontal ppi api_GetPPI = GetDeviceCaps(hdc,LOGPIXELSX) 'LOGPIXELSX=88 End If 'release handle hdc = ReleaseDC(0,hdc) End Function ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.msaccessgurus.com '*************** Code End *******************************************************' Made with Color Code add-in posted on http://msaccessgurus.com/tool/Addin_ColorCode.htm
At the top of the module, declare API functions from user32 to get (GetDC) and release (ReleaseDC) a DC (device caps) handle. Also declare GetDeviceCaps from gdi32 to return information about a device. Conditional compiling is used so this will work in all environments.
api_GetPPI is a wrapper function to set a handle to a device, read the value for pixel per inch, then release the device handle.
Unless pbHorizontal is false, the horizontal pixels per inch will be returned.
The index value for item = LOGPIXELSX is 88, which is pixels/inch in the X direction. The index value for item = LOGPIXELSY is 90, which is pixels/inch in the Y direction.
User32.DLL and gdi32.DLL are files with instructions for communicating with and getting information from the user interface and devices.
DLL, the file extension, stands for dynamic link library. DLL files are libraries with instructions that can be dynamically linked at runtime.
default value = True, which means that horizontal ppi will be determined; otherwise it will be for vertical.
Click
HERE
to download the zipped BAS file with the api_GetPPI function
and the API declarations it needs.
(2 kb, unzips to a module BAS file)
This code may be used freely, but you may not sell it in whole or in part. You may include it in applications you use yourself, and that you develop to help others. Keep attribution. Use at your own risk.
Download Peter Cole's free Scanner and Viewer (comes with scanner)
to find problems and lookup correct syntax for API calls.
https://www.thememydatabase.co.uk/access32to64.html
it's free -- click the Download button and then click Add to Cart in the screen that pops up. There won't be a charge.
Help: Device Context Functions
interesting article about Microsoft Windows library files on Wikipedia
The GetDeviceCaps function returns information about capabilities for the specified index. What you won't find on the help link for GetDeviceCaps are the numeric values for the index constants. You'll need this information to call GetDeviceCaps from VBA. I found the constant values in a header file on my computer called wingdi.h
GetDeviceCaps gets information about device capabilities and is in the gdi library. The user32 library is used to get a handle to the screen device, release it when done, and does a lot more! I read that user32 uses gdi for some of its functions, but I don't know if that's true; maybe user32 got pieces from gdi.
Once I found the constant values for the settings I wanted, using GetDeviceCaps turned out to be pretty easy.
Updated 14 Feb 2023
I thought this would work in 32 or 64 bit, but it didn't!
Thanks to
Peter Cole's super-helpful, free
API Viewer,
now it's fixed to work in 64-bit too!
Share with others ...
here's the link to copy:
http://msaccessgurus.com/VBA/Code/API_GetDeviceCaps_ppi.htm
Share your comments! Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more (there is always more)?
Some of you write to say thanks and tell me what you're doing with Access ... its nice to hear from you. I want you to be the best you can with Access, and leverage other applications like Excel, Word, and PowerPoint ... and Windows.
Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.
Let's communicate, collaborate, and appreciate ... we all get better by sharing.
Email me anytime at info@msAccessGurus