How to drag an item from a C # form and abandon another application?

I want to know how we can drag an item from a tree to C # and drop another application (autocad). This item is mainly an autocad.dwg file.

I wrote the code:

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{     

     TreeNode n = (TreeNode)e.Item;
     this.treeView1.DoDragDrop(AcadObj, DragDropEffects.Copy);

}

AcadObj is an autocad object.

This event works fine, but the DragDrop event does not fire, even when I drag the autocad, the mouse pointer gives me a plus sign (that the file is accepted here). My DragDrop event:

private void treeview1_DragDrop(object sender, DragEventArgs e){

       MessageBox.Show("It works");
}

The above event does not work and does not show me a MessageBox. I implemented only these two events.

Please help me and advise me how I can do this.

+3
source share
3 answers

. , .NET CLR. , - .NET , , .NET dragdrop.

.

WINOLEAPI DoDragDrop( 
  IDataObject * pDataObject,  //Pointer to the data object
  IDropSource * pDropSource,  //Pointer to the source
  DWORD dwOKEffect,           //Effects allowed by the source
  DWORD * pdwEffect           //Pointer to effects on the source
);

, IDataObject, .

, , "", . Google . ++ COM. .NET.

+3

, . -, , , , 99% , AutoCAD, . -, AutoCAD verson.NET? , .

, DragDrop treeview , AutoCAD - ! AutoCAD DoDrop treeview . , AutoCAD Application DoDragDrop:

AcApp.DoDragDrop(, , DragDropEffects.All, New DropTargetNotifier())

DropTargetNotifier

+3

So that your TreeView control can drag and draw in the AutoCAD drawing area, you will need to place / paste your control in the AutoCAD PaletteSet: here is my example (I use ListBox here, i.e. LB):

public dragdropentity TestLB; //dragdropentity is my actuall control containing my ListBox

        [CommandMethod("ListBox")]
        public void lb()
        {
            if (this.TestLB == null)
            {
                myPaletteSet = new PaletteSet("Test ListBox", new Guid("{B32639EE-05DF-4C48-ABC4-553769C67995}"));
                TestLB = new dragdropentity();
                myPaletteSet.Add("LB", TestLB);

            }
            myPaletteSet.Visible = true;
        }

Once you can display the TreeView in the PaletteSet, you can call the DragDrop method of the AutoCAD application. Here is the code segment from the dragdropentity class:

public partial class dragdropentity : UserControl
    {
        public dragdropentity()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //            MessageBox.Show(listBox1.SelectedIndex.ToString() + '\n' + listBox1.SelectedItem.ToString());
            pictureBox1.Load(@"D:\My\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\Images\" + listBox1.SelectedItem.ToString() + ".png");
        }
        void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            int indexOfItem = listBox1.IndexFromPoint(e.X, e.Y);
            if (indexOfItem >= 0 && indexOfItem < listBox1.Items.Count)  // check that an string is selected
            {
                listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy);

            }

            //           throw new System.NotImplementedException();
        }


        void listBox1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
        {
            ListBox lb = (ListBox)sender;
            textBox1.AppendText('\n' + e.Action.ToString() + '\n'+ this.Name.ToString());

            if (e.Action == DragAction.Drop)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DoDragDrop(this, "Drag & drop successful!!!", System.Windows.Forms.DragDropEffects.All, new DragDrop());
            }
        }
}

DragDrop () is your own class that handles the Drop event. Here is my code:

 class DragDrop : DropTarget
    {

        public override void OnDrop(System.Windows.Forms.DragEventArgs e)
        {
            using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
        //        MyCommands mc = new MyCommands();
          //      mc.CircleJig();

                //Call your own methods etc here.

             }

        }
0
source

Source: https://habr.com/ru/post/1717913/


All Articles