我有一个应用程序,它有时会在 Panel
控件上绘制许多圆圈。然后我想命名每个圆圈(只是一个字母和一个数字)。我希望文本居中,以使其看起来美观。现在,我有这样的东西:
我所做的就是以该圆的中心为中心,然后执行以下操作:
Graphics.DrawString($"s{i+1}", panel.Font, new SolidBrush(Color.White), pointOnCircle.X, pointOnCircle.Y);
(pointOnCircle.X 和 Y
是中心的坐标)。正如您所看到的,它看起来有点糟糕。
我的问题是:有没有办法以某种方式计算指定字体大小和那些小圆圈半径的 X 和 Y,使其看起来居中?
使用接受的答案或@Johnny Mopp 评论的结果:
最佳答案
您需要使用 DrawString
方法的重载 that takes a StringFormat
argument然后使用 StringFormat.Alignment
和 StringFormat.LineAlignment
将字符串对齐到圆的中心和中间:
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Graphics.DrawString($"s{i + 1}", panel.Font, new SolidBrush(Color.White),
pointOnCircle.X, pointOnCircle.Y, sf);
}
关于c# - 在椭圆中间绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52745317/