|
|

Twips and Pixels
What are Twips and Pixels?
Twips are screen-independent units to ensure that the proportion of screen elements are the same on all display systems. A twip is defined as being 1/1440 of an inch.
A Pixel is a screen-dependent unit, standing for 'picture element'. A pixel is a dot that represents the smallest graphical measurement on a screen.
Why do we need to convert between the two?
Within Access, screen elements are measured in twips, so if you get the width of a form (using its .WindowWidth property) it will be returned in Twips, for example 14205. However, most of the Win32 API calls, such as GetWindowPlacement, will use pixels, and this will return something like 947 for the width of the same form.
How do we convert between the two?
As you will have seen above, Access and Windows often use different measurements for measuring objects on the screen, and Pixels are dependent on the screen resolution, and are not necessarily square. Therefore, you need to be able to convert between the two units as needed. Here is some code that I use, adapted from article Q94927 on the Microsoft Knowledge Base.
Option Compare Database
Option Explicit
Private Declare Function apiGetDC Lib "user32" Alias "GetDC" _
(ByVal hwnd As Long) As Long
Private Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" _
(ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" _
(ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Public Const DIRECTION_VERTICAL = 1
Public Const DIRECTION_HORIZONTAL = 0
Function fTwipsToPixels(lngTwips As Long, lngDirection As Long) As Long
On Error GoTo E_Handle
Dim lngDeviceHandle As Long
Dim lngPixelsPerInch As Long
lngDeviceHandle = apiGetDC(0)
If lngDirection = DIRECTION_HORIZONTAL Then
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSX)
Else
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSY)
End If
lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
fTwipsToPixels = lngTwips / 1440 * lngPixelsPerInch
fExit:
On Error Resume Next
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
Function fPixelsToTwips(lngPixels As Long, lngDirection As Long) As Long
On Error GoTo E_Handle
Dim lngDeviceHandle As Long
Dim lngPixelsPerInch As Long
lngDeviceHandle = apiGetDC(0)
If lngDirection = DIRECTION_HORIZONTAL Then
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSX)
Else
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSY)
End If
lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
fPixelsToTwips = lngPixels * 1440 / lngPixelsPerInch
fExit:
On Error Resume Next
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
|
Top
HOME |
NEW |
TABLES |
QUERIES |
FORMS |
REPORTS |
GENERAL |
API |
DOWNLOADS |
TUTORIAL |
RESOURCES
E-MAIL
Copyright & Disclaimer
|
| |