bec*_*lmw 12 c# system.drawing
在使用Graphics.DrawString时,是否可以以某种方式控制字母间距?我找不到允许我这样做的DrawString或Font的任何重载.
g.DrawString("MyString",
new Font("Courier", 44, GraphicsUnit.Pixel),
Brushes.Black,
new PointF(262, 638));
Run Code Online (Sandbox Code Playgroud)
字母间距我指的是字母之间的距离.如果我添加了足够的空间,那么间距MyString可能看起来很像.
Han*_*ant 14
开箱即用不支持.你要么必须单独绘制每个字母(很难做到正确),要么自己在字符串中插入空格.您可以使用Graphics.ScaleTransform()来拉伸字母,但这看起来很难看.
或者,您可以使用GDI API函数SetTextCharacterExtra(HDC hdc, int nCharExtra)
(MSDN文档):
[DllImport("gdi32.dll", CharSet=CharSet.Auto)]
public static extern int SetTextCharacterExtra(
IntPtr hdc, // DC handle
int nCharExtra // extra-space value
);
public void Draw(Graphics g)
{
IntPtr hdc = g.GetHdc();
SetTextCharacterExtra(hdc, 24); //set spacing between characters
g.ReleaseHdc(hdc);
e.Graphics.DrawString("str",this.Font,Brushes.Black,0,0);
}
Run Code Online (Sandbox Code Playgroud)