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 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -2825,10 +2825,30 @@ private unsafe void CustomDraw(ref Message m)
using (Graphics g = nmtvcd->nmcd.hdc.CreateGraphics())
{
Rectangle bounds = node.Bounds;
Rectangle fillRectangle = node.Bounds;
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
Rectangle focusRectangle = 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
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));

if (RightToLeft == RightToLeft.Yes && RightToLeftLayout)
{
// Reverse the X-axis drawing coordinates of the rectangle.
int invertedX = Width - 2 - bounds.X - textSize.Width;
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
textLoc = new(invertedX - 2, bounds.Y);
fillRectangle = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
textLoc = new(invertedX - 1, bounds.Y);
focusRectangle = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
}
else
{
textLoc = new(bounds.X - 1, bounds.Y);
fillRectangle = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
focusRectangle = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
}

DrawTreeNodeEventArgs e = new(g, node, bounds, (TreeNodeStates)(nmtvcd->nmcd.uItemState));
OnDrawNode(e);

Expand All @@ -2843,15 +2863,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
Loading