4

I have drag and drop control with adorner support. When dragging the list item from right to left the adorer window is not near the mouse pointer. Please anyone help me to place the adorner window near the mouse pointer. I have followed for creating the adorner window and displaying the adorner by using the below question Please refer the question. I tried like this for placing the adorner window near the mouse pointer. But its not working for higher resolution window, I did not get any correct solution. Please suggestion your idea.

Behavior

private Window _dragdropWindow = null;
ListBoxItem draggedItem = null;
Win32Point w32Mouse = new Win32Point();      

this.AssociatedObject.PreviewMouseMove += (sender, e) =>
{
   if (e.LeftButton == MouseButtonState.Pressed && dataObject != null && !IsDragging)
   {
      var currentPoint = e.GetPosition(sender as UIElement);
      if (Math.Abs(currentPoint.X - startingPoint.X) > 10 || (Math.Abs(currentPoint.Y - startingPoint.Y) > 10))
      {
         IsDragging = true;

         if (draggedItem != null)
         {
            CreateDragDropWindow(draggedItem);
         }

         DragDrop.DoDragDrop(sender as ListBox, dataObject, DragDropEffects.Copy);

         if (_dragdropWindow != null)
         {
            _dragdropWindow.Close();
            _dragdropWindow = null;
         }
      }
   }    
};


this.AssociatedObject.PreviewMouseLeftButtonDown += (sender, e) =>
{
   var listBoxItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
   if (listBoxItem != null)
   {
      startingPoint = e.GetPosition(sender as UIElement);
      dataObject = listBoxItem.DataContext as Details;                                       
      draggedItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
   }
   else
   {
      dataObject = null;
      IsDragging = false;
   }
};

this.AssociatedObject.PreviewGiveFeedback += (sender, e) =>
{
   Win32Point w32Mouse = new Win32Point();   
   GetCursorPos(ref w32Mouse);               

  _dragdropWindow.Left = w32Mouse.X;
  _dragdropWindow.Top = w32Mouse.Y;                
}; 

 private void CreateDragDropWindow(Visual dragElement)
 {
    this._dragdropWindow = new Window();
    _dragdropWindow.WindowStyle = WindowStyle.None;
    _dragdropWindow.AllowsTransparency = true;
    _dragdropWindow.AllowDrop = false;
    _dragdropWindow.Background = null;
    _dragdropWindow.IsHitTestVisible = false;
    _dragdropWindow.SizeToContent = SizeToContent.WidthAndHeight;
    _dragdropWindow.Topmost = true;
    _dragdropWindow.ShowInTaskbar = false;

    Rectangle rectangle = new Rectangle();
    rectangle.Width = ((FrameworkElement)dragElement).ActualWidth;
    rectangle.Height = ((FrameworkElement)dragElement).ActualHeight;
    rectangle.Fill = new VisualBrush(dragElement);
    this._dragdropWindow.Content = rectangle;           

    this._dragdropWindow.Left = w32Mouse.X;
    this._dragdropWindow.Top = w32Mouse.Y;
    this._dragdropWindow.Show();
 }


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);

[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
   public Int32 X;
   public Int32 Y;
};

When dragging the list item the adorner is not near the mouse pointerPlease refer the screenshot

sameer
  • 322
  • 1
  • 12

0 Answers0