Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1440
WinListView DragDrop demo why are elements dragged so far from each other during a drag
posted

You WinListView DragDrop demo is a very terrible example when displaying elements being dragged. If I select like three items that are going to be dragged that are far apart from each other in the list view these items remain far apart from each other while being dragged. These items should be directly right underneath each other while being dragged not half way spread out away from each other. Do you have a fix for this in you demo.

Parents
No Data
Reply
  • 48586
    Verified Answer
    posted

    Hello ,

     

    As far as I understand you want to “rearrange the icons” in the image which is displayed when the “Drag” operation starts.  In the sample, which you mentioned this icons are drawing with their relative coordinates (actually they are drawing with their locations and sizes onto a bitmap ) that's why the elements are dragged so far from each other.  Also this functionality is not embedded into the component, which mean that the developer is responsible for its implementation. Based on this, order of icons is designer’s decision and any kind of ordering  should be consider as a correct. In the sample which you mentioned, the code which is responsible for displaying of this icons, when the “Drag” operation starts, is in “DragDropManager”  class find  “DragImageManager” class, “Methods” region, “CreateDragImage” region, CreateDragImage method. So you could modify this method based on your needs. One possible modification could be the following:

     

    #region CreateDragImage

                        /// <summary>

                        /// Generates the image that is displayed to depict the contents of the drag operation.

                        /// </summary>

                        /// <param name="dragElements">An array of UIElements from which the image is generated.</param>

                        private void CreateDragImage( UIElement[] dragElements )

                        {

                               if ( dragElements == null || dragElements.Length == 0 )

                                     return;

     

                               //     Calculate the bounds of a rectangle which contains all the elements

                               int left = short.MaxValue, top = short.MaxValue, right = 0, bottom = 0;

                               for ( int i = 0; i < dragElements.Length; i ++ )

                               {

                                     UIElement element = dragElements[i];

                                     if ( element == null )

                                            continue;

     

                                     Rectangle elementRect = element.Rect;

                                     left = Math.Min( left, elementRect.Left );

                                     top = Math.Min( top, elementRect.Top );

                                     right = Math.Max( right, elementRect.Right );

                                     bottom = Math.Max( bottom, elementRect.Bottom );

                               }

     

                               Point renderingOffset = new Point( left, top );

                               Size size = new Size( right - left, bottom - top );

                               Rectangle bounds = new Rectangle( Point.Empty, size );

     

                               //     Create the Bitmap which we will draw on to create the drag image

                    this._dragImage = new Bitmap( size.Width, size.Height );

                               Bitmap img = new Bitmap( size.Width, size.Height );

                  

                               Graphics gr = null;

                    Graphics grHelper = null;

                               Matrix matrix = null;

     

                               try

                               {

                                     //     Create a Graphics object from the Bitmap we created above;

                                     //     fill it with the transparent color and offset its rendering origin.

                                     gr = Graphics.FromImage( this._dragImage );

                        grHelper = Graphics.FromImage( img );

                                     //     Transform the coordinate system to shift the point of origin

                                     matrix = new System.Drawing.Drawing2D.Matrix( 1, 0, 0, 1, renderingOffset.X * -1, renderingOffset.Y * -1 );

                                     gr.Transform = matrix;

                        grHelper.Transform = matrix;

                                     //     Get the BackColor of the parent form...we need to use this as the background color

                                     //     because when we draw the UIElements to the Graphics object, transparent areas will

                                     //     be drawn with this color, and we want those areas to be masked out of the resulting image.

                                     Color parentFormBackColor = dragElements[0] != null ? dragElements[0].Control.BackColor : Form.DefaultBackColor;

                                     gr.Clear( parentFormBackColor );

                        grHelper.Clear(parentFormBackColor);

     

                                     //     Draw each element onto the Graphics object

                        Point location = new Point(1,1);

                                     for ( int i = 0; i < dragElements.Length; i ++ )

                                     {

                                            UIElement element = dragElements[i];

                                            if ( element == null )

                                                   continue;

                            if (location.X == -1)

                                location = element.Rect.Location;

     

                            Rectangle rect = new Rectangle(location, element.Rect.Size);

                            Debug.WriteLine(string.Format("el rect{0}, my rect {1}", element.Rect, rect));

                            element.Draw(grHelper, element.Rect, true, AlphaBlendMode.Disabled);

                            gr.DrawImage(CropImage(img, element.Rect), location);

                            location.Offset(10,10);

                                     }

     

                                     //     Generate a Region from the Bitmap we just created above

                                     this._dragImageRegion = ImageManager.CreateBitmapRegion( this._dragImage, parentFormBackColor );

     

                                     //     Adjust the opacity of the drag image to give it

                                     //     a semi-transparent, "ghosted" appearance.

                                     this.AdjustTransparency( this._dragImage );

                               }

                               finally

                               {

                                     //     Dispose of the Graphics and Matrix objects

                                     if ( gr != null )

                                            gr.Dispose();

                        if (grHelper != null)

                            grHelper.Dispose();

     

                                     if ( matrix != null )

                                            matrix.Dispose();

                               }

                        }

     

                public Bitmap CropImage(Bitmap source, Rectangle section)

                {

     

                    // An empty bitmap which will hold the cropped image

                    Bitmap bmp = new Bitmap(section.Width, section.Height);

     

                    Graphics g = Graphics.FromImage(bmp);

     

                    // Draw the given area (section) of the source image

                    // at location 0,0 on the empty bitmap (bmp)

                    g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

     

                    return bmp;

                }

                               #endregion CreateDragImage

     

     

    Thank you for using Infragistics Components.

Children