ShowPlusMinus
and/orShowRootLines
のときに表示されるプラス ( + ) およびマイナス ( - ) の画像から、展開/折りたたみの画像を変更するにはどうすればよいですかtrue
。
視覚化を支援するために、次の TreeView を作成したいと思います
このように見えます(Windowsエクスプローラーのように)
Ivan Ičinのソリューションの拡張:
[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
public static void SetTreeViewTheme(IntPtr treeHandle) {
SetWindowTheme(treeHandle, "explorer", null);
}
TreeView
使用するには、フォームに を追加し、にForm_Load
:
SetTreeViewTheme( treeView1.Handle );
または、TreeView オブジェクトを拡張することもできます
public class MyTreeView : TreeView
{
[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
public MyTreeView() {
SetWindowTheme(this.Handle, "explorer", null);
}
}
私が考えることができる3つの方法があります:
サニーはすでに使用について言及しましたSetWindowTheme(TreeView.Handle, "explorer", null)
それがオプションである場合はWPFを使用し、TreeViewItemオブジェクトを追加します
複雑すぎる OnPaint メソッドのオーバーライドは、1 つしか実行できないため、1 つまたは 2 つを選択するのはあなた次第です。
ツリービュー コントロールをカスタマイズする場合、Microsoft はツリービュー コントロールに「TreeViewDrawMode」という名前のプロパティを提供します。その値は、Normal、OwnerDrawText、OwnerDrawAll の 3 つの値を持つ列挙型です。状況によっては、OwnerDrawAll を使用する必要があります。そのプロパティを OwnerDrawAll として設定した後、ツリービューのノードが表示されているときに、「DrawNode」という名前のイベントがトリガーされるため、そこで描画を処理できます。自分で描画する場合、通常、展開/折りたたみアイコン、ノード アイコン、ノード テキストの 3 つを描画する必要があります。私のサンプルは次のとおりです。 //アイコン ファイル パスを定義します。string plusPath = Application.StartupPath + Path.DirectorySeparatorChar + "plus.png"; 文字列 nodePath = アプリケーション。
public FrmTreeView()
{
InitializeComponent();
//setting to customer draw
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
this.treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
}
void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Rectangle nodeRect = e.Node.Bounds;
/*--------- 1. draw expand/collapse icon ---------*/
Point ptExpand = new Point(nodeRect.Location.X - 20, nodeRect.Location.Y + 2);
Image expandImg = null;
if (e.Node.IsExpanded || e.Node.Nodes.Count < 1)
expandImg = Image.FromFile(minusPath);
else
expandImg = Image.FromFile(plusPath);
Graphics g = Graphics.FromImage(expandImg);
IntPtr imgPtr = g.GetHdc();
g.ReleaseHdc();
e.Graphics.DrawImage(expandImg, ptExpand);
/*--------- 2. draw node icon ---------*/
Point ptNodeIcon = new Point(nodeRect.Location.X - 4, nodeRect.Location.Y + 2);
Image nodeImg = Image.FromFile(nodePath);
g = Graphics.FromImage(nodeImg);
imgPtr = g.GetHdc();
g.ReleaseHdc();
e.Graphics.DrawImage(nodeImg, ptNodeIcon);
/*--------- 3. draw node text ---------*/
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null)
nodeFont = ((TreeView)sender).Font;
Brush textBrush = SystemBrushes.WindowText;
//to highlight the text when selected
if ((e.State & TreeNodeStates.Focused) != 0)
textBrush = SystemBrushes.HotTrack;
//Inflate to not be cut
Rectangle textRect = nodeRect;
//need to extend node rect
textRect.Width += 40;
e.Graphics.DrawString(e.Node.Text, nodeFont, textBrush, Rectangle.Inflate(textRect, -12, 0));
}
私のテストの結果は次のようになります。