public TreeViewHitTestInfo HitTest(int x, int y)
 {
     System.Windows.Forms.NativeMethods.TV_HITTESTINFO lParam = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO {
         pt_x = x,
         pt_y = y
     };
     IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, lParam);
     TreeNode hitNode = (handle == IntPtr.Zero) ? null : this.NodeFromHandle(handle);
     return new TreeViewHitTestInfo(hitNode, (TreeViewHitTestLocations) lParam.flags);
 }
 protected override void OnMouseHover(EventArgs e)
 {
     System.Windows.Forms.NativeMethods.TV_HITTESTINFO lParam = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO();
     Point position = Cursor.Position;
     position = base.PointToClientInternal(position);
     lParam.pt_x = position.X;
     lParam.pt_y = position.Y;
     IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, lParam);
     if ((handle != IntPtr.Zero) && ((lParam.flags & 70) != 0))
     {
         TreeNode node = this.NodeFromHandle(handle);
         if ((node != this.prevHoveredNode) && (node != null))
         {
             this.OnNodeMouseHover(new TreeNodeMouseHoverEventArgs(node));
             this.prevHoveredNode = node;
         }
     }
     if (!this.hoveredAlready)
     {
         base.OnMouseHover(e);
         this.hoveredAlready = true;
     }
     base.ResetMouseEventArgs();
 }
 public TreeNode GetNodeAt(int x, int y)
 {
     System.Windows.Forms.NativeMethods.TV_HITTESTINFO lParam = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO {
         pt_x = x,
         pt_y = y
     };
     IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, lParam);
     if (!(handle == IntPtr.Zero))
     {
         return this.NodeFromHandle(handle);
     }
     return null;
 }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x4e:
                {
                    System.Windows.Forms.NativeMethods.NMHDR lParam = (System.Windows.Forms.NativeMethods.NMHDR) m.GetLParam(typeof(System.Windows.Forms.NativeMethods.NMHDR));
                    switch (lParam.code)
                    {
                        case -521:
                            if (!this.WmShowToolTip(ref m))
                            {
                                base.WndProc(ref m);
                                return;
                            }
                            m.Result = (IntPtr) 1;
                            return;

                        case -520:
                        case -530:
                            System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(lParam, lParam.hwndFrom), 0x418, 0, SystemInformation.MaxWindowTrackSize.Width);
                            this.WmNeedText(ref m);
                            m.Result = (IntPtr) 1;
                            return;
                    }
                    base.WndProc(ref m);
                    return;
                }
                case 0x7b:
                {
                    if (this.treeViewState[0x2000])
                    {
                        this.treeViewState[0x2000] = false;
                        base.WndProc(ref m);
                        return;
                    }
                    TreeNode selectedNode = this.SelectedNode;
                    if ((selectedNode == null) || ((selectedNode.ContextMenu == null) && (selectedNode.ContextMenuStrip == null)))
                    {
                        base.WndProc(ref m);
                        return;
                    }
                    Point pt = new Point(selectedNode.Bounds.X, selectedNode.Bounds.Y + (selectedNode.Bounds.Height / 2));
                    if (base.ClientRectangle.Contains(pt))
                    {
                        if (selectedNode.ContextMenu != null)
                        {
                            selectedNode.ContextMenu.Show(this, pt);
                            return;
                        }
                        if (selectedNode.ContextMenuStrip == null)
                        {
                            return;
                        }
                        bool isKeyboardActivated = ((int) ((long) m.LParam)) == -1;
                        selectedNode.ContextMenuStrip.ShowInternal(this, pt, isKeyboardActivated);
                    }
                    return;
                }
                case 0x83:
                case 5:
                case 70:
                case 0x47:
                    if (this.treeViewState[0x8000])
                    {
                        this.DefWndProc(ref m);
                        return;
                    }
                    base.WndProc(ref m);
                    return;

                case 7:
                    if (!this.treeViewState[0x4000])
                    {
                        base.WndProc(ref m);
                        return;
                    }
                    this.treeViewState[0x4000] = false;
                    base.WmImeSetFocus();
                    this.DefWndProc(ref m);
                    this.OnGotFocus(EventArgs.Empty);
                    return;

                case 0x15:
                    base.SendMessage(0x1107, this.Indent, 0);
                    base.WndProc(ref m);
                    return;

                case 0x201:
                {
                    try
                    {
                        this.treeViewState[0x10000] = true;
                        this.FocusInternal();
                    }
                    finally
                    {
                        this.treeViewState[0x10000] = false;
                    }
                    this.treeViewState[0x1000] = false;
                    System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO {
                        pt_x = System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam),
                        pt_y = System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam)
                    };
                    this.hNodeMouseDown = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo);
                    if ((tv_hittestinfo.flags & 0x40) != 0)
                    {
                        this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                        if (!base.ValidationCancelled && this.CheckBoxes)
                        {
                            TreeNode node = this.NodeFromHandle(this.hNodeMouseDown);
                            if (!this.TreeViewBeforeCheck(node, TreeViewAction.ByMouse) && (node != null))
                            {
                                node.CheckedInternal = !node.CheckedInternal;
                                this.TreeViewAfterCheck(node, TreeViewAction.ByMouse);
                            }
                        }
                        m.Result = IntPtr.Zero;
                    }
                    else
                    {
                        this.WmMouseDown(ref m, MouseButtons.Left, 1);
                    }
                    this.downButton = MouseButtons.Left;
                    return;
                }
                case 0x202:
                case 0x205:
                {
                    System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo2 = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO {
                        pt_x = System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam),
                        pt_y = System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam)
                    };
                    IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo2);
                    if (handle != IntPtr.Zero)
                    {
                        if (!base.ValidationCancelled && (!this.treeViewState[0x800] & !this.treeViewState[0x1000]))
                        {
                            if (handle == this.hNodeMouseDown)
                            {
                                this.OnNodeMouseClick(new TreeNodeMouseClickEventArgs(this.NodeFromHandle(handle), this.downButton, 1, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam)));
                            }
                            this.OnClick(new MouseEventArgs(this.downButton, 1, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                            this.OnMouseClick(new MouseEventArgs(this.downButton, 1, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                        }
                        if (this.treeViewState[0x800])
                        {
                            this.treeViewState[0x800] = false;
                            if (!base.ValidationCancelled)
                            {
                                this.OnNodeMouseDoubleClick(new TreeNodeMouseClickEventArgs(this.NodeFromHandle(handle), this.downButton, 2, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam)));
                                this.OnDoubleClick(new MouseEventArgs(this.downButton, 2, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                                this.OnMouseDoubleClick(new MouseEventArgs(this.downButton, 2, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                            }
                        }
                    }
                    if (!this.treeViewState[0x1000])
                    {
                        this.OnMouseUp(new MouseEventArgs(this.downButton, 1, System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam), System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam), 0));
                    }
                    this.treeViewState[0x800] = false;
                    this.treeViewState[0x1000] = false;
                    base.CaptureInternal = false;
                    this.hNodeMouseDown = IntPtr.Zero;
                    return;
                }
                case 0x203:
                    this.WmMouseDown(ref m, MouseButtons.Left, 2);
                    this.treeViewState[0x800] = true;
                    this.treeViewState[0x1000] = false;
                    base.CaptureInternal = true;
                    return;

                case 0x204:
                {
                    this.treeViewState[0x1000] = false;
                    System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo3 = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO {
                        pt_x = System.Windows.Forms.NativeMethods.Util.SignedLOWORD(m.LParam),
                        pt_y = System.Windows.Forms.NativeMethods.Util.SignedHIWORD(m.LParam)
                    };
                    this.hNodeMouseDown = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo3);
                    this.WmMouseDown(ref m, MouseButtons.Right, 1);
                    this.downButton = MouseButtons.Right;
                    return;
                }
                case 0x206:
                    this.WmMouseDown(ref m, MouseButtons.Right, 2);
                    this.treeViewState[0x800] = true;
                    this.treeViewState[0x1000] = false;
                    base.CaptureInternal = true;
                    return;

                case 0x207:
                    this.treeViewState[0x1000] = false;
                    this.WmMouseDown(ref m, MouseButtons.Middle, 1);
                    this.downButton = MouseButtons.Middle;
                    return;

                case 0x209:
                    this.treeViewState[0x1000] = false;
                    this.WmMouseDown(ref m, MouseButtons.Middle, 2);
                    return;

                case 0x2a3:
                    this.prevHoveredNode = null;
                    base.WndProc(ref m);
                    return;

                case 0x114:
                    base.WndProc(ref m);
                    if (this.DrawMode == TreeViewDrawMode.OwnerDrawAll)
                    {
                        base.Invalidate();
                    }
                    return;

                case 0x113f:
                case 0x110d:
                    base.WndProc(ref m);
                    if (this.CheckBoxes)
                    {
                        System.Windows.Forms.NativeMethods.TV_ITEM tv_item = (System.Windows.Forms.NativeMethods.TV_ITEM) m.GetLParam(typeof(System.Windows.Forms.NativeMethods.TV_ITEM));
                        if (!(tv_item.hItem != IntPtr.Zero))
                        {
                            return;
                        }
                        System.Windows.Forms.NativeMethods.TV_ITEM tv_item2 = new System.Windows.Forms.NativeMethods.TV_ITEM {
                            mask = 0x18,
                            hItem = tv_item.hItem,
                            stateMask = 0xf000
                        };
                        System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(null, base.Handle), System.Windows.Forms.NativeMethods.TVM_GETITEM, 0, ref tv_item2);
                        this.NodeFromHandle(tv_item.hItem).CheckedStateInternal = (tv_item2.state >> 12) > 1;
                    }
                    return;

                case 0x204e:
                    this.WmNotify(ref m);
                    return;

                case 0x317:
                    this.WmPrint(ref m);
                    return;
            }
            base.WndProc(ref m);
        }
 private unsafe bool WmShowToolTip(ref Message m)
 {
     System.Windows.Forms.NativeMethods.NMHDR* lParam = (System.Windows.Forms.NativeMethods.NMHDR*) m.LParam;
     IntPtr hwndFrom = lParam->hwndFrom;
     System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO();
     Point position = Cursor.Position;
     position = base.PointToClientInternal(position);
     tv_hittestinfo.pt_x = position.X;
     tv_hittestinfo.pt_y = position.Y;
     IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo);
     if ((handle != IntPtr.Zero) && ((tv_hittestinfo.flags & 70) != 0))
     {
         TreeNode node = this.NodeFromHandle(handle);
         if ((node != null) && !this.ShowNodeToolTips)
         {
             Rectangle bounds = node.Bounds;
             bounds.Location = base.PointToScreen(bounds.Location);
             System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, hwndFrom), 0x41f, 1, ref bounds);
             System.Windows.Forms.SafeNativeMethods.SetWindowPos(new HandleRef(this, hwndFrom), System.Windows.Forms.NativeMethods.HWND_TOPMOST, bounds.Left, bounds.Top, 0, 0, 0x15);
             return true;
         }
     }
     return false;
 }
        private unsafe void WmNotify(ref Message m)
        {
            System.Windows.Forms.NativeMethods.NMHDR* lParam = (System.Windows.Forms.NativeMethods.NMHDR*) m.LParam;
            if (lParam->code == -12)
            {
                this.CustomDraw(ref m);
            }
            else
            {
                System.Windows.Forms.NativeMethods.NMTREEVIEW* nmtv = (System.Windows.Forms.NativeMethods.NMTREEVIEW*) m.LParam;
                switch (nmtv->nmhdr.code)
                {
                    case -5:
                    case -2:
                    {
                        MouseButtons left = MouseButtons.Left;
                        System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO();
                        Point position = Cursor.Position;
                        position = base.PointToClientInternal(position);
                        tv_hittestinfo.pt_x = position.X;
                        tv_hittestinfo.pt_y = position.Y;
                        IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo);
                        if ((nmtv->nmhdr.code != -2) || ((tv_hittestinfo.flags & 70) != 0))
                        {
                            left = (nmtv->nmhdr.code == -2) ? MouseButtons.Left : MouseButtons.Right;
                        }
                        if ((((nmtv->nmhdr.code != -2) || ((tv_hittestinfo.flags & 70) != 0)) || this.FullRowSelect) && ((handle != IntPtr.Zero) && !base.ValidationCancelled))
                        {
                            this.OnNodeMouseClick(new TreeNodeMouseClickEventArgs(this.NodeFromHandle(handle), left, 1, position.X, position.Y));
                            this.OnClick(new MouseEventArgs(left, 1, position.X, position.Y, 0));
                            this.OnMouseClick(new MouseEventArgs(left, 1, position.X, position.Y, 0));
                        }
                        if (nmtv->nmhdr.code == -5)
                        {
                            TreeNode treeNode = this.NodeFromHandle(handle);
                            if ((treeNode != null) && ((treeNode.ContextMenu != null) || (treeNode.ContextMenuStrip != null)))
                            {
                                this.ShowContextMenu(treeNode);
                            }
                            else
                            {
                                this.treeViewState[0x2000] = true;
                                base.SendMessage(0x7b, base.Handle, System.Windows.Forms.SafeNativeMethods.GetMessagePos());
                            }
                            m.Result = (IntPtr) 1;
                        }
                        if (!this.treeViewState[0x1000] && ((nmtv->nmhdr.code != -2) || ((tv_hittestinfo.flags & 70) != 0)))
                        {
                            this.OnMouseUp(new MouseEventArgs(left, 1, position.X, position.Y, 0));
                            this.treeViewState[0x1000] = true;
                        }
                        break;
                    }
                    case -460:
                    case -411:
                        m.Result = this.TvnEndLabelEdit((System.Windows.Forms.NativeMethods.NMTVDISPINFO) m.GetLParam(typeof(System.Windows.Forms.NativeMethods.NMTVDISPINFO)));
                        return;

                    case -459:
                    case -410:
                        m.Result = this.TvnBeginLabelEdit((System.Windows.Forms.NativeMethods.NMTVDISPINFO) m.GetLParam(typeof(System.Windows.Forms.NativeMethods.NMTVDISPINFO)));
                        return;

                    case -458:
                    case -453:
                    case -452:
                    case -409:
                    case -404:
                    case -403:
                        break;

                    case -457:
                    case -408:
                        this.TvnBeginDrag(MouseButtons.Right, nmtv);
                        return;

                    case -456:
                    case -407:
                        this.TvnBeginDrag(MouseButtons.Left, nmtv);
                        return;

                    case -455:
                    case -406:
                        this.TvnExpanded(nmtv);
                        return;

                    case -454:
                    case -405:
                        m.Result = this.TvnExpanding(nmtv);
                        return;

                    case -451:
                    case -402:
                        this.TvnSelected(nmtv);
                        return;

                    case -450:
                    case -401:
                        m.Result = this.TvnSelecting(nmtv);
                        return;

                    default:
                        return;
                }
            }
        }
 private void WmNeedText(ref Message m)
 {
     System.Windows.Forms.NativeMethods.TOOLTIPTEXT lParam = (System.Windows.Forms.NativeMethods.TOOLTIPTEXT) m.GetLParam(typeof(System.Windows.Forms.NativeMethods.TOOLTIPTEXT));
     string controlToolTipText = this.controlToolTipText;
     System.Windows.Forms.NativeMethods.TV_HITTESTINFO tv_hittestinfo = new System.Windows.Forms.NativeMethods.TV_HITTESTINFO();
     Point position = Cursor.Position;
     position = base.PointToClientInternal(position);
     tv_hittestinfo.pt_x = position.X;
     tv_hittestinfo.pt_y = position.Y;
     IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x1111, 0, tv_hittestinfo);
     if ((handle != IntPtr.Zero) && ((tv_hittestinfo.flags & 70) != 0))
     {
         TreeNode node = this.NodeFromHandle(handle);
         if ((this.ShowNodeToolTips && (node != null)) && !string.IsNullOrEmpty(node.ToolTipText))
         {
             controlToolTipText = node.ToolTipText;
         }
         else if ((node != null) && (node.Bounds.Right > base.Bounds.Right))
         {
             controlToolTipText = node.Text;
         }
         else
         {
             controlToolTipText = null;
         }
     }
     lParam.lpszText = controlToolTipText;
     lParam.hinst = IntPtr.Zero;
     if (this.RightToLeft == RightToLeft.Yes)
     {
         lParam.uFlags |= 4;
     }
     Marshal.StructureToPtr(lParam, m.LParam, false);
 }