
A 回答 (3件)
- 最新から表示
- 回答順に表示
No.3
- 回答日時:
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
if (e.Button == MouseButtons.Left && allowLine == false)
{
allowLine = true;
if (this.VisibleDraggedNodeLabel == true)
{
motionLabel.Visible = true;
}
}
if (allowLine == true)
{
//motionLabel.Location = Cursor.Position;
Point client = treeView1.PointToClient(Cursor.Position);
if (clickedNode != null && this.VisibleDraggedNodeLabel==true)
{
motionLabel.Location = new Point(treeView1.Bounds.X+clickedNode.Bounds.X+(clickedNode.Bounds.Width/2),
treeView1.Bounds.Y+clickedNode.Bounds.Y+(clickedNode.Bounds.Height/2));
}
treeView1.Invalidate();
}
}
}
TreeViewクラス、TreeNodeクラス、TreeNode.DrawModeプロパティのヘルプを参考になさってください。
No.2
- 回答日時:
(承前)
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;
}
No.1
- 回答日時:
VB.netの環境がなく、C#で申し訳ありませんが…。
treeView1と、motionLabel(Label)を配置します。
treeView.DrawModeは、OwnerDrawTextとします。
VisibleDraggedNodeLabelがTrueの場合はドラッグ時にドラッグをしているノード名がmotionLabelに表示されます。不要ならばfalse。
TreeViewの描画はDrawNode内部で。コンポーネントの操作や再描画契機(Invalidate)の発行はMouseXXXイベントで行なっています。
この事例では、ノード間のドラッグアンドドロップに対応しており、アイコンなどを外側からドラッグしてくる状況を考える場合にはイベントなどを考慮する必要があります。
public partial class Form1 : Form
{
int drawcount = 0;
private bool allowLine = false;
Pen pen;
//Graphics g = null;
Font tagFont = new Font("Helvetica", 8, FontStyle.Bold);
private bool _visibleMotionLabel = true;
public bool VisibleDraggedNodeLabel{
get
{
return _visibleMotionLabel;
}
set
{
_visibleMotionLabel = value;
}
}
private void Form1_Load(object sender, EventArgs e)
{
allowLine = false;
motionLabel.Location = new Point(-100, 10);
motionLabel.BackColor = Color.Transparent;
this.VisibleDraggedNodeLabel = true;
pen = new Pen(Color.Black, 2);
}
private void treeView1_MouseLeave(object sender, EventArgs e)
{
treeView1.Capture = false;
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
treeView1.Capture = false;
if (allowLine == true)
{
motionLabel.Location = new Point(-100, 10);
motionLabel.Visible = false;
allowLine = false;
}
treeView1.Invalidate();
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- XML マスターノード 1 2023/03/14 10:38
- Excel(エクセル) エクセルの折れ線グラフで教えて下さい 1 2023/03/05 22:48
- Visual Basic(VBA) ScilabのWaveletに関する質問です。 1 2022/04/25 08:58
- アプリ Edgeを操作するアプリについて 4 2023/05/11 22:48
- その他(Microsoft Office) EXCELでバーコードを作成すると白くなってコードが見えません 1 2022/04/04 14:24
- Excel(エクセル) 折れ線グラフの描き方 2 2022/07/11 10:12
- PDF 「PDF文書を簡単にWordで編集する方法」と 罫線が 図形で出力されるのは? 6 2022/06/14 06:51
- Word(ワード) Wordで作った「表の罫線を部分的に削除したい」 4 2023/07/24 07:00
- Excel(エクセル) 【スプレッドシート】日報を統合して各業務の所要時間をピボットで表示したい 1 2023/07/06 16:49
- その他(職業・資格) 消防設備士 第4類の配線について 2 2023/07/06 12:52
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
eclipseでoracle接続時のエラー...
-
Java配列でNullPointerExceptio...
-
ORA-01858: 数値を指定する箇所...
-
C# ListBoxのインデックスの値...
-
配列にnullを代入すると、null...
-
JSPやサーブレットでSystem.out...
-
InputStreamはreadが1回しかで...
-
イメージボタンを作成したいの...
-
【Java】再帰的なプログラムで...
-
C#で、あるクラスのメンバーす...
-
System.err. printlnとSystem.o...
-
stderrとstdout
-
javaアプレット repaintのタイ...
-
javaのパッケージについての質...
-
Vectorクラスの使い方
-
ランダムな英数字を発生させて...
-
flush()とclose()について
-
java Threadのsleepを途中で止...
-
実数からの小数部の取得
-
JavaBeansの配列の値取得方法に...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
ORA-01858: 数値を指定する箇所...
-
配列にnullを代入すると、null...
-
BOOL値を逆にしたい
-
getStringの値がNULLの時の処理
-
Java配列でNullPointerExceptio...
-
javaの掲示板について
-
eclipseでoracle接続時のエラー...
-
C#でラジオボタンとコンボボッ...
-
例外でBeanUtils.populateが、...
-
C# ListBoxのインデックスの値...
-
逆コンパイルの見方について(...
-
Tomcatを再起動するとエラーが...
-
空欄のテキストフィールドの判...
-
jsp/Servletの動的に増えるフォ...
-
プログラミングの質問です
-
入力された文字列の制限
-
androidアプリが強制終了してし...
-
if文を通らない
-
サーブレット、JSP、Mysqlについて
-
Stringを返す getText()メソッ...
おすすめ情報