Wrapping text within a CListBox
-
Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!
-
Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!
i dont think you can wrap the text in a listbox onto more than one line ... thats why they gave it a horiz scroll bar and info tips / tool tips someone might know better than i on this --- "every year we invent better idiot proof systems and every year they invent better idiots"
-
Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!
The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.
-
The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.
In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text.
You have to use the flag DT_CALCRECT. MSDN says:
DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.
HTH, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.
Or you could make it customdraw. I think there's an article or 2 here that addresses customdraw.
-
The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.
Cheers for that Guys! My problem now is getting the text to wrap! I have got the text/background size correct and can change the text/background colour when the item is selected, and also add the focus rectangle no probs... just don't know how to make the text I'm adding wrap round properly so it is not clipped Any more ideas?