PDA

View Full Version : Pixel to Point ratio



dlh
09-01-2008, 09:46 AM
Can anyone tell me how to retrieve a screen's pixels-per-point ratio in VBA? I've tried this:

MsgBox CDbl(1) / CDbl(ActiveWindow.PointsToScreenPixelsX(1))

But I must be misinterpreting the usage of PointsToScreenPixels because the above gives a negative number. And moreover 1/PtsToPix(1) <> 100/PtsToPix(100).

Plus any reference to ActiveWindow breaks when not actually run from the workbook (i.e., debugging from the code-window doesn't work).

TomSchreiner
09-01-2008, 03:18 PM
I have not had any success with ActiveWindow.PointsToScreenPixels for anything except finding the left/top screen location of cell 1 of the visible range. Notta beyond that. Just curious as to what you need this for???

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

Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90

Sub Example()
Dim hdc As Long
Dim PixPerInchX As Long
Dim PixPerInchY As Long
Dim PixPerPtX As Double
Dim PixPerPtY As Double

hdc = GetDC(0)

PixPerInchX = GetDeviceCaps(hdc, LOGPIXELSX)
PixPerInchY = GetDeviceCaps(hdc, LOGPIXELSY)

'there are 72 points per inch
PixPerPtX = PixPerInchX / 72
PixPerPtY = PixPerInchY / 72

Debug.Print "PixPerPtX: " & PixPerPtX, "PixPerPtY: " & PixPerPtY

ReleaseDC 0, hdc
End Sub

DoctorRad
08-05-2010, 10:32 AM
I have not had any success with ActiveWindow.PointsToScreenPixels for anything except finding the left/top screen location of cell 1 of the visible range. Notta beyond that. Just curious as to what you need this for???

One possible reason for needing this in VBA would be to place form controls over the top of a ChartSpace ActiveX component and move them relative to elements of the chart. The ChartSpace position values are given in pixels, whereas positions for form controls are set in points, hence the need to convert.

Your code helped me do just this, many thanks.