Announcement

Collapse
No announcement yet.

fitting popup listbox to data, font, width, height

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    fitting popup listbox to data, font, width, height

    I'm using a popup listbox (not ownerdrawn, and using LBS_HASSTRINGS style)which should be created the right size first time. So far I have managed to avoid doing this sort of thing! The character and number of rows is easy to determine, but to get the correct dimensions in pixels seems a bit trickier. It looks as if the following steps are required:

    1. Get the font used by the listbox control
    2. Get the DC of the listbox window
    3. Use drawtext with DT_CALCRECT to get the content width required
    4. Add the margin dimensions from GETSYSTEMMETRICS to give the overall width
    5. Use the LB_GETITEMHEIGHT message to get the item height, multiply by the number of items in the list
    6. Add vertical margin widths from GETSYSTEMMETRICS to give the overall height

    This gets the maximum width and height required - quite possibly, too large to display, so constraints are applied.

    *** update*** - don't need the font - (step 1), as it is the default font in the DC (step 2)

    The procedure seems rather longwinded - is there a simpler method?
    Last edited by Chris Holbrook; 4 Jan 2009, 08:33 AM.

    #2
    Chris, I found a post in poffs where John Kovacich was stating that the listbox changed to fit the font. I don't know if it will be of use to you but the
    title of the post was 'Changing text size of listbox alters box size'.
    Client Writeup for the CPA

    buffs.proboards2.com

    Links Page

    Comment


      #3
      Thanks Fred, it (well Chris Boss actually) suggested using %LBS_NONINTEGRALHEIGHT to prevent the listbox autosizing on change of font.

      The problem is not quite as simple as stated. The listbox has to exist in order to get its DC, so I will have to create it, hide it, measure the text in it, destroy it, then create another one with the correct dimensions.

      Comment


        #4
        A one-column (subitem) header-less listview control might be a bit easier to control size-wise and it sure looks a lot like a listbox control.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


          #5
          Originally posted by Michael Mattias View Post
          A one-column (subitem) header-less listview control might ...
          next time, maybe. I have something which works, along the lines of:

          Code:
                     hListLB = CreateWindowEx( 0,"LISTBOX",sz, _
                               %ws_child or %ws_visible or %ws_border or %ws_tabstop or _
                               %ws_vscroll or %lbs_notify or %lbs_hasstrings, _
                               X, Y, 100, 100, _
                               hdlg, %IDC_LBSUBITEM, getmodulehandle(byval 0), byval %NULL)
          
                     ' get the approx dimensions of the listbox data in n (count) and w(char width)
                     for i = 1 to parsecount(sListBoxData)
                         sz = parse$(sListBoxData, i)
                         w = max(w, len(sz))
                         incr n
                     next
                     sz = string$(w, "n") ' max width
                     ' Get the DC of the listbox window
                     hDC = getDC(hListLB)
                     ' Use drawtext with DT_CALCRECT to get the content width required
                     drawtext ( hDC, byval varptr(sz), byval -1, r, %DT_CALCRECT)
                     releasedc ( hListLB, hDC)
                     w = r.nright - r.nleft
                     h = (r.nbottom - r.ntop) * (n + 1)
                     destroywindow (hListLB)
                     hListLB = CreateWindowEx( 0,"LISTBOX",sz, _
                               %ws_child or %ws_visible or %ws_border or %ws_tabstop or _
                               %ws_vscroll or %lbs_notify or %lbs_hasstrings, _
                               X, Y, w, h,_
                               hdlg, %IDC_LBSUBITEM, getmodulehandle(byval 0), byval %NULL)
          It still seems a long way around.

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