* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-13 10:07:41 +08:00
parent c23e3f5634
commit fb098d6c97
2 changed files with 26 additions and 26 deletions

View File

@ -24,6 +24,7 @@
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
* 2022-09-26: V3.2.4 Readonly时 * 2022-09-26: V3.2.4 Readonly时
* 2022-04-23: V3.3.5 ActiveChanging事件 * 2022-04-23: V3.3.5 ActiveChanging事件
* 2022-05-13: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -270,14 +271,16 @@ namespace Sunny.UI
if (!Active) if (!Active)
{ {
g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6); g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6);
SizeF sf = g.MeasureString(InActiveText, Font); //SizeF sf = g.MeasureString(InActiveText, Font);
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2); //g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3 + rect.Height - 6, 0, width, rect.Height), ContentAlignment.MiddleCenter);
} }
else else
{ {
g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6); g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6);
SizeF sf = g.MeasureString(ActiveText, Font); //SizeF sf = g.MeasureString(ActiveText, Font);
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2); //g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3, 0, width, rect.Height), ContentAlignment.MiddleCenter);
} }
} }
@ -290,14 +293,16 @@ namespace Sunny.UI
if (!Active) if (!Active)
{ {
g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6, Radius); g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6, Radius);
SizeF sf = g.MeasureString(InActiveText, Font); //SizeF sf = g.MeasureString(InActiveText, Font);
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2); //g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3 + rect.Height - 6, 0, width, rect.Height), ContentAlignment.MiddleCenter);
} }
else else
{ {
g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6, Radius); g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6, Radius);
SizeF sf = g.MeasureString(ActiveText, Font); //SizeF sf = g.MeasureString(ActiveText, Font);
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2); //g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3, 0, width, rect.Height), ContentAlignment.MiddleCenter);
} }
} }
} }

View File

@ -32,6 +32,7 @@
* 2022-12-06: V3.3.0 * 2022-12-06: V3.3.0
* 2023-03-13: V3.3.3 MouseDoubleClick和MouseClick事件 * 2023-03-13: V3.3.3 MouseDoubleClick和MouseClick事件
* 2023-03-26: V3.3.4 LabelEdit属性 * 2023-03-26: V3.3.4 LabelEdit属性
* 2022-05-13: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -1051,36 +1052,30 @@ namespace Sunny.UI
{ {
if (Painter.ContainsKey(e.Node)) if (Painter.ContainsKey(e.Node))
{ {
e.Graphics.FillRectangle(Painter[e.Node].BackColor, e.Graphics.FillRectangle(Painter[e.Node].BackColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); //e.Graphics.DrawString(e.Node.Text, Font, Painter[e.Node].ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, Painter[e.Node].ForeColor, drawLeft, e.Graphics.DrawString(e.Node.Text, Font, Painter[e.Node].ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
} }
else else
{ {
if (e.Node == SelectedNode) if (e.Node == SelectedNode)
{ {
e.Graphics.FillRectangle(SelectedColor, e.Graphics.FillRectangle(SelectedColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); //e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, drawLeft,
e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
checkboxColor = SelectedForeColor; checkboxColor = SelectedForeColor;
} }
else if (e.Node == CurrentNode && (e.State & TreeNodeStates.Hot) != 0) else if (e.Node == CurrentNode && (e.State & TreeNodeStates.Hot) != 0)
{ {
e.Graphics.FillRectangle(HoverColor, e.Graphics.FillRectangle(HoverColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); //e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
} }
else else
{ {
e.Graphics.FillRectangle(FillColor, e.Graphics.FillRectangle(FillColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); //e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
} }
} }