* 增加一个以文字中心点为原点,旋转文字的通用函数

This commit is contained in:
Sunny 2021-04-06 17:59:25 +08:00
parent b5bc2fb7f1
commit 7ed46b29d6
6 changed files with 30 additions and 21 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -275,30 +275,10 @@ namespace Sunny.UI
SizeF sfName = g.MeasureString(Option.YAxis.Name, SubFont);
float xx = DrawOrigin.X - Option.YAxis.AxisTick.Length - widthMax - sfName.Width;
float yy = Option.Grid.Top + (DrawSize.Height - sfName.Height) / 2.0f;
DrawStringRotateAtCenter(g, Option.YAxis.Name, SubFont, ChartStyle.ForeColor, new PointF(xx + sfName.Width / 2.0f, yy + sfName.Height / 2.0f), 270);
g.DrawStringRotateAtCenter(Option.YAxis.Name, SubFont, ChartStyle.ForeColor, new PointF(xx + sfName.Width / 2.0f, yy + sfName.Height / 2.0f), 270);
}
}
public void DrawStringRotateAtCenter(Graphics graphics, string text, Font font, Color color, PointF centerPoint, int angle)
{
SizeF sf = graphics.MeasureString(text, font);
float x1 = centerPoint.X - sf.Width / 2.0f;
float y1 = centerPoint.Y - sf.Height / 2.0f;
// 把画板的原点(默认是左上角)定位移到文字中心
graphics.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2);
// 旋转画板
graphics.RotateTransform(angle);
// 回退画板x,y轴移动过的距离
graphics.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2));
graphics.DrawString(text, font, new SolidBrush(Color.Black), x1, y1);
//恢复
graphics.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2);
graphics.RotateTransform(-angle);
graphics.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2));
}
protected virtual void DrawSeries(Graphics g, Color color, UILineSeries series)
{
if (series.Points.Count == 0)

View File

@ -959,6 +959,35 @@ namespace Sunny.UI
g.DrawString(s, font, brush, rotatePt, newFormat, angle);
}
/// <summary>
/// 以文字中心点为原点,旋转文字
/// </summary>
/// <param name="graphics">Graphics</param>
/// <param name="text">文字</param>
/// <param name="font">字体</param>
/// <param name="color">颜色</param>
/// <param name="centerPoint">文字中心点</param>
/// <param name="angle">角度</param>
public static void DrawStringRotateAtCenter(this Graphics graphics, string text, Font font, Color color, PointF centerPoint, int angle)
{
SizeF sf = graphics.MeasureString(text, font);
float x1 = centerPoint.X - sf.Width / 2.0f;
float y1 = centerPoint.Y - sf.Height / 2.0f;
// 把画板的原点(默认是左上角)定位移到文字中心
graphics.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2);
// 旋转画板
graphics.RotateTransform(angle);
// 回退画板x,y轴移动过的距离
graphics.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2));
graphics.DrawString(text, font, new SolidBrush(Color.Black), x1, y1);
//恢复
graphics.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2);
graphics.RotateTransform(-angle);
graphics.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2));
}
/// <summary>
/// 绘制根据点旋转文本,一般旋转点给定位文本包围盒中心点
/// </summary>