Advertisement
Guest User

Win32 TreeView function (SysTreeView32)

a guest
May 1st, 2018
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. public class TreeView
  2.             {
  3.                 //http://www.perlmonks.org/bare/?node_id=478016
  4.                 //https://stackoverflow.com/questions/3660556/how-to-select-an-item-in-a-treeview-using-win32-api
  5.                 //https://bytes.com/topic/c-sharp/answers/621199-tvm_getitem-does-not-return-unicode-label
  6.  
  7.                 //Define TreeView Flags and Messages
  8.                 private const int BN_CLICKED = 0xF5;
  9.                 private const int TV_FIRST = 0x1100;
  10.                 private const int TVGN_ROOT = 0x0;
  11.                 private const int TVGN_NEXT = 0x1;
  12.                 private const int TVGN_CHILD = 0x4;
  13.                 private const int TVGN_FIRSTVISIBLE = 0x5;
  14.                 private const int TVGN_NEXTVISIBLE = 0x6;
  15.                 private const int TVGN_CARET = 0x9;
  16.                 private const int TVM_SELECTITEM = (TV_FIRST + 11);
  17.                 private const int TVM_GETNEXTITEM = (TV_FIRST + 10);
  18.                 private const int TVM_GETITEM = (TV_FIRST + 12);
  19.                 private const int TVIF_TEXT = 0x1;
  20.                 private const int MY_MAXLVITEMTEXT = 128;
  21.  
  22.                 [DllImport("user32", EntryPoint = "SendMessageW")]
  23.                 private static extern int SendMessageTVI(IntPtr hWnd, int wMsg, int wParam, ref TVITEM tvi);
  24.  
  25.                 [DllImport("kernel32.dll")]
  26.                 public static extern IntPtr LocalAlloc(uint flags, uint cb);
  27.  
  28.                 [DllImport("kernel32.dll")]
  29.                 public static extern IntPtr LocalFree(IntPtr p);
  30.  
  31.                 struct TVITEM
  32.                 {
  33.                     public uint mask;
  34.                     public IntPtr hItem;
  35.                     public uint state;
  36.                     public uint stateMask;
  37.                     public IntPtr pszText;
  38.                     public int cchTextMax;
  39.                     public int iImage;
  40.                     public int iSelectedImage;
  41.                     public int cChildren;
  42.                     public IntPtr lParam;
  43.                 }
  44.  
  45.                 [DllImport("user32.dll", CharSet = CharSet.Auto)]
  46.                 public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
  47.                 public static IntPtr GetRoot(IntPtr treeViewHwnd)
  48.                 {
  49.                     return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_ROOT, IntPtr.Zero));
  50.                 }
  51.                 public static IntPtr GetNextItem(IntPtr treeViewHwnd, IntPtr lastItem)
  52.                 {
  53.                     return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_NEXT, lastItem));
  54.                 }
  55.                 public static IntPtr GetChildItem(IntPtr treeViewHwnd, IntPtr parrentItem)
  56.                 {
  57.                     return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_CHILD, parrentItem));
  58.                 }
  59.                 public static void SelectItem(IntPtr treeViewHwnd, IntPtr item)
  60.                 {
  61.                     SendMessage((int)treeViewHwnd, TVM_SELECTITEM, TVGN_CARET, item);
  62.                 }
  63.                 public static IntPtr GetSelectedItem(IntPtr treeViewHwnd)
  64.                 {
  65.                     return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETITEM, TVGN_CARET, IntPtr.Zero));
  66.                 }
  67.  
  68.                 public static string GetTreeItemText(IntPtr treeViewHwnd, IntPtr hItem)
  69.                 {
  70.                     int ret;
  71.                     TVITEM tvi = new TVITEM();
  72.                     IntPtr pszText = LocalAlloc(0x40, MY_MAXLVITEMTEXT);
  73.  
  74.                     tvi.mask = TVIF_TEXT;
  75.                     tvi.hItem = hItem;
  76.                     tvi.cchTextMax = MY_MAXLVITEMTEXT;
  77.                     tvi.pszText = pszText;
  78.  
  79.                     ret = SendMessageTVI(treeViewHwnd, TVM_GETITEM, 0, ref tvi);
  80.                     string buffer = Marshal.PtrToStringUni((IntPtr)tvi.pszText,
  81.                     MY_MAXLVITEMTEXT);
  82.  
  83.                     //char[] arr = buffer.ToCharArray(); //<== use this array to look at the bytes
  84. //in debug mode
  85.  
  86. LocalFree(pszText);
  87.                     return buffer;
  88.                 }
  89.  
  90.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement