Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TreeView
- {
- //http://www.perlmonks.org/bare/?node_id=478016
- //https://stackoverflow.com/questions/3660556/how-to-select-an-item-in-a-treeview-using-win32-api
- //https://bytes.com/topic/c-sharp/answers/621199-tvm_getitem-does-not-return-unicode-label
- //Define TreeView Flags and Messages
- private const int BN_CLICKED = 0xF5;
- private const int TV_FIRST = 0x1100;
- private const int TVGN_ROOT = 0x0;
- private const int TVGN_NEXT = 0x1;
- private const int TVGN_CHILD = 0x4;
- private const int TVGN_FIRSTVISIBLE = 0x5;
- private const int TVGN_NEXTVISIBLE = 0x6;
- private const int TVGN_CARET = 0x9;
- private const int TVM_SELECTITEM = (TV_FIRST + 11);
- private const int TVM_GETNEXTITEM = (TV_FIRST + 10);
- private const int TVM_GETITEM = (TV_FIRST + 12);
- private const int TVIF_TEXT = 0x1;
- private const int MY_MAXLVITEMTEXT = 128;
- [DllImport("user32", EntryPoint = "SendMessageW")]
- private static extern int SendMessageTVI(IntPtr hWnd, int wMsg, int wParam, ref TVITEM tvi);
- [DllImport("kernel32.dll")]
- public static extern IntPtr LocalAlloc(uint flags, uint cb);
- [DllImport("kernel32.dll")]
- public static extern IntPtr LocalFree(IntPtr p);
- struct TVITEM
- {
- public uint mask;
- public IntPtr hItem;
- public uint state;
- public uint stateMask;
- public IntPtr pszText;
- public int cchTextMax;
- public int iImage;
- public int iSelectedImage;
- public int cChildren;
- public IntPtr lParam;
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
- public static IntPtr GetRoot(IntPtr treeViewHwnd)
- {
- return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_ROOT, IntPtr.Zero));
- }
- public static IntPtr GetNextItem(IntPtr treeViewHwnd, IntPtr lastItem)
- {
- return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_NEXT, lastItem));
- }
- public static IntPtr GetChildItem(IntPtr treeViewHwnd, IntPtr parrentItem)
- {
- return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETNEXTITEM, TVGN_CHILD, parrentItem));
- }
- public static void SelectItem(IntPtr treeViewHwnd, IntPtr item)
- {
- SendMessage((int)treeViewHwnd, TVM_SELECTITEM, TVGN_CARET, item);
- }
- public static IntPtr GetSelectedItem(IntPtr treeViewHwnd)
- {
- return new IntPtr(SendMessage((int)treeViewHwnd, TVM_GETITEM, TVGN_CARET, IntPtr.Zero));
- }
- public static string GetTreeItemText(IntPtr treeViewHwnd, IntPtr hItem)
- {
- int ret;
- TVITEM tvi = new TVITEM();
- IntPtr pszText = LocalAlloc(0x40, MY_MAXLVITEMTEXT);
- tvi.mask = TVIF_TEXT;
- tvi.hItem = hItem;
- tvi.cchTextMax = MY_MAXLVITEMTEXT;
- tvi.pszText = pszText;
- ret = SendMessageTVI(treeViewHwnd, TVM_GETITEM, 0, ref tvi);
- string buffer = Marshal.PtrToStringUni((IntPtr)tvi.pszText,
- MY_MAXLVITEMTEXT);
- //char[] arr = buffer.ToCharArray(); //<== use this array to look at the bytes
- //in debug mode
- LocalFree(pszText);
- return buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement