[英]How to Drag item from C# form and drop on other application?
我想知道如何在C#中从treeview拖动项目并放入其他(autocad)应用程序。 该项基本上是autocad文件.dwg。
我写了一些代码:
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode n = (TreeNode)e.Item;
this.treeView1.DoDragDrop(AcadObj, DragDropEffects.Copy);
}
AcadObj是一个autocad对象。
这个事件运行正常,但事件DragDrop没有触发,即使我拖动autocad鼠标指针给我加号(这里接受文件)。 我的DragDrop事件是:
private void treeview1_DragDrop(object sender, DragEventArgs e){
MessageBox.Show("It works");
}
上面的事件不起作用,没有向我展示MessageBox。 我只实现了这两个事件。
请帮助我并指导我如何做到这一点
问题是因为内存空间。 你看,.NET将其内存存储在CLR中。 这意味着,您无法使用.NET拖放将任何内容从.NET拖放到另一个在不同内存空间中运行的应用程序中。
您必须使用进程间拖放。
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实现中,则可以将drop拖放到任何应用程序中。
我会发一个例子,但是我在源代码中找不到一个足够“干净”的帖子作为例子。 谷歌周围。 寻找使用C ++ COM的拖放实现。 使用它代替拖放中内置的.NET。
实际上,OP没有传达所有细节。 首先,他很可能已经在相同的存储空间中运行,因为99%的AutoCAD相关编程都是在进程中完成的。 第二,什么版本的AutoCAD和什么版本的.NET? 在不知道这两个关键数据的情况下,任何答案可能都不正确。
话虽如此,如果你进入AutoCAD,树视图控件上的DragDrop事件永远不会触发 - 事件是放在树视图上! 处理AutoCAD时,从树视图调用DoDrop也不是最佳选择。 您应该做的是调用AutoCAD Application的DoDragDrop事件:
AcApp.DoDragDrop(source,data,DragDropEffects.All,New DropTargetNotifier())
DropTargetNotifier处理已删除的数据,并且是您放置消息框的位置
要使TreeView控件能够拖放到AutoCAD绘图区域,您需要在AutoCAD的PaletteSet中托管/嵌入您的控件:这是我的示例(我在这里使用ListBox即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;
}
一旦能够在PaletteSet中显示TreeView,就可以调用AutoCAD的应用程序DragDrop方法。 以下是dragdropentity类的代码段:
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()是您自己的处理Drop事件的类。 这是我的代码:
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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.