Using GetDeviceCaps to return pixels per inch

charlie_hildebrandt
charlie_hildebrandt used Ask the Experts™
on
Have the following code to get the X and Y pixels per inch (VBA in Access). Fails under XP (returns zero, even though GetDC returns good value. Works under 9x/NT/2000

Declare Function GetDC Lib "user32" (ByVal hwnd as Long) As Integer

Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Integer) As Long

hDC = GetDC(Application.hWndAccessApp)

nPixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX)
nPixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY)


Any help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Have you tried the following:

A twip is a unit of length equal to 1/20 of a printer's point, and a printer's point is 1/72 of an inch.

So you can get the TwipsPerPixelX, TwipsPerPixelX  and do the math without needing to get a DC:

Usage:
dcObject.TwipsPerPixelX

Commented:
Don't know if this the problem:

Declare Function GetDC Lib "user32" (ByVal hwnd as Long) As Integer

Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Integer) As Long


All parameters and return types should be Long, not Integer.  This will cause truncation which will cause GetDeviceCaps to not work properly.  I've done this (GetDC and GetDeviceCaps) many times in VB, so it is possible.

Regards,
/rjvs
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- assign points to twalgrave
Please leave any comments here within the
next seven days.
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial