Results 1 to 3 of 3

Thread: [RESOLVED] Converting pixels to points?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [RESOLVED] Converting pixels to points?

    Is there a way of converting pixels to points? The dimensions in VB are in pixels, but Office COM such as WordApp.Resize are expressed in points. So trying to align app windows is a mess.

    I guess the ratio of pixels to points varies from one monitor to another(?), so this would need to be calculated at run time.

    I tried a nifty trick of doing Me.Font.SizeInPoints / Me.FontHeight but that doesnt seem to be quite right or a very sensible way of doing it.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Converting pixels to points?

    Ive cracked it...

    For posterity (and to save anyone else spending an entire day trying to get API's to work) here is the code.

    VB Code:
    1. Public Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr) As Int32
    2.     Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As IntPtr, ByVal nIndex As Int32) As Int32
    3.     Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Int32, ByVal hdc As Int32) As Int32
    4.     Public Const LOGPIXELSX = 88
    5.     Private Const POINTS_PER_INCH As Long = 72
    6.     Public Function PointsPerPixel() As Double
    7.         Dim hDC As IntPtr
    8.         Dim lDotsPerInch As Int32
    9.         hDC = GetDC(IntPtr.Zero)
    10.         lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
    11.         PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
    12.         ReleaseDC(IntPtr.Zero, hDC)
    13.         Return PointsPerPixel
    14.     End Function

  3. #3
    New Member
    Join Date
    Jun 2010
    Posts
    1

    Re: [RESOLVED] Converting pixels to points?

    Hi
    thanks for your answer. But can you explain about the folloeing lines? and what are 88 and 72? are they g.DpiX and g.DpiY?

    Public Const LOGPIXELSX = 88
    Private Const POINTS_PER_INCH As Long = 72

Posting Permissions

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



Click Here to Expand Forum to Full Width