大哥大姐们,帮我实现两个TreeView控件上节点之间的画线,谢谢了!
点击左边一棵树的节点,再点右边一棵树的节点后,就实现两节点之间的画线 --------------------编程问答-------------------- 什么意思?用GDI画条线行不 --------------------编程问答-------------------- 可以就是用GDI+画线。还有不明白的加我QQ再说好吧 --------------------编程问答-------------------- QQ125548996,比较急,知道的帮帮忙,小弟 谢谢各位了 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace TreeViewDemo2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
treeView1.ExpandAll();
treeView2.ExpandAll();
}
private List<Line> lines = new List<Line>();
private Line myLine = null;
protected override void OnPaint( PaintEventArgs e )
{
base.OnPaint( e );
Graphics grfx = e.Graphics;
grfx.SmoothingMode = SmoothingMode.AntiAlias;
Pen myPen = new Pen( Color.Red, 1 );
foreach ( Line line in lines )
{
grfx.DrawLine( myPen, line.startPoint, line.endPoint );
}
}
private void treeView2_AfterSelect( object sender, TreeViewEventArgs e )
{
TreeNode node = treeView2.SelectedNode;
if ( node != null )
{
// 判断直线对象是否存在
if ( myLine == null )
{
myLine = new Line();
lines.Add( myLine );
}
myLine.endPoint.Y = node.Bounds.Bottom -
node.Bounds.Height / 2;
myLine.endPoint.X = this.Width - treeView2.Width;
// 判断直线终点是否存在,存在则画线
if ( myLine.startPoint != Point.Empty )
{
Invalidate();
myLine = null;
}
}
}
private void treeView1_AfterSelect( object sender, TreeViewEventArgs e )
{
TreeNode node = treeView1.SelectedNode;
if ( node != null )
{
// 判断直线对象是否存在
if ( myLine == null )
{
myLine = new Line();
lines.Add( myLine );
}
myLine.startPoint.Y = node.Bounds.Bottom -
node.Bounds.Height / 2;
myLine.startPoint.X = treeView1.Width;
// 判断直线终点是否存在,存在则画线
if ( myLine.endPoint != Point.Empty )
{
Invalidate();
myLine = null;
}
}
}
}
//反向展开树
public class MyTreeView : TreeView
{
public MyTreeView()
{
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x400000 | 0x4000; //WS_EX_LAYOUTRIL = 0x400000, WS_EX_LEFT = 0x4000.
return cp;
}
}
}
class Line
{
public Point startPoint;
public Point endPoint;
public Line()
{
}
public Line( Point pt )
{
startPoint = pt;
endPoint = pt;
}
}
}
注意: 在Form1.Designer.cs中的最下面 修改一下 将System.Windows.Forms.TreeView treeView2那行注释,输入private MytreeView treeView2以生成反向展开树.
#endregion
private System.Windows.Forms.TreeView treeView1;
private MyTreeView treeView2;
//private System.Windows.Forms.TreeView treeView2;
两个树控件均从工具箱中拖入窗体.此程序只能初步实现节点连线,仅提供参考.因为小弟也只是才学了一个多月的新手,希望有高人能给出更好的程序.
补充:.NET技术 , C#