TreeView wrap text

Hi all,
I want the treeview control to word wrap the text in nodes. Any ideas?

Regards
abass_a_hajjAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PhilAICommented:
I don't think such a thing is possible, I'm afraid, but don't quote me on that ;o)
0
 
abass_a_hajjAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PhilAICommented:
Oh, I'm sorry, I didn't look through that question in that much detail...
0
 
abass_a_hajjAuthor Commented:
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
 
PhilAICommented:
You're probably right, but it takes time to write and test these things...
0
 
abass_a_hajjAuthor Commented:
okay, I'll be waiting then :)
0
 
danaseamanCommented:
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
 
abass_a_hajjAuthor Commented:
I'm not sure that I'll be able to translate this code to VB.
0
 
danaseamanConnect With a Mentor Commented:
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
 
abass_a_hajjAuthor Commented:
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
 
danaseamanCommented:
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
All Courses

From novice to tech pro — start learning today.