private void treeView1_DragEnter(object sender, DragEventArgs e)
{
if ( e.Data.GetDataPresent("System.Windows.Forms.TreeNode") )
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
StartTV = ( (TreeView) sender );
TreeNode TempNode = (TreeNode) e.Data.GetData("System.Windows.Forms.TreeNode");
if ( e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false) )
{
Point pt;
TreeNode DestNode;
pt = ( (TreeView) sender ).PointToClient(new Point(e.X, e.Y));
DestNode = ( (TreeView) sender ).GetNodeAt(pt);
treeView1.SelectedNode = DestNode;
if ( StartTV.Left > EndTV.Left )
DrawLine(StartTV, EndTV);
else
DrawLine(EndTV, StartTV);
}
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
EndTV = ( (TreeView) sender );
TreeNode SelectNode = new TreeNode();
foreach ( TreeNode nodes in treeView1.Nodes )
{
foreach ( TreeNode node in nodes.Nodes )
{
if ( node.ToString().Trim() == e.Item.ToString().Trim() )
{
SelectNode = node;
break;
}
}
}
treeView1.SelectedNode = SelectNode;
treeView1.DoDragDrop(treeView1.SelectedNode, DragDropEffects.Copy);
}
private void treeView2_DragDrop(object sender, DragEventArgs e)
{
StartTV = ( (TreeView) sender );
TreeNode TempNode = (TreeNode) e.Data.GetData("System.Windows.Forms.TreeNode");
if ( e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false) )
{
Point pt;
TreeNode DestNode;
pt = ( (TreeView) sender ).PointToClient(new Point(e.X, e.Y));
DestNode = ( (TreeView) sender ).GetNodeAt(pt);
treeView2.SelectedNode = DestNode;
if ( StartTV.Left > EndTV.Left )
DrawLine(StartTV, EndTV);
else
DrawLine(EndTV, StartTV);
}
}
private void treeView2_DragEnter(object sender, DragEventArgs e)
{
if ( e.Data.GetDataPresent("System.Windows.Forms.TreeNode") )
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void treeView2_ItemDrag(object sender, ItemDragEventArgs e)
{
EndTV = ( (TreeView) sender );
TreeNode SelectNode = new TreeNode();
foreach ( TreeNode nodes in treeView2.Nodes )
{
foreach ( TreeNode node in nodes.Nodes )
{
if ( node.ToString().Trim() == e.Item.ToString().Trim() )
{
SelectNode = node;
break;
}
}
}
treeView2.SelectedNode = SelectNode;
treeView2.DoDragDrop(treeView2.SelectedNode, DragDropEffects.Copy);
}
private void DrawLine(TreeView TV1, TreeView TV2)
{
TreeNode tn1 = TV1.SelectedNode;
TreeNode tn2 = TV2.SelectedNode;
Point p11;
p11 = new Point(tn1.Bounds.Left, tn1.Bounds.Top + ( tn1.Bounds.Bottom - tn1.Bounds.Top ) / 2);
Point p12;
p12 = new Point(-10, tn1.Bounds.Top + ( tn1.Bounds.Bottom - tn1.Bounds.Top ) / 2);
Point p21;
p21 = new Point(tn2.Bounds.Right, tn2.Bounds.Top + ( tn2.Bounds.Bottom - tn2.Bounds.Top ) / 2);
Point p22;
p22 = new Point(tn2.Bounds.Right + tn2.Bounds.Width + tn2.Bounds.Left, tn2.Bounds.Top + ( tn2.Bounds.Bottom - tn2.Bounds.Top ) / 2);
Point p31;
Point p32;
p31 = new Point(TV2.Width + TV2.Left, TV2.Top + tn2.Bounds.Top + ( tn2.Bounds.Bottom - tn2.Bounds.Top ) / 2);
p32 = new Point(TV1.Left, TV1.Top + tn1.Bounds.Top + ( tn1.Bounds.Bottom - tn1.Bounds.Top ) / 2);
TreeView[] tv = new TreeView[] { TV1,TV2};
lTV.Add(tv);
Point[] p = new Point[] { p11, p12, p21, p22, p31, p32 };
lp.Add(p);
DrawLine();
}
private void DrawLine()
{
this.Refresh();
for ( int i = 0; i < lp.Count; i++ )
{
TreeView[] tt = lTV[i];
Point[] linePoint = lp[i];
Graphics g1 = tt[0].CreateGraphics();
g1.DrawLine(new Pen(Color.Red), linePoint[0], linePoint[1]);
g1.SmoothingMode = SmoothingMode.HighQuality;
Graphics g2 = tt[1].CreateGraphics();
g2.DrawLine(new Pen(Color.Red), linePoint[2], linePoint[3]);
g2.SmoothingMode = SmoothingMode.HighQuality;
Graphics g3 = this.groupBox1.CreateGraphics();
g3.DrawLine(new Pen(Color.Red), linePoint[4], linePoint[5]);
g3.SmoothingMode = SmoothingMode.HighQuality;
}
}
结果如图:想请教高手怎样实现两点间之间画线,同时线段不要出现锯齿