ACC2000: How to Convert Twips to Pixels

ID: Q210590


The information in this article applies to:

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

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SUMMARY

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.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

You can use the following ConvertTwipsToPixels() function 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. Create a new module and type the following in the Declarations section:
    
    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.


  2. Type the following procedures:


  3. 
    Function ConvertTwipsToPixels (lngTwips as Long, _
       lngDirection as long) As Long
    
       'Handle to device
       Dim lngDC as long                        
       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 
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) 

Additional query words: inf


Keywords          : kbprg kbdta 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: July 6, 1999