private void FolderTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
// Get the bounds of the node
Rectangle nodeRect = e.Node.Bounds;
// Clear the icon area before redrawing
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); // Clear previous drawing
/*--------- 1. Draw expand/collapse icon ---------*/
if (e.Node.Nodes.Count > 0)
{
// Calculate position for expand/collapse icon
Point ptExpand = new Point(nodeRect.Left - 16, nodeRect.Top + (nodeRect.Height - 16) / 2); // Align vertically
// Choose the appropriate icon
Image expandCollapseImg = e.Node.IsExpanded ? collapseIcon : expandIcon;
// Draw the icon (ensure it is redrawn over a clear background)
e.Graphics.DrawImage(expandCollapseImg, ptExpand);
}
/*--------- 2. Draw node text ---------*/
// Get the node's font (default if none is set)
Font nodeFont = e.Node.NodeFont ?? ((TreeView)sender).Font;
// Set the color for the text (highlight if selected)
Brush textBrush = SystemBrushes.WindowText;
if ((e.State & TreeNodeStates.Selected) != 0)
{
textBrush = SystemBrushes.HighlightText;
e.Graphics.FillRectangle(SystemBrushes.Highlight, nodeRect); // Highlight background
}
// Draw the text
e.Graphics.DrawString(e.Node.Text, nodeFont, textBrush, nodeRect);
// Draw focus rectangle if node is selected
if ((e.State & TreeNodeStates.Focused) != 0)
{
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
}
}
этот артефакт возникает во многих узлах. я просто показываю несколько примеров.
и здесь я отметил артефакты справа красными кружками
Не могу понять, почему появляются эти артефакты и как их убрать? вверху: [code]public partial class CustomFolderBrowser : Form { private TreeView folderTreeView; private Button okButton; private Button cancelButton; private Button makeNewFolderButton; private BufferedLabel feedbackLabel;
public string SelectedPath { get; private set; } [/code] затем в событии drawode: [code]private void FolderTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { // Get the bounds of the node Rectangle nodeRect = e.Node.Bounds;
// Clear the icon area before redrawing e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); // Clear previous drawing
/*--------- 1. Draw expand/collapse icon ---------*/ if (e.Node.Nodes.Count > 0) { // Calculate position for expand/collapse icon Point ptExpand = new Point(nodeRect.Left - 16, nodeRect.Top + (nodeRect.Height - 16) / 2); // Align vertically
// Draw the icon (ensure it is redrawn over a clear background) e.Graphics.DrawImage(expandCollapseImg, ptExpand); }
/*--------- 2. Draw node text ---------*/ // Get the node's font (default if none is set) Font nodeFont = e.Node.NodeFont ?? ((TreeView)sender).Font;
// Set the color for the text (highlight if selected) Brush textBrush = SystemBrushes.WindowText; if ((e.State & TreeNodeStates.Selected) != 0) { textBrush = SystemBrushes.HighlightText; e.Graphics.FillRectangle(SystemBrushes.Highlight, nodeRect); // Highlight background }
// Draw the text e.Graphics.DrawString(e.Node.Text, nodeFont, textBrush, nodeRect);
// Draw focus rectangle if node is selected if ((e.State & TreeNodeStates.Focused) != 0) { ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds); } } [/code] этот артефакт возникает во многих узлах. я просто показываю несколько примеров. [img]https://i.sstatic.net/TfQIDSJj.jpg[/img]
и здесь я отметил артефакты справа красными кружками [img]https://i.sstatic.net/gYWm846I.jpg[/img]