Tuesday, August 24, 2010

Disabling and/or adjusting the drag and drop activation distance

Updated: A better Visual Studio 2010 specific solution is to install the VSCommands 2010 plugin.

Several times I've caught myself out in Visual Studio Solution Explorer by dragging and dropping a file/folder into another folder by accident. Usually the folder immediately adjacent to the file/folder being moved. The default windows threshold for drag and drop it 4 pixels. By increasing this I'm hoping to avoid accidental drag and drop.

Note: This will affect all drag and drop operations in Windows, not just Visual Studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace AdjustDragDropDistance
{
 class Program
 {
  /// 
  /// uAction for drag and drop horizontal threshold
  /// 
  static int SPI_SETDRAGWIDTH = 76;
  /// 
  /// uAction for drag and drop vertical threshold
  /// 
  static int SPI_SETDRAGHEIGHT = 77;
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

  static void Main(string[] args)
  {
   //Default value to use the can be overridden from the command line.
   //The normal windows default is around 4 pixels.
   int _dragThresholdInPixels = 25;

   try
   {
    if (args.Length > 0)
    {
     if(!int.TryParse(args[0], out _dragThresholdInPixels))
     {
      Console.Error.WriteLine("Failed to parse [{0}] as integer for drag threshold.", args[0]);
     }
    }
    SystemParametersInfo(SPI_SETDRAGWIDTH, _dragThresholdInPixels, "", 0);
    SystemParametersInfo(SPI_SETDRAGHEIGHT, _dragThresholdInPixels, "", 0);
   }
   catch (Exception ex)
   {
    Console.Error.WriteLine(ex.ToString());
    Console.Read();
   }
  }
 }
}

See Also: