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
Solved

TreeView wrap text

Posted on 2004-10-03
15
855 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

840 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