ACC: How to Convert Twips to Pixels

Last reviewed: August 29, 1997
Article ID: Q94927
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

Because Microsoft Access stores dimension/location properties as twips, it may be necessary to convert to pixels in certain cases, such as when calling a Windows API function. This article describes how to do this.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access 2.0 and earlier. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in version 2.0.

MORE INFORMATION

You can use the ConvertTwipsToPixels() function below to convert twips to pixels. Note that pixels are not always square (the height and width are not the same); therefore, it is necessary to pass in the desired "direction" to use (horizontal or vertical).

  1. To add this function to your application, type one of the following examples in a new module, depending on your version of Microsoft Access:

    In Microsoft Access 7.0 and 97:

        Option Explicit
        Declare Function GetDC% Lib "User32" (ByVal hw%)
        Declare Function ReleaseDC% Lib "User32" (ByVal hw%, ByVal hDC%)
        Declare Function GetDeviceCaps% Lib "Gdi32" (ByVal hDC%, _
           ByVal iCapability%)
    

        Const WU_LOGPIXELSX = 88
        Const WU_LOGPIXELSY = 90
    

    NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

        Function ConvertTwipsToPixels (lngTwips as Long, lngDirection _
        as long) As Long
    
        Dim lngDC as long                        'Handle to device
        Dim lngPixelsPerInch as Long
        Const nTwipsPerInch = 1440
        lngDC = GetDC(0)
        If (lngDirection = 0) Then       'Horizontal
            lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
        Else                            'Vertical
            lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
        End If
        lngDC = ReleaseDC(0, lngDC)
        ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
        End Function
    
        Function ShowConvert()
           Dim lngOldTwips As Long
           lngOldTwips = 2377
           ShowConvert = ConvertTwipsToPixels(lngOldTwips, 0)
        End Function
    
       In Microsoft Access 1.x and 2.0:
    
       NOTE: In the following code samples, an underscore (_) at the end of a
       line is used as a line-continuation character. Remove the underscore
       from the end of the line when re-creating this code in Access Basic.
    
        Option Explicit
        Declare Function GetDC% Lib "User" (ByVal hw%)
        Declare Function ReleaseDC% Lib "User" (ByVal hw%, ByVal hDC%)
        Declare Function GetDeviceCaps% Lib "Gdi" (ByVal hDC%, _
            ByVal iCapability%)
    
        Const WU_LOGPIXELSX = 88
        Const WU_LOGPIXELSY = 90
    
       NOTE: You may have some Microsoft Windows API functions defined in an
       existing Microsoft Access library; therefore, your declarations may
       be duplicates. If you receive a duplicate procedure name error message,
       remove or comment out the declarations statement in your code.
    
        Function ConvertTwipsToPixels (nTwips%, nDirection%) As Integer
           Dim hDC%                        'Handle to device
           Dim nPixelsPerInch%
           Const nTwipsPerInch = 1440
           hDC% = GetDC(0)
           If (nDirection% = 0) Then       'Horizontal
              nPixelsPerInch% = GetDeviceCaps(hDC%, WU_LOGPIXELSX)
           Else                            'Vertical
              nPixelsPerInch% = GetDeviceCaps(hDC%, WU_LOGPIXELSY)
           End If
             hDC% = ReleaseDC(0, hDC%)
             ConvertTwipsToPixels = (nTwips% / nTwipsPerInch%) * nPixelsPerInch
        End Function
    
        Function ShowConvert()
           Dim intOldTwips As Integer
           intOldTwips = 2377
           ShowConvert = ConvertTwipsToPixels(intOldTwips, 0)
         End Function
    
    

  2. To call this function, pass the number of twips you want to convert, and another parameter indicating the horizontal or vertical measurement (0 for horizontal, non-zero for vertical). The following is a sample call:

          OldTwips = 2377
          NewPixels = ConvertTwipsToPixels(OldTwips, 0)
    

REFERENCES

For more information about this topic, search for "declare statement," using the Microsoft Access Help Index.

Keywords          : kbprg PgmApi
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.