60000430 Drag & Drop a data object from a List box that represents a Drawing, in to VectorDraw Control as Insert object.

Article 60000430
Type HowTo
Product Engine
Date Added 2/23/2008 12:00:00 AM
Fixed (2/23/2008 12:00:00 AM)
Submitted by Levi

Summary

Drag & Drop a data object from a List box that represents a Drawing, in to VectorDraw Control as Insert object.

Solution

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using VectorDraw.Actions;
using VectorDraw.Geometry;
using VectorDraw.Professional.vdPrimaries;
using VectorDraw.Professional.vdCollections;
using VectorDraw.Professional.vdObjects;
using VectorDraw.Professional.ActionUtility;
using VectorDraw.Professional.vdFigures;

namespace WindowsApplication1
{
    //Example of Drag & Drop a data object from a List box that represents a Drawing, in to VectorDraw Control as Insert object.
    public partial class Form1 : Form
    {
       
        public vdInsert Winsert = null;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            vdDest.vdDragEnter += new VectorDraw.Professional.Control.DragEnterEventHandler(vdDest_vdDragEnter);
            vdDest.vdDragDrop += new VectorDraw.Professional.Control.DragDropEventHandler(vdDest_vdDragDrop);
            vdDest.vdDragOver +=new VectorDraw.Professional.Control.DragOverEventHandler(vdDest_vdDragOver);
            vdDest.vdDragLeave += new VectorDraw.Professional.Control.DragLeaveEventHandler(vdDest_vdDragLeave);
            vdDest.DrawOverAll += new VectorDraw.Professional.Control.DrawOverAllEventHandler(vdDest_DrawOverAll);
       
        }
        void vdDest_DrawOverAll(object sender, VectorDraw.Render.vdRender render, ref bool cancel)
        {
            if (Winsert == null) return;
            //If this event is called when a DragDrop action is active (from  vdDest_vdDragOver) then we repaint the screen with the Insret object in the curent Cursor position.
            gPoint curpt = vdDest.ActiveDocument.CCS_CursorPos();
            Winsert.InsertionPoint = curpt;
            Winsert.Update();
            render.UnLock();//use Unlock / Lock in order the rendering be smoother as GDIPlusRender
            Winsert.Draw(render);
            render.Lock();
        }
        void vdDest_vdDragLeave(EventArgs e, ref bool cancel)
        {
            //Leaving the VectorDraw control
            cancel = true;
            Winsert = null;
        }
        void vdDest_vdDragEnter(DragEventArgs drgevent, ref bool cancel)
        {
            //A drag drop action is active and the cursor is just activate in the VectorDraw screen
            cancel = true;

            //get the data object and check if is represents a drawing file.
            DataObject dataobject = drgevent.Data as DataObject;
            if (dataobject == null) return;
            System.Collections.Specialized.StringCollection strings = dataobject.GetFileDropList();
            if (strings == null || strings.Count != 1) return;
            string filename = strings[0];
            string blockname = System.IO.Path.GetFileNameWithoutExtension(filename);//get th block name of the file
            vdBlock block = vdDest.ActiveDocument.Blocks.FindName(blockname);//if the block already exist in the drawing then we do not redifine it.
            if (block == null)
            {
                Cursor curCursor = Cursor;
                Cursor = Cursors.WaitCursor;
                block = vdDest.ActiveDocument.Blocks.AddFromFile(filename, false);//add the block in the drawing
                Cursor = curCursor;
            }
            if (block == null) return;
            drgevent.Effect = DragDropEffects.Copy;

            //create an insert object but we do not add it in the Document ActiveLayout entities.
            Winsert = new vdInsert();
            Winsert.SetUnRegisterDocument(vdDest.ActiveDocument);
            Winsert.setDocumentDefaults();
            Winsert.Block = block;
            Winsert.CreateDefaultAttributes();
            Winsert.InsertionPoint = vdDest.ActiveDocument.CCS_CursorPos();
            Winsert.Update();
           
        }
        void vdDest_vdDragDrop(DragEventArgs drgevent, ref bool cancel)
        {
            cancel = true;
            if (Winsert == null) return;
            //Add the insert object in to Document ActiveLayout entities.
            Winsert.Invalidate();
            Winsert.InsertionPoint = vdDest.ActiveDocument.CCS_CursorPos();
            Winsert.Update();
            vdDest.ActiveDocument.ActiveLayOut.Entities.AddItem(Winsert);
            Winsert.Invalidate();
            Winsert = null;
        }

       
        private void vdDest_vdDragOver(DragEventArgs drgevent, ref bool cancel)
        {
            cancel = true;
            if (Winsert == null) return;
            gPoint curpt = vdDest.ActiveDocument.CCS_CursorPos();
            //If the mouse is not moved then we do not refresh the graphics screen to avoid flicking
            if (Winsert.InsertionPoint.AreEqual(curpt, vdDest.ActiveDocument.ActiveActionRender.PixelSize / 2.0d)) return;
            //This command will refresh the graphics screen and call the vdDest_DrawOverAll event
            vdDest.ActiveDocument.ActiveLayOut.RefreshGraphicsControl(null);
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //Create a new data object that contains an existing file and begin a Drag drop operation.
            DataObject data = new DataObject();
            System.Collections.Specialized.StringCollection filepaths = new System.Collections.Specialized.StringCollection();
            filepaths.Add(Application.StartupPath + "\\exemplo.dwg");
            data.SetFileDropList(filepaths);
            listBox1.DoDragDrop(data, DragDropEffects.Copy);
        }
    }
}


   
In version 7 and due to the Render's changes the DrawOverAll is not fired constantly as in version 6. In this case the code should be altered to :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VectorDraw.Actions;
using VectorDraw.Generics;
using VectorDraw.Geometry;
using VectorDraw.Professional;
using VectorDraw.Professional.vdPrimaries;
using VectorDraw.Professional.vdCollections;
using VectorDraw.Professional.vdObjects;
using VectorDraw.Professional.ActionUtility;
using VectorDraw.Professional.vdFigures;
using VectorDraw.Render;
using VectorDraw.Serialize;

