Solved

TreeView wrap text

Posted on 2004-10-03
15
864 Views
Last Modified: 2013-12-26
Hi all,
I want the treeview control to word wrap the text in nodes. Any ideas?

Regards
0
Comment
Question by:abass_a_hajj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
15 Comments
 
LVL 6

Expert Comment

by:PhilAI
ID: 12216713
I don't think such a thing is possible, I'm afraid, but don't quote me on that ;o)
0
 
LVL 6

Accepted Solution

by:
PhilAI earned 250 total points
ID: 12216796
0
 

Author Comment

by:abass_a_hajj
ID: 12216861
Thank you PhilAI, as you might have noticed that I have gone through that thread before I asked this question and I posted a request there.

Regards
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 6

Expert Comment

by:PhilAI
ID: 12217001
Oh, I'm sorry, I didn't look through that question in that much detail...
0
 

Author Comment

by:abass_a_hajj
ID: 12265988
What's wrong with you guys.... I'm sure that a number of the experts on this site have the solution. All I need is to make the length of text in the nodes of a treeview control is limited to a certain width. If the text is longer than this width, I want the node to wrap the text on the next line and so on...

Regards
0
 
LVL 6

Expert Comment

by:PhilAI
ID: 12266011
You're probably right, but it takes time to write and test these things...
0
 

Author Comment

by:abass_a_hajj
ID: 12269570
okay, I'll be waiting then :)
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 12273854
To do this you need to subclass the Treeview control or better yet use a Vb created Treeview from VbAccelerator.com. You then need to implement CustomDraw using CDDS_PREPAINT, CDDS_ITEMPREPAINT, CDDS_ITEMPOSTPAINT, etc. Below is a snippet from http://support.microsoft.com/default.aspx?scid=kb;EN-US;q248496 which should give you an idea. It is in C++ but the code is easily adaptable to Vb. Note the DT_LEFT flag in call to CDDS_ITEMPOSTPAINT/DrawText should be changed to DT_WORDBREAK. You will also probably want to increase the item node height using API SendMessage and TVM_SETITEMHEIGHT. To prevent control from rendering internally you use CDRF_SKIPDEFAULT instead of CDRF_DODEFAULT.

Other examples of how to CustomDraw using Vb can be found on PlanetSourceCode and VbAccelerator sites. Also see http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/custdraw/custdraw.asp and http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/custdraw/samples/customdrawlistview.asp

