Tag: 压缩框架

Graphics.DrawString()的中心文本输出

我正在使用.NETCF(Windows Mobile) Graphics类和DrawString()方法来呈现单个字符的屏幕。 问题是,我似乎无法正确地居中。 无论我为string渲染的位置的Y坐标设置什么,它总是低于此值,文本大小越大,Y偏移量越大。 例如,在文本大小为12时,偏移大约是4,但是在32处偏移大约是10。 我希望angular色垂直占据大部分正在绘制的矩形,并水平居中。 这是我的基本代码。 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); 是的,我知道有我可以使用的标签控件,并将其设置为居中,但实际上我需要使用Graphics类手动执行此操作。