Consulting

Results 1 to 3 of 3

Thread: Pixel to Point ratio

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    41
    Location

    Pixel to Point ratio

    Can anyone tell me how to retrieve a screen's pixels-per-point ratio in VBA? I've tried this:

    [vba]MsgBox CDbl(1) / CDbl(ActiveWindow.PointsToScreenPixelsX(1))
    [/vba]
    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).

  2. #2
    VBAX Regular
    Joined
    Jul 2008
    Location
    Cincinnati, OH
    Posts
    86
    Location
    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???

    [VBA]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
    [/VBA]

  3. #3
    Quote Originally Posted by TomSchreiner
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •