problem with horizontal scrolling in a simple dialogue window

problem with horizontal scrolling in a simple dialogue window

I'd really appreciate it if someone could give me a tip with making a dialog box in which there is a list box capable of horizontal scrolling (if the text is longer than the window width). There is no problem with the vertical scroll working, but even with the "horizontal scroll" checked off, the latter is not working. Is there some other thing I need to do in order to activate horizontal scrolling? Thanks alot...Rob
p.s. I've attached a small program that demonstrates the problem.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As MS would say, "This feature is by design". (Read: The design was very stupid, but now we mustn't change it because it would break existing programs). To make long story short, a list box, even with WS_HSCROLL style set, does not adjust horizontal scrollbar automatically. You have to send it a LB_SETHORIZONTALEXTENT message:


INTEGER::       maxLen, hDC, hFont, hOldFont, i, j

hDC = GetDC(GetDlgItem(Dlg%hWnd, IDC_LIST1))
hFont = GetStockObject(DEFAULT_GUI_FONT)
hOldFont = SelectObject(hDC, hFont)
DO i=1,nItems
   j = GetTextExtentPoint32(hDC, sItem(i), LEN_TRIM(sItem(i)), PT)
   maxLen = MAX(maxLen, PT%cx)
j = SendMessage(GetDlgItem(Dlg%hWnd, IDC_LIST1), &
j = SelectObject(hDC, hOldFont)
j = ReleaseDC(hDC)

sItem() above are strings from the list. Of course, you can retrieve them using DlgGet. The code must be placed in a callback function; best, define a dialog-initialization callback (DlgSetSub(Dlg, IDD_MYDIALOG, OnDlgInit)) which is called immediately after DlgModal.

See Russ Freeman's home page for detailed discussion and a general solution (but it's in C, so it might not be applicable in your case).



Thanks a lot. I'll look forward to trying this out.....Rob

For the start, first parameter of SendMessage is wrong -- you should send it to a HWND (obtained by GetDlgItem(hDlg, IDC_LOADTSERIES)), not to HDC.

Second, the length (of the scrollbar) is measured in pixels, not in characters (259) -- that's the whole point of this mess, i.e. that's why GetDC/SelectObject/GetTextExtentPoint32/ReleaseDC are required. Thus, you have to measure length, in pixels, for every string you add into listbox, find the maximal length, and do LB_SETHORIZONTALEXTENT for it.

Third, why do you create a hfontDlg at all? (Maybe it's just because you snipped the code down?).



Leave a Comment

Please sign in to add a comment. Not a member? Join today