C# – How to Drag item from C# form and drop on other application

cdrag and drop

I want to know that how can we drag item from treeview in C# and drop in other(autocad) application. That item is basically the autocad file .dwg.

I have written some 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 is working fine but the event DragDrop is not firing, even when i drag on autocad the mouse pointer gives me plus sign (that the file is accepted here). My DragDrop event is :

private void treeview1_DragDrop(object sender, DragEventArgs e){

       MessageBox.Show("It works");
}

The above event is not working and not showing me MessageBox. I have implemented only these two events.

Please helpe me and guide me how can i do this

Best Answer

The problem is because of memory space. You see, .NET stores its memory inside the CLR. This means , you cannot drag drop anything from .NET into another application running in a different memory space using the .NET dragdrop.

You have to use an interprocess drag-drop.

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

If you wrap the object you want to drag drop within your own implementation of IDataObject, you can drag drop into any application.

I would post an example, but i cant find one in my source which is "clean" enough to post as an example. Google around. Look for drag-drop implementations using C++ COM. Use that instead of the .NET built in drag drop.

Related Topic