namespace Example_Drag_7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        vdInsert Winsert = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            vdDest.vdDragDrop += new VectorDraw.Professional.Control.DragDropEventHandler(vectorDrawBaseControl1_vdDragDrop);
            vdDest.vdDragEnter += new VectorDraw.Professional.Control.DragEnterEventHandler(vectorDrawBaseControl1_vdDragEnter);
            vdDest.vdDragLeave += new VectorDraw.Professional.Control.DragLeaveEventHandler(vectorDrawBaseControl1_vdDragLeave);
            vdDest.vdDragOver += new VectorDraw.Professional.Control.DragOverEventHandler(vectorDrawBaseControl1_vdDragOver);
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //Create a new data object that contains an existing file and begin a Drag drop operation.
            DataObject data = new DataObject();
            System.Collections.Specialized.StringCollection filepaths = new System.Collections.Specialized.StringCollection();
            filepaths.Add(Application.StartupPath + "\\exemplo.dwg");
            data.SetFileDropList(filepaths);
            listBox1.DoDragDrop(data, DragDropEffects.Copy);
        }

        private void vectorDrawBaseControl1_vdDragDrop(DragEventArgs drgevent, ref bool cancel)
        {
            cancel = true;
            if (Winsert == null) return;
            //Add the insert object in to Document ActiveLayout entities.
            vdDocument docAcess = vdDest.ActiveDocument;
            Winsert.Invalidate();
            Winsert.InsertionPoint = docAcess.CCS_CursorPos();
            Winsert.Update();
            docAcess.ActiveLayOut.Entities.AddItem(Winsert);
            Winsert.Invalidate();
            Winsert = null;
        }

        private void vectorDrawBaseControl1_vdDragEnter(DragEventArgs drgevent, ref bool cancel)
        {
            //A drag drop action is active and the cursor is just activate in the VectorDraw screen
            cancel = true;
            vdDocument docAccess = vdDest.ActiveDocument;
            //get the data object and check if is represents a drawing file.
            DataObject dataobject = drgevent.Data as DataObject;
            if (dataobject == null) return;
            System.Collections.Specialized.StringCollection strings = dataobject.GetFileDropList();
            if (strings == null || strings.Count != 1) return;
            string filename = strings[0];
            string blockname = System.IO.Path.GetFileNameWithoutExtension(filename);//get th block name of the file
            vdBlock block = docAccess.Blocks.FindName(blockname);//if the block already exist in the drawing then we do not redifine it.
            if (block == null)
            {
                Cursor curCursor = Cursor;
                Cursor = Cursors.WaitCursor;
                block = docAccess.Blocks.AddFromFile(filename, false);//add the block in the drawing
                Cursor = curCursor;
            }

            if (block == null) return;
            drgevent.Effect = DragDropEffects.Copy;

            //create an insert object but we do not add it in the Document ActiveLayout entities.
            Winsert = new vdInsert();
            Winsert.SetUnRegisterDocument(docAccess);
            Winsert.setDocumentDefaults();
            Winsert.Block = block;
            Winsert.CreateDefaultAttributes();
            Winsert.InsertionPoint = docAccess.CCS_CursorPos();
            Winsert.Update();
        }

        private void vectorDrawBaseControl1_vdDragLeave(EventArgs e, ref bool cancel)
        {  //Leaving the VectorDraw control 
            cancel = true;
            Winsert = null;
            vdDest.ActiveDocument.ActiveLayOut.Refresh();
        }

        private void vectorDrawBaseControl1_vdDragOver(DragEventArgs drgevent, ref bool cancel)
        {
            cancel = true;
            if (Winsert == null) return;
            vdDocument docAccess = vdDest.ActiveDocument;
            gPoint curpt = docAccess.CCS_CursorPos();
            curpt = docAccess.ActiveLayOut.User2WorldMatrix.Transform(curpt);
            Winsert.InsertionPoint = curpt;
            Winsert.Update();

            vdRender render = docAccess.ActiveActionRender;
            // Draw the insert in the mouse position
            bool isstarted = render.Started;
            if (!isstarted) render.StartDraw(true);
            if (render.Started)
            {
                Winsert.Draw(render);
                if (!isstarted) render.EndDraw();
            }
        }
    }
}

Send comments on this topic.