Ada*_*ile 58 c# graphics compact-framework
我正在使用.NETCF(Windows Mobile)Graphics
类和DrawString()
方法将单个字符渲染到屏幕上.
问题是我似乎无法使其正确居中.无论我为字符串渲染的位置的Y坐标设置什么,它总是低于该值,文本大小越大Y偏移越大.
例如,在文本大小12处,偏移量约为4,但在32处,偏移量约为10.
我希望角色垂直占据它所绘制的大部分矩形并水平居中.这是我的基本代码.this
引用正在绘制的用户控件.
Graphics g = this.CreateGraphics();
float padx = ((float)this.Size.Width) * (0.05F);
float pady = ((float)this.Size.Height) * (0.05F);
float width = ((float)this.Size.Width) - 2 * padx;
float height = ((float)this.Size.Height) - 2 * pady;
float emSize = height;
g.DrawString(letter, new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular),
new SolidBrush(Color.Black), padx, pady);
Run Code Online (Sandbox Code Playgroud)
是的,我知道我可以使用标签控件来设置居中,但实际上我需要在Graphics
课堂上手动完成.
Qui*_*ome 78
我想为StringFormat对象添加另一个投票.您可以简单地使用它来指定"center,center",文本将在矩形或中心提供的中心绘制:
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
Run Code Online (Sandbox Code Playgroud)
然而,在CF中有一个问题.如果对两个值都使用中心,则会关闭TextWrapping.不知道为什么会这样,它似乎是CF的一个错误.
小智 56
要对齐文本,请使用以下内容:
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString("My String", this.Font, Brushes.Black, ClientRectangle, sf);
Run Code Online (Sandbox Code Playgroud)
请注意,此处的文本在给定范围内对齐.在此示例中,这是ClientRectangle.
Ada*_*ile 19
通过我得到的建议的组合,我想出了这个:
private void DrawLetter()
{
Graphics g = this.CreateGraphics();
float width = ((float)this.ClientRectangle.Width);
float height = ((float)this.ClientRectangle.Width);
float emSize = height;
Font font = new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular);
font = FindBestFitFont(g, letter.ToString(), font, this.ClientRectangle.Size);
SizeF size = g.MeasureString(letter.ToString(), font);
g.DrawString(letter, font, new SolidBrush(Color.Black), (width-size.Width)/2, 0);
}
private Font FindBestFitFont(Graphics g, String text, Font font, Size proposedSize)
{
// Compute actual size, shrink if needed
while (true)
{
SizeF size = g.MeasureString(text, font);
// It fits, back out
if (size.Height <= proposedSize.Height &&
size.Width <= proposedSize.Width) { return font; }
// Try a smaller font (90% of old size)
Font oldFont = font;
font = new Font(font.Name, (float)(font.Size * .9), font.Style);
oldFont.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这完美无瑕.
我唯一要改变的是将FindBestFitFont()调用移动到OnResize()事件,这样我每次绘制一个字母时都不会调用它.只需在控件大小更改时调用它.我只是把它包含在功能中以保持完整性.
dbk*_*bkk 10
要绘制居中文本:
TextRenderer.DrawText(g, "my text", Font, Bounds, ForeColor, BackColor,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.GlyphOverhangPadding);
Run Code Online (Sandbox Code Playgroud)
确定填充区域的最佳字体大小有点困难.我找到的一个工作灵魂是反复试验:从一个大字体开始,然后重复测量字符串并收缩字体直到它适合.
Font FindBestFitFont(Graphics g, String text, Font font,
Size proposedSize, TextFormatFlags flags)
{
// Compute actual size, shrink if needed
while (true)
{
Size size = TextRenderer.MeasureText(g, text, font, proposedSize, flags);
// It fits, back out
if ( size.Height <= proposedSize.Height &&
size.Width <= proposedSize.Width) { return font; }
// Try a smaller font (90% of old size)
Font oldFont = font;
font = new Font(font.FontFamily, (float)(font.Size * .9));
oldFont.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
你用它作为:
Font bestFitFont = FindBestFitFont(g, text, someBigFont, sizeToFitIn, flags);
// Then do your drawing using the bestFitFont
// Don't forget to dispose the font (if/when needed)
Run Code Online (Sandbox Code Playgroud)
这是一些代码。这假设您在表单或用户控件上执行此操作。
Graphics g = this.CreateGraphics();
SizeF size = g.MeasureString("string to measure");
int nLeft = Convert.ToInt32((this.ClientRectangle.Width / 2) - (size.Width / 2));
int nTop = Convert.ToInt32((this.ClientRectangle.Height / 2) - (size.Height / 2));
Run Code Online (Sandbox Code Playgroud)
从您的帖子来看,听起来 ClientRectangle 部分(例如,您没有使用它)给您带来了困难。
归档时间: |
|
查看次数: |
106052 次 |
最近记录: |