Results 1 to 4 of 4

Thread: String Length

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    String Length

    How do you get a strings width in pixels? like textwidth method on a picturebox, but with out a picturebox or form, and in pixels.

  2. #2
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    I can only give you some hints: create a DC using CreateCompatibleDC, use SelectObject to select the font you want to measure (create the font with CreateFont or CreateFontIndirect), use DrawText with the DT_CALCRECT flag to calculate the size of a string in pixels, use the return value of the first SelectObject in another call to SelectObject, and use that return value in a call to DeleteObject (SelectObject returns the previous object, in this case our font) then use DeleteDC to release the device context...

    Don't know the exact way to do it in VB though, but at least you've got a starting point now



    Oh, and try to keep de CreateCompatibleDC calls to a minimum, if you want to measure another string using the same font (or even another one) reuse the old DC, it will save you some execution time (if you need it for a game )
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

  3. #3
    Si_the_geek
    Guest
    two ways, use the printer textwidth (but don't actually print anything)...

    or: you can use the API function GetTextExtentPoint32, but you need a DC first:

    Call GetTextExtentPoint32(hDC, Text, Len(Text), sz)
    TextLength = sz.cx

    I think you need to multiply it by 16 or something (maybe screen.twipsperpixelx)

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    sz contains the size in pixels, so screen.twipsperpixel would be ok.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width