ACC: How to Convert Twips to Pixels (94927)



The information in this article applies to:

  • Microsoft Access 1.0
  • Microsoft Access 1.1
  • Microsoft Access 2.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97
  • Microsoft Visual Basic for Applications 1.0

This article was previously published under Q94927

SUMMARY

Advanced: Requires expert coding, interoperability, and multi-user skills.

Because Microsoft Access stores dimension and location properties in twips, you may sometimes need 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.

MORE INFORMATION

You can use the TwipsPerPixelX() and TwipsPerPixelY() functions below to find the dimensions of a pixel in twips. Pixel dimensions can vary between systems and may not always be square, so separate functions for pixel width and height are required.

To add these functions to your application, copy one of the following blocks of code into a new module.

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 declaration statement in your code.

In Microsoft Access 7.0 and 97

NOTE: Place this code in a standard module, not a class module.
Option Explicit

Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _
  ByVal hdc As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
  ByVal nIndex As Long) As Long

Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function
				
To use these functions, divide the number of twips that you want to convert by either TwipsPerPixelX() for horizontal measurement or TwipsPerPixelY() for vertical measurement. The following is an example:
OldTwips = 2377
NewPixels = OldTwips / TwipsPerPixelX()
				

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 hwnd As Integer) As Integer
Declare Sub ReleaseDC Lib "User" (ByVal hwnd As Integer, _
  ByVal hdc As Integer)
Declare Function GetDeviceCaps Lib "Gdi" (ByVal hdc As Integer, _
  ByVal nIndex As Integer) As Integer

Const HWND_DESKTOP = 0
Const LOGPIXELSX = 88
Const LOGPIXELSY = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim intDC As Integer
  intDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440 / GetDeviceCaps(intDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, intDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim intDC As Integer
  intDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440 / GetDeviceCaps(intDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, intDC
End Function
				
To use these functions, divide the number of twips that you want to convert by either TwipsPerPixelX() for horizontal measurement or TwipsPerPixelY() for vertical measurement. The following is an example:
OldTwips = 2377
NewPixels = OldTwips / TwipsPerPixelX()
				

REFERENCES

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

Modification Type:MinorLast Reviewed:8/15/2005
Keywords:kbhowto kbProgramming KB94927