long handleCustomDraw(HWND hWndTreeView, LPNMTVCUSTOMDRAW pNMTVCD)
{
   if (pNMTVCD==NULL)
   {
      return -1;
   }
   switch (pNMTVCD->nmcd.dwDrawStage)
   {
      case CDDS_PREPAINT:
         return (CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYITEMDRAW);
      case CDDS_ITEMPREPAINT:
         {

            HTREEITEM hItem = (HTREEITEM) pNMTVCD->nmcd.dwItemSpec;
            TVITEM tvi = { 0 };
            tvi.mask = TVIF_HANDLE | TVIF_PARAM;
            tvi.hItem = hItem;
            TreeView_GetItem(hWndTreeView, &tvi);
            if (tvi.lParam)
            {
               CItemData *pItemData = (CItemData *) tvi.lParam;
               if (pItemData)
               {
                  if (pItemData->m_itemType == ITEM_COLORS)
                  {
                     if ((pNMTVCD->nmcd.uItemState & CDIS_FOCUS) == CDIS_FOCUS)
                     {

                        HFONT hFont = (HFONT) ::SendMessage(hWndTreeView, WM_GETFONT, 0, 0);
                       
                        LOGFONT lf = { 0 };
                        GetObject(hFont, sizeof(LOGFONT), &lf);

                        lf.lfWeight |= FW_BOLD;
                       
                        HFONT hFontBold = CreateFontIndirect(&lf);
                        HFONT hOldFont = (HFONT) SelectObject(pNMTVCD->nmcd.hdc, hFontBold);

                        DeleteObject(hFontBold);
                     }
                     else
                     {
                        pNMTVCD->clrText = pItemData->m_itemData.clrData.clr;
                     }
                  }
                  else if (pItemData->m_itemType == ITEM_FONTS)
                  {
                     SelectObject(pNMTVCD->nmcd.hdc, pItemData->m_itemData.fontData.hFont);
                  }
               }
            }
            return (CDRF_NOTIFYPOSTPAINT | CDRF_NEWFONT);
         }
      case CDDS_ITEMPOSTPAINT:
         {
            RECT rc;
            TreeView_GetItemRect(hWndTreeView, (HTREEITEM) pNMTVCD->nmcd.dwItemSpec, &rc, 1);

            int temp = rc.left;
            //rc.top += 1;
            rc.left = rc.right + GAP_SIZE;
            rc.right += temp + rc.right + GAP_SIZE;

           
            TCHAR szFace[LF_FACESIZE];
            HTREEITEM hItem = (HTREEITEM) pNMTVCD->nmcd.dwItemSpec;
            TVITEM tvi = { 0 };
            tvi.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT;
            tvi.hItem = hItem;
            tvi.pszText = szFace;
            tvi.cchTextMax = sizeof(szFace)/sizeof(TCHAR);

            TreeView_GetItem(hWndTreeView, &tvi);
            if (tvi.lParam)
            {
               CItemData *pItemData = (CItemData *) tvi.lParam;
               if (pItemData)
               {
                  if (pItemData->m_itemType == ITEM_COLORS)
                  {
                     if ((pNMTVCD->nmcd.uItemState & CDIS_FOCUS) == CDIS_FOCUS)
                     {
                        // Draw the Color Text
                        int nOldClr = SetTextColor(pNMTVCD->nmcd.hdc, RGB(0,0,255));
                        DrawText(pNMTVCD->nmcd.hdc, _szClrTag[pItemData->m_itemData.clrData.clrTag], -1, &rc, DT_LEFT);
                        SetTextColor(pNMTVCD->nmcd.hdc, nOldClr);
                     }
                     else
                     {
                        // Erase the Color Text drawn previously
                        FillRect(pNMTVCD->nmcd.hdc, &rc, (HBRUSH) (COLOR_WINDOW+1));
                     }
                  }
                  else if (pItemData->m_itemType == ITEM_FONTS)
                  {
                  }
               }
            }
            return CDRF_DODEFAULT;
         }
         break;
      default:
         break;
   }
   return 0;
}
0
 

Author Comment

by:abass_a_hajj
ID: 12310064
I'm not sure that I'll be able to translate this code to VB.
0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 250 total points
ID: 12310587
VbAccelerator Treeview has CustomDraw implemented in Vb. See http://www.vbaccelerator.com/home/VB/Code/Controls/TreeView/TreeView_Control/article.asp In the code you will need to change this line:
DrawText NMTVCD.NMCD.hdc, "(" & CStr(lNumber) & ")", -1, rc, DT_LEFT
'to
DrawText NMTVCD.NMCD.hdc, "(" & CStr(lNumber) & ")", -1, rc, DT_WORDBREAK

Use Property ItemHeight to increase the node height so that yuou have space for multiple lines.
0
 

Author Comment

by:abass_a_hajj
ID: 12456151
Hi danaseaman,
I have seen this control before. The problem was that it doesn't support right to left layout. Do you know how to make it read from right to left. I have tried with API functions but it didn't work.

Regards
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 12456162
Or WS_EX_LAYOUTRTL with lExStyle when you create the control

Private Const WS_EX_LAYOUTRTL = &H400000

   lExStyle = lExStyle Or WS_EX_LAYOUTRTL

   GetWindowRect UserControl.hwnd, tR
   m_hWnd = CreateWindowEx(lExStyle, _
      WC_TREEVIEW, "", _
      lStyle, 0, 0, tR.right - tR.left, tR.bottom - tR.top, _
      UserControl.hwnd, 0, App.hInstance, 0)
   

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month7 days, 4 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question