Width of List Item in Pixels

M

mohit_ranka

Hi All,
I was wondering if there is a way to find out the Pixel
Width of ListBox Item in VBA. Currently I am finding the Length of the
string using Len(List(i) and multiply it by a constant (=4) to get the
approx. Pixel Width of the Item, but I would really appreciate if some
one knows how to get the length of the List Item Text in pixel.

Regards,
Mohit
 
J

Jean-Guy Marcil

mohit_ranka said:
Hi All,
I was wondering if there is a way to find out the Pixel
Width of ListBox Item in VBA. Currently I am finding the Length of the
string using Len(List(i) and multiply it by a constant (=4) to get the
approx. Pixel Width of the Item, but I would really appreciate if some
one knows how to get the length of the List Item Text in pixel.

Regards,
Mohit

There is no way I know of to get a listbox item width. You could, I guess,
create a temporary dummy document, insert the text from the list box using
the same font attributes and then calculate the pixel width like this:


Dim lngRangeStart As Long
Dim lngRangeEnd As Long
Dim sngPixels As Single
Dim rngCurrent As Range

Set rngCurrent = Selection.Range

With rngCurrent
lngRangeStart = ActiveDocument.Range(.Start, .Start) _
.Information(wdHorizontalPositionRelativeToPage)
lngRangeEnd = ActiveDocument.Range(.End, .End) _
.Information(wdHorizontalPositionRelativeToPage)
If ActiveDocument.Range(.Start, .Start) _
.Information(wdVerticalPositionRelativeToPage) <> _
ActiveDocument.Range(.End, .End) _
.Information(wdVerticalPositionRelativeToPage) Then
MsgBox "This procedure can only be used with a " _
& "single-line selection.", vbExclamation, "Cancelled"
Exit Sub
Else
sngPixels = PointsToPixels(lngRangeEnd - lngRangeStart)
End If
End With

With System
MsgBox "The selection is " & sngPixels & " pixels wide " _
& " at the current resolution of " & .HorizontalResolution _
& " x " & .VerticalResolution & ".", vbInformation, "Pixel width"
End With


Keep in mind that this is not scientifically accurate...

Another approach would be to use a proportional font (so that all characters
are of the same width), and then calculate (using code like I suggested
above), how much space one character occupies at the target font size at all
possible screen horizontal resolution (If you do this, use 20 characters and
divide by 20 to get a better sampling and check a few times to make sure you
get as an accurate result as is possible with this technique...). Store these
values as constants in your code.
Then your code would only need to determine the screen resolution in order
to choose the appropriate constant as a mutliplier for the number of
characters in the targeted listbox item.

But overall, why do you need this?
As soon as you start using pixels to determine on-screen size, you have to
take into account the screen resolution... It might get complicated,
depending on what it is you want to achieve...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top