Advertisement

07.28.2008 at 09:10PM PDT, ID: 23602878
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

How do I use DrawText to break a sentence without DT_WORDBREAK flag specified?

Asked by talltk in Windows MFC Programming, Windows Programming, Microsoft Visual C++

Tags:

Ok here's the scenario:

I have a custom combo box with Owner Draw being set to variable.  In my DrawItem Function, I used dc.DrawText() to draw the text in my combo box.  I have implemented the combo box to be of multiple lines depending on the length of text entered. The text added to the combo box is:

"The model of camera is: s1/s2/s3/s4/s5/s6/s7/s8/s8/s10/s11/s12"  

With DT_LEFT | DT_WORDBREAK flags specified, the above will appear as :

"The model of camera is:
s1/s2/s3/s4/s5/s6/s7/s8/s8/"

the models "s/10/s11/s12" is not displayed because  "s1/s2/s3/s4/s5/s6/s7/s8/s8/s10/s11/s12"  is considered as one word and the rest of the word is too long to be viewed in the combo box.  Extending the length of the combo box is not an option.  

I am looking for a solution so that the sentence can be cut at various places (even in between a word) so that the entire sentence can be displayed as one item of the combo box.  Something like this is my desirable output:

"The model of camera is:
s1/s2/s3/s4/s5/s6/s7/s
8/s8/s10/s11/s12"

[The model s8 is cut in between the s and the 8]

I don't think DT_WORDBREAK can be set if I want such an output.  Please advise.

I will provide my DrawItem and MeasureItem code for reference.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
void CMyBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	 ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);
 
	 // 1 - Get combobox item ID
	 UINT id = (UINT) (WORD) lpDrawItemStruct->itemID;
 
	 // Consistency checks
	 ASSERT(id == lpDrawItemStruct->itemID);
 
	 // 2 - Get device context & item rectangle
	 CDC dc;
	 dc.Attach(lpDrawItemStruct->hDC);
 
	 CRect rcItem = lpDrawItemStruct->rcItem;
	 int nCurrentX = rcItem.left;
 
	 // 3 - Select background and foreground
	 // colors depending on item selected state
	 COLORREF crBack, crFore;
	 if(lpDrawItemStruct->itemState & ODS_SELECTED)
	 {
		 crBack = ::GetSysColor(COLOR_HIGHLIGHT);
		 crFore = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
	 }
	 else
	 {
		 crBack = ::GetSysColor(COLOR_WINDOW);
		 crFore = ::GetSysColor(COLOR_WINDOWTEXT);
	 }
 
	 // 4 - Fill item background
	 CRect rcBack(nCurrentX, rcItem.top, rcItem.right, rcItem.bottom);
	 CBrush brFill(crBack);
	 dc.FillRect(&rcBack, &brFill);
	 
	 // 5 - Draw item color rectangle
	 CBrush brColor(::GetSysColor(COLOR_WINDOWTEXT));
	 CBrush* pOldBrush = dc.SelectObject(&brColor);
	 CRect rcColor(nCurrentX, rcItem.top, nCurrentX, rcItem.bottom);
	 dc.Rectangle(rcColor);
	 dc.SelectObject(pOldBrush);
 
	 // 6 - Draw item text
	 COLORREF crOldTextColor = dc.SetTextColor(crFore);
	 int nOldBkMode = dc.SetBkMode(TRANSPARENT);
	 CString str;
	 this->GetLBText(id, str);
	 dc.DrawText(str, -1, rcItem, DT_LEFT|DT_WORDBREAK);
	 dc.DrawTextEx
	 dc.SetTextColor(crOldTextColor);
	 dc.SetBkMode(nOldBkMode);
	 dc.Detach();
}
 
void CMyBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	/*DWORD dwStyle = ::GetWindowLong( m_hWnd, GWL_STYLE );*/
	/*ASSERT( dwStyle & CBS_DROPDOWNLIST );*/
 
	CFont* pFont = GetFont();
	CFont* pOldFont = 0;
	CString str;
	CDC* pDC = GetDC();
	pOldFont = pDC->SelectObject(pFont);
 
	// Get text from combo box
	this->GetLBText(lpMeasureItemStruct->itemID, str);
 
	// Obtain rectangle that text is originally in
	CRect rc;
	GetClientRect(&rc);
	//GetWindowRect(&rc);
	rc.left = 0;
	rc.top = 0;
	rc.bottom = 0;
 
	// With the inclusion of DT_CALCRECT
	// this DrawText does not draw the text
	// it just measures the rectangle
	pDC->DrawText(str, str.GetLength(), &rc, DT_CALCRECT|DT_LEFT|DT_WORDBREAK); 
	lpMeasureItemStruct->itemHeight = rc.bottom;
	pDC->SelectObject(pOldFont);
	ReleaseDC(pDC); 
}
 
 
[+][-]07.29.2008 at 02:43AM PDT, ID: 22109823

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.29.2008 at 06:13PM PDT, ID: 22116913

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.30.2008 at 03:51AM PDT, ID: 22119120

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Windows MFC Programming, Windows Programming, Microsoft Visual C++
Tags: C++ MFC
Sign Up Now!
Solution Provided By: alb66
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628