public TreeNode GetNodeAt(TreeView treeView, Point point) { TVHITTESTINFO hti = new TVHITTESTINFO(); hti.pt = new POINT(point); Win32Declarations.SendMessage(treeView.Handle, TreeViewMessage.TVM_HITTEST, 0, ref hti); if ((hti.flags & TreeViewHitTestFlags.ONITEM) != 0) { return(TreeNode.FromHandle(treeView, hti.hItem)); } if ((hti.flags & TreeViewHitTestFlags.ONITEMRIGHT) != 0) { using (Graphics g = treeView.CreateGraphics()) { IntPtr hdc = g.GetHdc(); try { TreeNode node = TreeNode.FromHandle(treeView, hti.hItem); int offset = CalculateOffset(node); Rectangle content = CalculateContentRectangle(node, hdc, offset); if (content.Contains(point)) { return(node); } } finally { g.ReleaseHdc(hdc); } } } return(null); }
private void HandleClick(Point pt, Keys modifierKeys) { IShellItem item = null; try { TVHITTESTINFO structure = new TVHITTESTINFO { pt = pt }; IntPtr wParam = PInvoke.SendMessage(treeController.Handle, 0x1111, IntPtr.Zero, ref structure); if (wParam != IntPtr.Zero) { if ((structure.flags & 0x10) == 0 && (structure.flags & 0x80) == 0) { treeControl.HitTest(pt, out item); if (item != null) { IntPtr pidl; if (PInvoke.SHGetIDListFromObject(item, out pidl) == 0) { using (IDLWrapper wrapper = new IDLWrapper(pidl)) { TreeViewMiddleClicked(wrapper, modifierKeys); } } } } } } finally { if (item != null) { Marshal.ReleaseComObject(item); } } }
protected void TreeControl_MouseUp(object sender, MouseEventArgs e) { try { // Show menu only if the right mouse button is clicked. // Point where the mouse is clicked. Point p = new Point(e.X, e.Y); // Get the node that the user has clicked. TreeNode node = GetNodeAt(p); if (node != null) { TVHITTESTINFO ht = new TVHITTESTINFO(); ht.pt = e.Location; SendMessage(this.Handle, TVM_HITTEST, IntPtr.Zero, ref ht); // Select the node the user has clicked. // The node appears selected until the menu is displayed on the screen. m_OldSelectNode = SelectedNode; SelectedNode = node; if (e.Button == MouseButtons.Right) { IEnumerable <ToolStripItem> items = MakeContextMenu(node); if (items != null) { treeContextMenu.Items.Clear(); foreach (var i in items) { treeContextMenu.Items.Add(i); } treeContextMenu.Show(this, p); } } else { if (ht.flags == 16)//todo { //Clicked on a ExpandOrCollapse button //- срабатывает не всегда //todo итем всё равно выделяется } else { OnItemSelected(); } } } } catch (Exception ex) { Log.ShowError(ex); } }
TVHITTESTINFO HitTest(Point p) { TVHITTESTINFO htInfo = new TVHITTESTINFO(); htInfo.pt = p; if (m_HitTestData == IntPtr.Zero) { m_HitTestData = Marshal.AllocHGlobal(Marshal.SizeOf(htInfo)); } Marshal.StructureToPtr(htInfo, m_HitTestData, true); //Save to unmanaged memory WindowsAPI.SendMessage(Handle, (int)TreeViewMessages.TVM_HITTEST, IntPtr.Zero, m_HitTestData); htInfo = (TVHITTESTINFO)Marshal.PtrToStructure(m_HitTestData, htInfo.GetType()); //Load from unmanaged memory return(htInfo); }
protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (myThreeStateCheckboxes) { bool doubleClick = false; if (myLastMouseUpPoint.X != -1) { if (Math.Abs(e.X - myLastMouseUpPoint.X) <= SystemInformation.DoubleClickSize.Width && Math.Abs(e.Y - myLastMouseUpPoint.Y) <= SystemInformation.DoubleClickSize.Height) { TimeSpan ts = DateTime.Now - myLastMouseUpTime; if (ts.TotalMilliseconds <= SystemInformation.DoubleClickTime) { doubleClick = true; } } } myLastMouseUpPoint = new Point(e.X, e.Y); myLastMouseUpTime = DateTime.Now; if (doubleClick) { return; } TVHITTESTINFO hti = new TVHITTESTINFO(); hti.pt = new POINT(e.X, e.Y); Win32Declarations.SendMessage(Handle, TreeViewMessage.TVM_HITTEST, 0, ref hti); if ((hti.flags & TreeViewHitTestFlags.ONITEMSTATEICON) != 0) { TreeNode node = TreeNode.FromHandle(this, hti.hItem); NodeCheckState state = GetNodeCheckState(node); if (state == NodeCheckState.Checked || state == NodeCheckState.Grayed) { state = NodeCheckState.Unchecked; } else { state = NodeCheckState.Checked; } ChangeNodeCheckState(node, state); } } }
private TriStateTreeNode GetTreeNodeHitAtCheckBoxByClientPosition(int iXClientPos, int iYClientPos) { //Get the hit info TVHITTESTINFO tvHitTestInfo = new TVHITTESTINFO(); tvHitTestInfo.pt.x = iXClientPos; tvHitTestInfo.pt.y = iYClientPos; IntPtr ptrTreeNode = SendMessage(this.Handle, TVM_HITTEST, 0, ref tvHitTestInfo); //Check if it has clicked on an item if (ptrTreeNode != null) { //Check if it has clicked on the state image of the item if ((tvHitTestInfo.flags & TVHT_ONITEMSTATEICON) != 0) { return((TriStateTreeNode)TriStateTreeNode.FromHandle(this, ptrTreeNode)); } } return(null); }
/// <summary> /// Get treenode by client position /// </summary> /// <param name="x">x position in client</param> /// <param name="y">y position in client</param> /// <returns></returns> private TreeNode GetTreeNodeHitAtCheckBoxByClientPosition(int x, int y) { TreeNode treeNode = null; UInt32 iTreeNodeHandle; TVHITTESTINFO tTVHITTESTINFO = new TVHITTESTINFO(); // Get the hit info tTVHITTESTINFO.pt.x = x; tTVHITTESTINFO.pt.y = y; iTreeNodeHandle = SendMessage(this.Handle, TVM_HITTEST, 0, ref tTVHITTESTINFO); // Check if it has clicked on an item if (iTreeNodeHandle != 0) { // Check if it has clicked on the state image of the item if ((tTVHITTESTINFO.flags & TVHT_ONITEMSTATEICON) != 0) { treeNode = TreeNode.FromHandle(this, new IntPtr(iTreeNodeHandle)); } } return(treeNode); }
private void HandleClick(Point pt) { IShellItem item = null; IntPtr ptr = IntPtr.Zero; try { TVHITTESTINFO structure = new TVHITTESTINFO { pt = pt }; ptr = Marshal.AllocHGlobal(Marshal.SizeOf(structure)); Marshal.StructureToPtr(structure, ptr, false); IntPtr wParam = PInvoke.SendMessage(treeController.Handle, 0x1111, IntPtr.Zero, ptr); if (wParam != IntPtr.Zero) { structure = (TVHITTESTINFO)Marshal.PtrToStructure(ptr, typeof(TVHITTESTINFO)); if ((structure.flags & 0x10) == 0 && (structure.flags & 0x80) == 0) { treeControl.HitTest(pt, out item); if (item != null) { TreeViewMiddleClicked(item); } } } } finally { if (item != null) { Marshal.ReleaseComObject(item); } if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } }
public extern static int SendMessage(IntPtr hwnd, uint msg, uint wParam, ref TVHITTESTINFO lParam);
private static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, ref TVHITTESTINFO lParam);
/// <summary> /// 現在のマウス選択位置のアイテムを取得。 /// </summary> /// <returns>現在のマウス選択位置のアイテム。</returns> private IntPtr GetCurrentMousePosItem() { TVHITTESTINFO info = new TVHITTESTINFO(); info.pt.X = Control.MousePosition.X; info.pt.Y = Control.MousePosition.Y; NativeMethods.ScreenToClient(WindowHandle, ref info.pt); return NativeMethods.SendMessage(WindowHandle, NativeTree.TVM_HITTEST, IntPtr.Zero, ref info); }
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, int wparam, ref TVHITTESTINFO lparam);
private TriStateTreeNode GetTreeNodeHitAtCheckBoxByClientPosition(int iXClientPos, int iYClientPos) { //Get the hit info TVHITTESTINFO tvHitTestInfo = new TVHITTESTINFO(); tvHitTestInfo.pt.x = iXClientPos; tvHitTestInfo.pt.y = iYClientPos; IntPtr ptrTreeNode = SendMessage(this.Handle, TVM_HITTEST, 0, ref tvHitTestInfo); //Check if it has clicked on an item if (ptrTreeNode != null) { //Check if it has clicked on the state image of the item if ((tvHitTestInfo.flags & TVHT_ONITEMSTATEICON) != 0) return (TriStateTreeNode)TriStateTreeNode.FromHandle(this, ptrTreeNode); } return null; }
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref TVHITTESTINFO lParam);
public static extern int SendMessage( IntPtr hwnd, uint msg, uint wParam, ref TVHITTESTINFO lParam);
protected override void WndProc(ref Message message) { bool oldLabelEdit = LabelEdit; switch (message.Msg) { case (int)Msg.WM_LBUTTONDOWN: POINT p = new POINT((int)message.LParam); TVHITTESTINFO ht = HitTest(p); if (ht.hItem != IntPtr.Zero && !m_SelectedItems.Contains((int)ht.hItem)) { LabelEdit = false; SelectTreeItem(ht.hItem); } break; // Reflected Messages come from the treeview control itself case (int)ReflectedMessages.OCM_NOTIFY: NMHDR nm2 = (NMHDR)message.GetLParam(typeof(NMHDR)); switch (nm2.code) { case (int)NotificationMessages.NM_CUSTOMDRAW: base.WndProc(ref message); NotifyTreeCustomDraw(ref message); return; case (int)TreeViewNotification.TVN_DeleteItem: NMTREEVIEW tv = (NMTREEVIEW)message.GetLParam(typeof(NMTREEVIEW)); if (m_SelectedItems.Contains((int)tv.itemOld.hItem)) { m_SelectedItems.Remove((int)tv.itemOld.hItem); m_selectionChanged = true; OnSelectionChanged(); } break; case (int)TreeViewNotification.TVN_SelectionChanged: NMTREEVIEW tv1 = (NMTREEVIEW)message.GetLParam(typeof(NMTREEVIEW)); int hItem = (int)tv1.itemNew.hItem; if ((WindowsAPI.GetKeyState((int)VirtualKeys.VK_SHIFT) | 1) != 1) { if (!m_SelectedItems.Contains(hItem)) { m_SelectedItems.Add(hItem); } else { m_SelectedItems.Remove(hItem); } } else if ((WindowsAPI.GetKeyState((int)VirtualKeys.VK_CONTROL) | 1) != 1) { if (!m_SelectedItems.Contains(hItem)) { m_SelectedItems.Add(hItem); } else { m_SelectedItems.Remove(hItem); } } else { bool bFullInvalidate = m_SelectedItems.Count > 1; m_SelectedItems.Clear(); m_SelectedItems.Add(hItem); if (bFullInvalidate) { Invalidate(); } } m_selectionChanged = true; OnSelectionChanged(); break; default: break; } break; default: break; } base.WndProc(ref message); LabelEdit = oldLabelEdit; }
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref TVHITTESTINFO lParam);