Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

API to get Twips per pixel ?

76 views
Skip to first unread message

John Contract

unread,
Apr 18, 2001, 8:34:17 AM4/18/01
to
Is there an API call to get the current twips per pixel setting on my system
?


Ian Williams

unread,
Apr 18, 2001, 9:48:54 AM4/18/01
to
Screen.TwipsPerPixel?

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/

"John Contract" <jcon...@bloomberg.net> wrote in message
news:3add89cc$0$99...@wodc7nh7.news.uu.net...

John Contract

unread,
Apr 18, 2001, 8:54:44 AM4/18/01
to
Need an API call Ian ... I'm working in VBA and don't have access to the
Screen object there ...

Ian Williams <adv...@kingsoft-dk.com> wrote in message
news:#xwAwXAyAHA.1340@tkmsftngp04...

Ian Williams

unread,
Apr 18, 2001, 10:27:36 AM4/18/01
to
John
you can sort out the declares yourself

Public Sub GetTwipsPerPixel()
Dim hdc As Long, lResult As Long
hdc = GetDC(0)
TwipsPerPixelX = 1440 / GetDeviceCaps(hdc, LOGPIXELSX)
TwipsPerPixelY = 1440 / GetDeviceCaps(hdc, LOGPIXELSY)
ReleaseDC 0, hdc
End Sub

regards

Ian

** invalid email address, change dk to denmark

"John Contract" <jcon...@bloomberg.net> wrote in message

news:3add8e98$0$74...@wodc7nh1.news.uu.net...

Mike D Sutton

unread,
Apr 18, 2001, 1:03:17 PM4/18/01
to
> Is there an API call to get the current twips per pixel setting on my
system

Use the screen object:

(Form1.Width \ Screen.TwipsPerPixelX) & ", " & _
(Form1.Height \ Screen.TwipsPerPixelY)

Hope this helps,

Mike


-- EDais --

WWW: Http://Members.xoom.com/EDais/
Work E-Mail: ED...@btclick.com
Other E-Mail: Mike....@btclick.com


Mike D Sutton

unread,
Apr 18, 2001, 1:24:38 PM4/18/01
to
> Need an API call Ian ... I'm working in VBA and don't have access to the
> Screen object there ...

Does it support the ScaleX() and ScaleY() functions?
If so then:

'***
FormWidth = ScaleX(Form1.Width, vbTwips, vbPixels)
FormHeight = ScaleX(Form1.Height, vbTwips, vbPixels)
'***

Otherwise take it as 15, I've never seen it as anything different. I think
there are API calls to do the conversion though, but I don't remember any
offhand. Maybe a quick search through the MSDN would be in order?

Bob Butler

unread,
Apr 18, 2001, 7:17:12 PM4/18/01
to

"Mike D Sutton" <Mike....@btclick.com> wrote in message
news:t5kD6.18203$986.27795@NewsReader...
<cut>

> Otherwise take it as 15, I've never seen it as anything different.

Go to Control Panel / Display / Settings and select Large Fonts.


Monte Hansen

unread,
Apr 18, 2001, 9:18:37 PM4/18/01
to
> Otherwise take it as 15, I've never seen it as anything different.
Bob is correct. That hardcode is evil.

> Does it support the ScaleX() and ScaleY() functions?

Please find the below functions which I wrote to emulate VB's ScaleX and
ScaleY functions. Hope they help.

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+

Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90

Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal
nIndex As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal
hdc As Long) As Long
Public Function LogicalToDeviceX(ByVal Width As Single, ByVal ScaleFrom As
ScaleModeConstants, Optional ByVal hdc As Long) As Single

'===========================================================================
====
' LogicalToDeviceX - Converts the supplied horizontal logical unit to
pixels.
'
' Width The value to convert to pixels.
' ScaleFrom The unit of measurement to scale from.
' hDC Optional. The device context that Width relates to.
'
' RETURNS The value scaled to pixels.
'===========================================================================
====

Dim LOGPIXELS As Long

' No error handler
On Error GoTo 0

' Use current meta device as a basic for the scale, if any,
' otherwise use the screen device.
If IsValidHandle(hdc) Then
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSX)
Else
' Get the screen device
hdc = GetDC(0&)
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSX)
' Release the screen
ReleaseDC 0, hdc
End If

