Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeView Autoscroll while dragging

Winforms TreeView, I want to scroll up/down when the user drag and item.

like image 834
Pacman Avatar asked May 17 '11 17:05

Pacman


1 Answers

Pretty much the same as the above, but without that 'top' bug and a bit simpler to use in bigger projects.

Add this class to your project:

public static class NativeMethods
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    public static void Scroll(this Control control)
    {
        var pt = control.PointToClient(Cursor.Position);

        if ((pt.Y + 20) > control.Height)
        {
            // scroll down
            SendMessage(control.Handle, 277, (IntPtr) 1, (IntPtr) 0);
        }
        else if (pt.Y < 20)
        {
            // scroll up
            SendMessage(control.Handle, 277, (IntPtr) 0, (IntPtr) 0);
        }
    }
}

Then simply subscribe to the DragOver event of your treeview (or any other control/custom control you want scrolling enabled while drag/dropping) and call the Scroll() method.

    private void treeView_DragOver(object sender, DragEventArgs e)
    {
        treeView.Scroll();
    }
like image 184
Vedran Avatar answered Sep 28 '22 16:09

Vedran