ACC: How to Convert Twips to Pixels

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, in certain cases you may have to convert twips to pixels, such as when you call a Windows API function. This article shows you 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.

Additional query words:

Keywords : kbprg
Version : WINDOWS:1.0,1.1,2.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 14, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.