Prod.: Engine, ver.: 6, ID: 60000430, HowTo : Drag & Drop a data object from a List box that represents a Drawing, in to VectorDraw Control as Insert object.

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

Article60000430
TypeHowTo
ProductEngine
Version6
Date Added2/23/2008
Submitted byLevi
Keywords

Subject

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

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);
        }

     

      


    }
}