' Now for perform the conversion
Select Case ScaleFrom
Case vbCentimeters
' Convert from metric; 2540 = himetric units per inch
LogicalToDeviceX = (Width * LOGPIXELS) / 2.54

Case vbMillimeters
' Convert from metric; 2540 = himetric units per inch
LogicalToDeviceX = (Width * LOGPIXELS) / 25.4

Case vbHimetric

' Convert from himetric; 2540 = himetric units per inch
LogicalToDeviceX = (Width * LOGPIXELS) / 2540

Case vbTwips
' Convert to twips; 1440 = twips per inch
LogicalToDeviceX = (Width * LOGPIXELS) / 1440

Case vbCharacters
' Convert to twips; 1 horizontal character = 120 twips per unit;
vertical = 240 twips per unit
LogicalToDeviceX = ((Width * LOGPIXELS) * 120) / 1440

Case vbInches
' Convert to inches; 1440 = twips per inch
LogicalToDeviceX = Width * LOGPIXELS

Case vbPoints
' Convert to points; 72 = points per inch
LogicalToDeviceX = ((Width * LOGPIXELS) * 20) / 1440

Case vbPixels
' No conversion needed for pixels
LogicalToDeviceX = Width

Case Else

Err.Raise 5, Module

End Select


ExitLabel:

Exit Function

End Function


Public Function LogicalToDeviceY(ByVal Height As Single, ByVal ScaleFrom As
ScaleModeConstants, Optional ByVal hdc As Long) As Single
'===========================================================================
====
' LogicalToDeviceY - Converts the supplied vertical logical unit to
pixels.
'
' Height The value to convert to pixels.
' ScaleFrom The unit of measurement to scale from.
' hDC Optional. The device context that Width relates to.
'
' RETURNS The value scaled to pixels.
'===========================================================================
====

Dim LOGPIXELS As Long

' No error handler
On Error GoTo 0

' Use current meta device as a basic for the scale, if any,
' otherwise use the screen device.
If IsValidHandle(hdc) Then
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSY)
Else
' Get the screen device
hdc = GetDC(0&)
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSY)
' Release the screen
ReleaseDC 0, hdc
End If

' Now for perform the conversion
Select Case ScaleFrom
Case vbCentimeters
' Convert from himetric; 2540 = himetric units per inch
LogicalToDeviceY = (Height * LOGPIXELS) / 2.54

Case vbMillimeters
' Convert from himetric; 2540 = himetric units per inch
LogicalToDeviceY = (Height * LOGPIXELS) / 25.4

Case vbHimetric

' Convert from himetric; 2540 = himetric units per inch
LogicalToDeviceY = (Height * LOGPIXELS) / 2540

Case vbTwips
' Convert to twips; 1440 = twips per inch
LogicalToDeviceY = (Height * LOGPIXELS) / 1440

Case vbCharacters
' Convert to twips; 1 horizontal character = 120 twips per unit;
vertical = 240 twips per unit
LogicalToDeviceY = ((Height * LOGPIXELS) * 240) / 1440

Case vbInches
' Convert to inches; 1440 = twips per inch
LogicalToDeviceY = Height * LOGPIXELS

Case vbPoints
' Convert to points; 72 = points per inch
LogicalToDeviceY = ((Height * LOGPIXELS) * 20) / 1440

Case vbPixels
' No conversion needed for pixels
LogicalToDeviceY = Height

Case Else

Err.Raise 5, Module

End Select


ExitLabel:

Exit Function

End Function

Public Function DeviceToLogicalX(ByVal Width As Single, ByVal ScaleTo As
ScaleModeConstants, Optional ByVal hdc As Long) As Single
'===========================================================================
====
' DeviceToLogicalX - Converts the supplied Width to the desired logical
unit.
'
' Width The value to convert from pixels.
' ScaleFrom The unit of measurement to scale from.
' hDC Optional. The device context that Width relates to.
'
' RETURNS The value scaled to pixels.
'===========================================================================
====

Dim LOGPIXELS As Long

' No error handler
On Error GoTo 0

