Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 67c0bdc

Browse files
committed
Fix inconsistent look of treeview items. (ISSUE#704)
- avoid drawing text using gdi+ on native win32 control - avoid drawing custom focus rectangle - use proper font as default
1 parent 31a286b commit 67c0bdc

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

src/Main/Base/Project/Src/Gui/Components/ExtTreeView/ExtTreeNode.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -252,35 +252,22 @@ public bool DrawDefault {
252252
protected virtual void DrawBackground(DrawTreeNodeEventArgs e)
253253
{
254254
Graphics g = e.Graphics;
255-
int width = MeasureItemWidth(e) + 2;
255+
int width = MeasureItemWidth(e);
256256
Rectangle backRect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);
257257

258-
if ((e.State & (TreeNodeStates.Selected | TreeNodeStates.Focused)) == TreeNodeStates.Selected) {
258+
if ((e.State & SelectedAndFocused) == TreeNodeStates.Selected) {
259259
g.FillRectangle(SystemBrushes.Control, backRect);
260260
} else if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected) {
261261
g.FillRectangle(SystemBrushes.Highlight, backRect);
262+
ControlPaint.DrawFocusRectangle(e.Graphics, backRect);
262263
} else {
263264
g.FillRectangle(SystemBrushes.Window, backRect);
264265
}
265-
266-
if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused) {
267-
backRect.Width--;
268-
backRect.Height--;
269-
using (Pen dottedPen = new Pen(SystemColors.WindowText)) {
270-
dottedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
271-
g.DrawRectangle(dottedPen, backRect);
272-
Color h = SystemColors.Highlight;
273-
dottedPen.Color = Color.FromArgb(255 - h.R, 255 - h.G, 255 - h.B);
274-
dottedPen.DashOffset = 1;
275-
g.DrawRectangle(dottedPen, backRect);
276-
}
277-
g.DrawLine(SystemPens.WindowText, backRect.Right + 1, backRect.Y, backRect.Right + 1, backRect.Bottom);
278-
}
279266
}
280267

281268
protected virtual int MeasureItemWidth(DrawTreeNodeEventArgs e)
282269
{
283-
return MeasureTextWidth(e.Graphics, Text, TreeView.Font);
270+
return MeasureTextWidth(e.Graphics, Text, e.Node.NodeFont);
284271
}
285272

286273
protected virtual void DrawForeground(DrawTreeNodeEventArgs e)
@@ -296,27 +283,22 @@ public void Draw(DrawTreeNodeEventArgs e)
296283
// Helper routines
297284
protected int MeasureTextWidth(Graphics g, string text, Font font)
298285
{
299-
SizeF size = g.MeasureString(text, font);
286+
var size = TextRenderer.MeasureText(text, font);
300287
return (int)size.Width;
301288
}
302289

303290
const TreeNodeStates SelectedAndFocused = TreeNodeStates.Selected | TreeNodeStates.Focused;
304291

305-
protected void DrawText(DrawTreeNodeEventArgs e, string text, Brush brush, Font font)
292+
protected void DrawText(DrawTreeNodeEventArgs e, string text, Color color, Font font)
306293
{
307294
float x = e.Bounds.X;
308-
DrawText(e, text, brush, font, ref x);
295+
DrawText(e, text, color, font, ref x);
309296
}
310297

311-
protected void DrawText(DrawTreeNodeEventArgs e, string text, Brush brush, Font font, ref float x)
298+
protected void DrawText(DrawTreeNodeEventArgs e, string text, Color color, Font font, ref float x)
312299
{
313-
if ((e.State & SelectedAndFocused) == SelectedAndFocused) {
314-
brush = SystemBrushes.HighlightText;
315-
}
316-
e.Graphics.DrawString(text, font, brush, new PointF(x, e.Bounds.Y));
317-
318-
SizeF size = e.Graphics.MeasureString(text, font);
319-
x += size.Width;
300+
color = GetTextColor(e.State, color);
301+
TextRenderer.DrawText(e.Graphics, text, font, new Point((int) x, e.Bounds.Y), color);
320302
}
321303

322304
protected Color GetTextColor(TreeNodeStates state, Color c)
@@ -353,7 +335,7 @@ public static Font ItalicMonospacedFont {
353335

354336
public static Font RegularDefaultFont {
355337
get {
356-
return TreeView.DefaultFont;
338+
return SystemFonts.MessageBoxFont;
357339
}
358340
}
359341

src/Main/Base/Project/Src/Gui/Components/ExtTreeView/ExtTreeView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ protected override void OnDrawNode(DrawTreeNodeEventArgs e)
362362
// HACK: work around TreeView bug in OwnerDrawText mode:
363363
// overpaint blue selection with the correct gray selection
364364
e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds);
365-
e.Graphics.DrawString(e.Node.Text, this.Font, SystemBrushes.ControlText, e.Bounds.Location);
365+
TextRenderer.DrawText(e.Graphics, e.Node.Text, this.Font, e.Bounds.Location, SystemColors.ControlText);
366366
e.DrawDefault = false;
367367
} else {
368368
e.DrawDefault = true;

src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/ProjectNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected override int MeasureItemWidth(DrawTreeNodeEventArgs e)
123123
protected override void DrawForeground(DrawTreeNodeEventArgs e)
124124
{
125125
if (isStartupProject) {
126-
DrawText(e, this.Text, SystemBrushes.WindowText, BoldDefaultFont);
126+
DrawText(e, this.Text, SystemColors.WindowText, BoldDefaultFont);
127127
}
128128
}
129129

src/Main/ICSharpCode.Core.WinForms/WinFormsResourceService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static Font LoadFont(string fontName, int size, FontStyle style, Graphics
153153
public static Font LoadFont(Font baseFont, FontStyle newStyle)
154154
{
155155
try {
156-
return new Font(baseFont, newStyle);
156+
return new Font(baseFont.FontFamily.Name, (int) Math.Ceiling(baseFont.Size), newStyle, baseFont.Unit);
157157
} catch (Exception ex) {
158158
LoggingService.Warn(ex);
159159
return baseFont;

0 commit comments

Comments
 (0)