Csharp/C#教程:C#抽绳字母间距分享


C#抽绳字母间距

在使用Graphics.DrawString时,是否可以以某种方式控制字母间距? 我找不到允许我这样做的DrawString或Font的任何重载。

g.DrawString("MyString", new Font("Courier", 44, GraphicsUnit.Pixel), Brushes.Black, new PointF(262, 638)); 

字母间距我指的是字母之间的距离。 如果我添加了足够的空间,间距MyString可能看起来像是什么。

开箱即用不支持。 你要么必须单独绘制每个字母(很难做到正确),要么自己在字符串中插入空格。 您可以使用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); } 

它不受支持,但作为一个黑客,你可以循环遍历字符串中的所有字母,并在每个字母之间插入一个空格字符。 您可以为它创建一个简单的函数:

编辑 – 我在Visual Studio中重新执行此操作并进行测试 – 现在删除了错误。

 private string SpacedString(string myOldString) { System.Text.StringBuilder newStringBuilder = new System.Text.StringBuilder(""); foreach (char c in myOldString.ToCharArray()) { newStringBuilder.Append(c.ToString() + ' '); } string MyNewString = ""; if (newStringBuilder.Length > 0) { // remember to trim off the last inserted space MyNewString = newStringBuilder.ToString().Substring(0, newStringBuilder.Length - 1); } // no else needed if the StringBuilder's length is <= 0... The resultant string would just be "", which is what it was intitialized to when declared. return MyNewString; } 

然后您的上述代码行将被修改为:

  g.DrawString(SpacedString("MyString"), new Font("Courier", 44, GraphicsUnit.Pixel), Brushes.Black, new PointF(262, 638)); 

我确实相信ExtTextOut会解决你的问题。 您可以使用lpDx参数添加字符间距离数组。 以下是相关的MSN文档:

https://msdn.microsoft.com/en-us/library/dd162713%28v=vs.85%29.aspx

上述就是C#学习教程:C#抽绳字母间距分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1011849.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