(承前)
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
treeView1.Capture = true;
TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
if (clickedNode != null)
{
if (NodeBounds(clickedNode).Contains(e.X, e.Y))
{
treeView1.SelectedNode = clickedNode;
if (this.VisibleDraggedNodeLabel == true) motionLabel.Text = clickedNode.Text;
}
}
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
drawcount++;
this.Text = drawcount.ToString() + " " + e.State;
// Draw the background and node text for a selected node.
if ((e.State & TreeNodeStates.Selected) != 0)
{
e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
// Draw the node text.
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
e.Node.Bounds.X + 2, e.Node.Bounds.Y);
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = NodeBounds(e.Node);
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
else
{// Use the default background and node text.
e.DrawDefault = true;
}
if (allowLine == true)
{
Graphics g = e.Graphics;
Point client = treeView1.PointToClient(Cursor.Position);
TreeNode clickedNode = treeView1.GetNodeAt(client);
if (clickedNode != null)
{
int x = treeView1.Width;
int y = clickedNode.Bounds.Y;
//g.DrawArc(pen, -4, y - 4, 8, 8, 0, 360);
g.FillPolygon(Brushes.Black, new Point[]{new Point(0+5,y-4),
new Point(0+5,y+4),
new Point(4+5,y)});
g.DrawLine(pen, 3 + 5, y, x - 3 - 5, y); //y = clickedNode.bound.y
g.FillPolygon(Brushes.Black, new Point[]{new Point(x-5,y-4),
new Point(x-5,y+4),
new Point(x-4-5,y)});
}
}
// If a node tag is present, draw its string representation
// to the right of the label text.
if (e.Node.Tag != null)
{
e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont,
Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);
}
// If the node has focus, draw the focus rectangle large, making
// it large enough to include the text of the node tag, if present.
if (((e.State & TreeNodeStates.Focused) != 0) ||
(e.State & TreeNodeStates.Selected) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = NodeBounds(e.Node);
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
private Rectangle NodeBounds(TreeNode node)
{
// Set the return value to the normal node bounds.
Rectangle bounds = node.Bounds;
if (node.Tag != null)
{
// Retrieve a Graphics object from the TreeView handle
// and use it to calculate the display width of the tag.
Graphics g = treeView1.CreateGraphics();
int tagWidth = (int)g.MeasureString
(node.Tag.ToString(), tagFont).Width + 6;
// Adjust the node bounds using the calculated value.
bounds.Offset(tagWidth/2, 0);
bounds = Rectangle.Inflate(bounds, tagWidth/2, 0);
g.Dispose();
}
return bounds;
}