Microsoft KB Archive/94927

= ACC: How to Convert Twips to Pixels =

Article ID: 94927

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



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 &quot;user32&quot; (ByVal hwnd As Long) As Long Declare Function ReleaseDC Lib &quot;user32&quot; (ByVal hwnd As Long, _ ByVal hdc As Long) As Long Declare Function GetDeviceCaps Lib &quot;gdi32&quot; (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 &quot;User&quot; (ByVal hwnd As Integer) As Integer Declare Sub ReleaseDC Lib &quot;User&quot; (ByVal hwnd As Integer, _ ByVal hdc As Integer) Declare Function GetDeviceCaps Lib &quot;Gdi&quot; (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

