ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to initialize the column width from pixels. Pixel to Char conversion (https://www.excelbanter.com/excel-programming/359300-how-initialize-column-width-pixels-pixel-char-conversion.html)

[email protected]

How to initialize the column width from pixels. Pixel to Char conversion
 
Hi,

Programmatically I try to initialize the column width from a pixel
value I have. I found a conversion function but doesn't function quite
accurate. In below code, GetScreenXPI returns 96 on my computer. My
default width is 8.43 char (64 pixel). PioxToCharX in this case would
return 8 char for 64 pixels. What am I missing? Any help would be
appreciated.

Reto
www.xcellery.com

' Conversion code
'*********************

Public Const TWIPS_PER_INCH = 1440 ' number TWIP units per
inch
Public Const TWIPS_PER_CHAR_X = 120 ' number TWIP units per
logical character

Public Function PixToCharX(ByVal pixels As Long) As Long
PixToCharX = ((pixels / GetScreenXDPI) * TWIPS_PER_INCH) /
TWIPS_PER_CHAR_X
End Function

Function GetScreenXDPI() As Long
Dim hdc As Long
hdc = CreateICA("DISPLAY", vbNullString, vbNullString, 0)
If (hdc < 0) Then
GetScreenXDPI = GetDeviceCaps(hdc, 88) 'screen res x
DeleteDC (hdc)
End If
End Function



All times are GMT +1. The time now is 07:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com