Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recalculate the node position of the TreeView when DrawMode = TreeViewDrawMode.OwnerDrawText #12698

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2826,8 +2826,24 @@ private unsafe void CustomDraw(ref Message m)
{
Rectangle bounds = node.Bounds;
Size textSize = TextRenderer.MeasureText(node.Text, node.TreeView!.Font);
Point textLoc = new(bounds.X - 1, bounds.Y); // required to center the text
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
bounds = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
Point textLocation = new(bounds.X, bounds.Y); // required to center the text

Rectangle fillRectangle = new Rectangle(textLocation, new(textSize.Width, bounds.Height));
Rectangle focusRectangle = new Rectangle(textLocation, new(textSize.Width, bounds.Height));

bounds = new Rectangle(textLocation, new(textSize.Width, bounds.Height));

if (RightToLeft == RightToLeft.Yes && RightToLeftLayout)
{
// Reverse the X-axis drawing coordinates of the rectangle.
// Leave four pixels for node prefix symbol.
int invertedX = Width - 4 - bounds.X - textSize.Width;

// To ensure that the right side of the rectangle does not
// touch the left edge of the control, 1 pixel is subtracted here.
fillRectangle = new Rectangle(new Point(invertedX - 1, bounds.Y), new(textSize.Width, bounds.Height));
focusRectangle = new Rectangle(new Point(invertedX, bounds.Y), new(textSize.Width, bounds.Height));
}

DrawTreeNodeEventArgs e = new(g, node, bounds, (TreeNodeStates)(nmtvcd->nmcd.uItemState));
OnDrawNode(e);
Expand All @@ -2843,15 +2859,14 @@ private unsafe void CustomDraw(ref Message m)
// Draw the actual node.
if ((curState & TreeNodeStates.Selected) == TreeNodeStates.Selected)
{
g.FillRectangle(SystemBrushes.Highlight, bounds);
ControlPaint.DrawFocusRectangle(g, bounds, color, SystemColors.Highlight);
g.FillRectangle(SystemBrushes.Highlight, fillRectangle);
ControlPaint.DrawFocusRectangle(g, focusRectangle, color, SystemColors.Highlight);
TextRenderer.DrawText(g, node.Text, font, bounds, color, TextFormatFlags.Default);
}
else
{
using var brush = BackColor.GetCachedSolidBrushScope();
g.FillRectangle(brush, bounds);

g.FillRectangle(brush, fillRectangle);
TextRenderer.DrawText(g, node.Text, font, bounds, color, TextFormatFlags.Default);
}
}
Expand Down