ACC2000: How to Convert Twips to Pixels

This article was previously published under Q210590
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

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

Because Microsoft Access stores dimension/location properties as twips, incertain cases you may have to convert twips to pixels, such as when youcall 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.
You can use the following ConvertTwipsToPixels() function to convert twipsto pixels. Note that pixels are not always square (the height andwidth 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 ExplicitDeclare Function GetDC Lib "user32" (ByVal hwnd As Long) As LongDeclare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _  ByVal hdc As Long) As LongDeclare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _  ByVal nIndex As Long) As LongConst WU_LOGPIXELSX = 88Const WU_LOGPIXELSY = 90						
    NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may beduplicates. If you receive a duplicate procedure name error message,remove or comment out the declarations statement in your code.
  2. Type the following procedure:
    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) * lngPixelsPerInchEnd 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:
Function ShowConvert()   Dim lngOldTwips As Long   lngOldTwips = 2377   ShowConvert = ConvertTwipsToPixels(lngOldTwips, 0)End Function				

Article ID: 210590 - Last Review: 12/05/2015 11:34:58 - Revision: 2.0

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbinfo kbprogramming KB210590