' Use current meta device as a basic for the scale, if any,
' otherwise use the screen device.
If IsValidHandle(hdc) Then
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSX)
Else
' Get the screen device
hdc = GetDC(0&)
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSX)
' Release the screen
ReleaseDC 0, hdc
End If

' Now for perform the conversion
Select Case ScaleTo
Case vbCentimeters
' Convert to centimeters; 2540 = himetric units per inch
DeviceToLogicalX = (Width * 2.54) / LOGPIXELS

Case vbMillimeters
' Convert to millimeters; 2540 = himetric units per inch
DeviceToLogicalX = (Width * 25.4) / LOGPIXELS

Case vbHimetric

' Convert to himetric; 2540 = himetric units per inch
DeviceToLogicalX = (Width * 2540) / LOGPIXELS

Case vbTwips
' Convert from twips; 1440 = twips per inch
DeviceToLogicalX = (Width * 1440) / LOGPIXELS

Case vbCharacters
' Convert from twips; 1 horizontal character = 120 twips per unit;
vertical = 240 twips per unit
DeviceToLogicalX = ((Width * 1440) / LOGPIXELS) / 120

Case vbInches
' Convert from twips; 1440 = twips per inch
DeviceToLogicalX = Width / LOGPIXELS

Case vbPoints
' Convert from twips; 72 = points per inch
DeviceToLogicalX = ((Width * 1440) / LOGPIXELS) / 20

Case vbPixels
' No conversion needed for pixels
DeviceToLogicalX = Width

Case Else

Err.Raise 5, Module

End Select

ExitLabel:

Exit Function

End Function

Public Function DeviceToLogicalY(ByVal Height As Single, ByVal ScaleTo As
ScaleModeConstants, Optional ByVal hdc As Long) As Single
'===========================================================================
====
' DeviceToLogicalY - Converts the supplied Height to the desired logical
unit.
'
' Height The value to convert from pixels.
' ScaleFrom The unit of measurement to scale from.
' hDC Optional. The device context that Width relates to.
'
' RETURNS The value scaled to pixels.
'===========================================================================
====

Dim LOGPIXELS As Long

' No error handler
On Error GoTo 0

' Use current meta device as a basic for the scale, if any,
' otherwise use the screen device.
If IsValidHandle(hdc) Then
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSY)
Else
' Get the screen device
hdc = GetDC(0&)
' Get # of pixels per logical inch
LOGPIXELS = GetDeviceCaps(hdc, LOGPIXELSY)
' Release the screen
ReleaseDC 0, hdc
End If

' Now for perform the conversion
Select Case ScaleTo
Case vbCentimeters
' Convert to centimeters; 2540 = himetric units per inch
DeviceToLogicalY = (Height * 2.54) / LOGPIXELS

Case vbMillimeters
' Convert to millimeters; 2540 = himetric units per inch
DeviceToLogicalY = (Height * 25.4) / LOGPIXELS

Case vbHimetric

' Convert to himetric; 2540 = himetric units per inch
DeviceToLogicalY = ((Height * 2540) / LOGPIXELS)

Case vbTwips
' Convert from twips; 1440 = twips per inch
DeviceToLogicalY = (Height * 1440) / LOGPIXELS

Case vbCharacters
' Convert from twips; 1 horizontal character = 120 twips per unit;
vertical = 240 twips per unit
DeviceToLogicalY = ((Height * 1440) / LOGPIXELS) / 240

Case vbInches
' Convert from twips; 1440 = twips per inch
DeviceToLogicalY = Height / LOGPIXELS

Case vbPoints
' Convert from twips; 72 = points per inch
DeviceToLogicalY = ((Height * 1440) / LOGPIXELS) / 20

Case vbPixels
' No conversion needed for pixels
DeviceToLogicalY = Height

Case Else

Err.Raise 5, Module

End Select

ExitLabel:

Exit Function

End Function

Public Function IsValidHandle(ByVal Handle As Long) As Boolean

Const INVALID_HANDLE_VALUE = -1

Select Case Handle
Case 0, INVALID_HANDLE_VALUE
Case Else
IsValidHandle = True
End Select

End Function


"Mike D Sutton" <Mike....@btclick.com> wrote in message
news:t5kD6.18203$986.27795@NewsReader...

0 new messages