|
Microsoft Knowledge Base Article - Q210590
ACC2000: How to Convert Twips to Pixels
The information in this article applies to:
Microsoft Access 2000
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.
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).
Create a new module and type the following in the Declarations section:
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 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.
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) * lngPixelsPerInch
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:
Function ShowConvert()
Dim lngOldTwips As Long
lngOldTwips = 2377
ShowConvert = ConvertTwipsToPixels(lngOldTwips, 0)
End Function
First Published: Nov 25 1998 5:20AM
Keywords: kbProgramming kbdta kbinfo inf
Twips and other units
Twips are often used as screen units in ActiveX controls.
1 pica = 1/6 inch
1 point = 1/12 pica
1 twip = 1/20 point
1 twip = 1/567 centimeter
The number of twips per pixel depends on hardware. The next example calculates this for a display monitor (in the current resolution).
{windows.i}
/* 1 twip = 1/1440 inch */
&GLOB LOGPIXELSX 88
&GLOB LOGPIXELSY 90
DEFINE VARIABLE hdc AS INTEGER NO-UNDO.
DEFINE VARIABLE ReturnValue AS INTEGER NO-UNDO.
DEFINE VARIABLE PixelsPerInchX AS INTEGER NO-UNDO.
DEFINE VARIABLE PixelsPerInchY AS INTEGER NO-UNDO.
RUN GetDC IN hpApi (INPUT DEFAULT-WINDOW:HWND,
OUTPUT hdc).
RUN GetDeviceCaps IN hpApi (INPUT hdc,
INPUT {&LOGPIXELSX},
OUTPUT PixelsPerInchX).
RUN GetDeviceCaps IN hpApi (INPUT hdc,
INPUT {&LOGPIXELSY},
OUTPUT PixelsPerInchY).
RUN ReleaseDC IN hpApi (INPUT DEFAULT-WINDOW:HWND,
INPUT hdc,
|
|