我准备先用一种字体写出一段字符串(例如大小为60的宋体),然后紧接着在同一行换用另一种字体继续写(例如大小为15的宋体)。我用MeasureCharacterRanges测量前一种字符串的长度,结果发现测得并不准。出现了严重的重叠问题。
代码如下:
string TestStr ="TestString";
StringFormat fmt  = new StringFormat();
fmt.SetMeasurableCharacterRanges(new  CharacterRange[]{new CharacterRange(0,TestStr.Length)});
Region[] regions=e.Graphics.MeasureCharacterRanges("TestString",
new Font("宋体",60),
new RectangleF(0,0,400,400),
fmt);
float Next=regions[0].GetBounds(e.Graphics).Width;
//画个框,看他测得到底准不准
e.Graphics.DrawRectangle(Pens.Blue,
Rectangle.Round(regions[0].GetBounds(e.Graphics)));
//第一段字
e.Graphics.DrawString(TestStr,
new Font("宋体",60),
Brushes.Black,0,0);
//第二段字
e.Graphics.DrawString(TestStr,
new Font("宋体",15),
Brushes.Black,
Next,
20);
恳请各位帮忙!

解决方案 »

  1.   

    可以设置字间距,我原来在CSDN回复过设置字间距问题至于获取字间距可能通过传进来的Font获取他的大小.
      

  2.   

    多谢lidong6(立冬),但我不明白,我该怎么做呢?查了半天字间距没找到,你指得是封装GDI的DrawString方法,然后使用GDI绘制有间距的字符吗?
      

  3.   

    设置字间距的办法.
    SetTextCharacterExtra()
    示例:
    [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
    public static extern int SetTextCharacterExtra(
    IntPtr hdc,         // handle to DC
    int nCharExtra   // extra-space value
    );
    Font f= new Font("宋体",20);
    Brush b= new SolidBrush(Color.Green);
    String s="11111";IntPtr hwnd = new IntPtr();
    hwnd = this.Handle;
    Graphics g = Graphics.FromHwnd(hwnd);IntPtr hdc = g.GetHdc();
    SetTextCharacterExtra(hdc,40);using(Graphics gg = Graphics.FromHdc(hdc))
    {
    gg.DrawString(s,f,b,0,0);
    }g.ReleaseHdc(hdc);g.Dispose();