为了使您的TreeView控件能够拖放到AutoCAD绘图区域,您需要将控件托管/嵌入到AutoCAD的PaletteSet中:这是我的示例(我在此处使用ListBox即LB):
public dragdropentity TestLB;
[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)
{
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)
{
listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy);
}
}
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()是您自己处理“拖放事件”的类。这是我的代码:
class DragDrop : DropTarget
{
public override void OnDrop(System.Windows.Forms.DragEventArgs e)
{
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
}
}