How to set image as background of TreeView widget item

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to set image as background of TreeView widget item

      Hi,
      I create an TreeView widget with nodes. I want to change TreeView Item images and set Text indent zero.
      When I use following code,

      Source Code

      1. TREEVIEW_SetImage(hItem,TREEVIEW_BI_OPEN,&bitmap200x20);
      2. TREEVIEW_SetTextIndent(hItem,0);
      TreeView item text disappears. So How can I bring TreeView item's text front of the TreeView item's bitmap ?

      P.S: I have reviewed all emWin User&Reference Guide. Document: UM03001. If I have miss related part of document. You can point document.

      Thanks in advance...

      Best Regards
      Emin ATEŞ
    • Hello,

      in case you set indentation of item text to 0 then text is covered by item images. I think it is because default drawing function first draws item text and after that it draws images.

      To change the order I think custom drawing function can be set where you should "do nothing" when handling text drawing command (WIDGET_ITEM_DRAW_TEXT) and sort of "repeat" text drawing after drawing item image when handling image drawing command (WIDGET_ITEM_DRAW_BITMAP). I think treeview items can be distinguished, say, by the y0 parameter of WIDGET_ITEM_DRAW_INFO structure passed to the custom drawing function.

      I'd do something like this:


      C Source Code: Main.c

      1. #include "DIALOG.h"
      2. TREEVIEW_Handle hTreeview;
      3. TREEVIEW_ITEM_Handle hTreeItemCur, hTreeItemNew;
      4. TREEVIEW_ITEM_Handle phItem[3];
      5. // Custom drawing function for the TREEVIEW widget
      6. static int _OwnerDraw_TREEVIEW(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
      7. U8 pBuffer[100];
      8. U8 Index;
      9. TREEVIEW_ITEM_INFO pInfo;
      10. switch (pDrawItemInfo->Cmd) {
      11. case WIDGET_ITEM_DRAW_TEXT:
      12. // Make sure to clear item rect instead of drawing the text
      13. GUI_SetBkColor(GUI_WHITE);
      14. GUI_Clear();
      15. break;
      16. case WIDGET_ITEM_DRAW_BITMAP:
      17. // Call default drawing function to draw the image
      18. TREEVIEW_OwnerDraw(pDrawItemInfo);
      19. // Calc the Index to get the item handler from the list. Divide y0 by the item height.
      20. Index = pDrawItemInfo->y0 / 16;
      21. // Get the item text and level in the treeview
      22. TREEVIEW_ITEM_GetText(phItem[Index], pBuffer, 100);
      23. TREEVIEW_ITEM_GetInfo(phItem[Index], &pInfo);
      24. if (TREEVIEW_GetSel(pDrawItemInfo->hWin) == phItem[Index])
      25. {
      26. // Set the text attributes, selected item
      27. GUI_SetBkColor(GUI_BLUE);
      28. GUI_SetColor(GUI_WHITE);
      29. GUI_SetTextMode(GUI_TM_NORMAL);
      30. }
      31. else
      32. {
      33. // Set the text attributes, unselected item
      34. GUI_SetColor(GUI_BLACK);
      35. GUI_SetTextMode(GUI_TM_TRANS);
      36. }
      37. // Draw the text over the image
      38. GUI_DispStringAt(pBuffer, pDrawItemInfo->x0 + 20 + pInfo.Level * 15, pDrawItemInfo->y0);
      39. break;
      40. default:
      41. return TREEVIEW_OwnerDraw(pDrawItemInfo);
      42. break;
      43. }
      44. return 0;
      45. }
      46. void MainTask(void) {
      47. GUI_Init();
      48. WM_MULTIBUF_Enable(1);
      49. GUI_SetBkColor(GUI_LIGHTGRAY);
      50. GUI_Clear();
      51. // Create a TREEVIEW widget
      52. hTreeview = TREEVIEW_CreateEx(60, 30, 200, 100, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_TREEVIEW0);
      53. // Create the root item
      54. hTreeItemCur = TREEVIEW_GetItem(hTreeview, 0, TREEVIEW_GET_LAST);
      55. hTreeItemNew = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_NODE, "Root", 0);
      56. TREEVIEW_AttachItem(hTreeview, hTreeItemNew, hTreeItemCur, TREEVIEW_INSERT_BELOW);
      57. // Add item hanlder to the list
      58. phItem[0] = hTreeItemNew;
      59. // Create another two child items and add the handlers to the list
      60. hTreeItemCur = hTreeItemNew;
      61. hTreeItemNew = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_LEAF, "Item 1", 0);
      62. TREEVIEW_AttachItem(hTreeview, hTreeItemNew, hTreeItemCur, TREEVIEW_INSERT_FIRST_CHILD);
      63. phItem[1] = hTreeItemNew;
      64. hTreeItemCur = hTreeItemNew;
      65. hTreeItemNew = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_LEAF, "Item 2", 0);
      66. TREEVIEW_AttachItem(hTreeview, hTreeItemNew, hTreeItemCur, TREEVIEW_INSERT_BELOW);
      67. phItem[2] = hTreeItemNew;
      68. // Set text indentation to 0.
      69. TREEVIEW_SetTextIndent(hTreeview, 0);
      70. // Set custom drawing function
      71. TREEVIEW_SetOwnerDraw(hTreeview, _OwnerDraw_TREEVIEW);
      72. while(1)
      73. {
      74. GUI_Delay(50);
      75. }
      76. }
      Display All
      Works in my simulation (STemWin v5.32). Some parameters (item height, font, selection colors...) may vary depending on your code.

      Alex.

      The post was edited 1 time, last by LexaGb ().