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 2 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 @@ -2734,6 +2734,13 @@ private unsafe void CustomDraw(ref Message m)
// as background color.
if (_drawMode == TreeViewDrawMode.OwnerDrawText)
{
if (node is { IsEditing: true })
{
nmtvcd->clrText = ColorTranslator.ToWin32(BackColor);
nmtvcd->clrTextBk = ColorTranslator.ToWin32(BackColor);
goto default;
}

nmtvcd->clrText = nmtvcd->clrTextBk;
m.ResultInternal = (LRESULT)(nint)(PInvoke.CDRF_NEWFONT | PInvoke.CDRF_NOTIFYPOSTPAINT);
return;
Expand Down Expand Up @@ -2792,6 +2799,11 @@ private unsafe void CustomDraw(ref Message m)
nmtvcd->clrTextBk = ColorTranslator.ToWin32(riBack);
}

if (node is { IsEditing: true })
{
nmtvcd->clrTextBk = ColorTranslator.ToWin32(BackColor);
}

if (renderinfo is not null && renderinfo.Font is not null)
{
// Mess with the DC directly...
Expand Down Expand Up @@ -2826,8 +2838,36 @@ private unsafe void CustomDraw(ref Message m)
{
Rectangle bounds = node.Bounds;
Size textSize = TextRenderer.MeasureText(node.Text, node.TreeView!.Font);
Point textLoc = new(AppContextSwitches.MoveTreeViewTextLocationOnePixel ? bounds.X : bounds.X - 1, bounds.Y);
bounds = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
Point textLocation = new(AppContextSwitches.MoveTreeViewTextLocationOnePixel ? bounds.X : bounds.X - 1, bounds.Y);
bounds = new Rectangle(textLocation, new Size(textSize.Width, bounds.Height));

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

if (RightToLeft == RightToLeft.Yes && RightToLeftLayout)
{
int borderWidth = BorderStyle switch
{
BorderStyle.FixedSingle => 1,
BorderStyle.Fixed3D => 2,
_ => 0
};

// Reverse the X-axis drawing coordinates of the rectangle.
int invertedX = Width - bounds.X - textSize.Width - borderWidth * 2;

// Subtract the scroll bar width when the scroll bar appears.
if (Height - borderWidth * 2 < PreferredHeight)
{
float dpiScale = (float)DeviceDpi / (float)ScaleHelper.InitialSystemDpi;
invertedX -= (int)(SystemInformation.VerticalScrollBarWidth * Math.Round(dpiScale, 2));
}

// To ensure that the right side of the fillRectangle does not
// touch the left edge of the node prefix symbol, 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,14 +2883,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 All @@ -2870,6 +2910,35 @@ private unsafe void CustomDraw(ref Message m)
}
}

private int PreferredHeight
{
get
{
int height = 0;
foreach (TreeNode node in Nodes)
{
height += GetNodeHeight(node);
}

return height;
}
}

private int GetNodeHeight(TreeNode node)
{
int height = ItemHeight;

if (node.IsExpanded)
{
foreach (TreeNode childNode in node.Nodes)
{
height += GetNodeHeight(childNode);
}
}

return height;
}

/// <summary>
/// Generates colors for each item. This can be overridden to provide colors on a per state/per node
/// basis, rather than using the ForeColor/BackColor/NodeFont properties on TreeNode.
Expand Down
Loading