C# 用于将文本精确放置在System.Label上的图形抽绳
我已在VS2008中重写了我的控件的方法:C# 用于将文本精确放置在System.Label上的图形抽绳,c#,graphics,onpaint,C#,Graphics,Onpaint,我已在VS2008中重写了我的控件的方法: void Label_OnPaint(object sender, PaintEventArgs e) { base.OnPaint(e); dim lbl = sender as Label; if (lbl != null) { string Text = lbl.Text; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAli
void Label_OnPaint(object sender, PaintEventArgs e) {
base.OnPaint(e);
dim lbl = sender as Label;
if (lbl != null) {
string Text = lbl.Text;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
if (myShowShadow) { // draw the shadow first!
e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(myShadowColor), myShadowOffset, StringFormat.GenericDefault);
}
e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(lbl.ForeColor), 0, 0, StringFormat.GenericDefault);
}
}
这是可行的,但我真的想知道如何垂直和水平居中文本。我听说过MeasureString()
方法,但我的“文本”使事情复杂化,因为它可能包括分页符
有人能告诉我怎么做吗?您可以使用
水平中心
和垂直中心
标志调用。或者,您可以创建自己的StringFormat
对象,并使用支持矩形的抽绳
重载将其传入:
StringFormat formatter = new StringFormat();
formatter.LineAlignment = StringAlignment.Center;
formatter.Alignment = StringAlignment.Center;
RectangleF rectangle = new RectangleF(0, 0, lbl.Width, lbl.Height);
e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(lbl.ForeColor), rectangle, formatter);
这是我目前使用的代码
SizeF size;
string text = "Text goes here";
size = e.Graphics.MeasureString(text, font);
x = (lineWidth / 2) - (size.Width / 2);
y = top;
e.Graphics.DrawString(text, font, Brushes.Black, x, y);
我只是想(一年后)添加一个我创建的工具,因为StringAlignment
证明不是很可靠。结果证明它与尼奥的版本非常相似
下面的代码很好地将文本垂直和水平居中。此外,我还编写了各种重载,以便提供不同的选项,使该控件的行为完全符合我的要求
以下是我的重载:
private static void DrawCenter(Label label, Graphics graphics) {
DrawCenter(label.Text, label, label.Location, label.ForeColor, graphics);
}
private void DrawCenter(string text, Label label, Graphics graphics) {
DrawCenter(text, label, label.Location, label.ForeColor, graphics);
}
private static void DrawCenter(string text, Label label, Point location, Graphics graphics) {
DrawCenter(text, label, location, label.ForeColor, graphics);
}
private static void DrawCenter(string text, Label label, Point location, Color fontColor, Graphics graphics) {
Rectangle rect = new Rectangle(location, label.Size);
SizeF lSize = graphics.MeasureString(text, label.Font, rect.Width);
PointF lPoint = new PointF(rect.X + (rect.Width - lSize.Width) / 2, rect.Y + (rect.Height - lSize.Height) / 2);
graphics.DrawString(text, label.Font, new SolidBrush(fontColor), lPoint);
}
要将这些代码用于标签的OnPaint事件,只需将问题中的原始代码修改为以下代码:
private void Label_OnPaint(object sender, PaintEventArgs e) {
base.OnPaint(e);
Label lbl = sender as Label;
if (lbl != null) {
string txt = lbl.Text;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
if (myShowShadow) { // draw the shadow first!
Point offset = new Point(lbl.Location.X - 1, lbl.Location.Y - 1)
DrawCenter(txt, lbl, offset, myShadowColor, e.Graphics);
}
DrawCenter(lbl, e.Graphics);
}
}
对于Print_Document事件,我有一个版本,如果在designer中标签周围已经有一个方框,它也会在标签周围打印一个方框:
private static void DrawCenter(string text, Label label, Point location, Color fontColor, Graphics graphics) {
Rectangle rect = new Rectangle(location, label.Size);
SizeF lSize = graphics.MeasureString(text, label.Font, rect.Width);
PointF lPoint = new PointF((rect.Width - lSize.Width) / 2, (rect.Height - lSize.Height) / 2);
graphics.DrawString(text, label.Font, new SolidBrush(fontColor), lPoint);
if (label.BorderStyle != BorderStyle.None) {
using (Pen p = new Pen(Color.Black)) {
graphics.DrawRectangle(p, rect);
}
}
}
如果你觉得这个有用,给我一个+1
~Joe您还必须使用带有
矩形的重载。TrueType暗示很可能破坏阴影效果。@SLaks:如何获取System.Label对象的DeviceContext(第一个参数)?从Paint
事件处理程序传递e.Graphics
。(Graphics
implementsidevicontext
)两者都是答案,但我只能标记一个。这一个有代码供其他人跟进。