FontMenuForm 基类见:http://blog.youkuaiyun.com/u013384702/article/details/17884617
字体裁剪的应用
Code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CsStudy
{
class ClipText : FontMenuForm
{
public new static void Main()
{
Application.Run(new ClipText());
}
public ClipText()
{
Text = "Clip Text";
Width *= 2;
strText = "猪爷爷";
font = new Font("Times New Roman", 108, FontStyle.Bold);//加粗
}
protected override void DoPage(Graphics grph, Color clr, int cx, int cy)
{
GraphicsPath path = new GraphicsPath();
float fFontSize = PointToPageUnits(grph, font);
//添加文本到图形路径
path.AddString(strText, font.FontFamily, (int)font.Style, fFontSize, new Point(0, 0), new StringFormat());
//设置裁剪区域
grph.SetClip(path);
RectangleF rectfBounds = path.GetBounds();
grph.TranslateClip((cx - rectfBounds.Width) / 2 - rectfBounds.Left,
(cy - rectfBounds.Height) / 2 - rectfBounds.Top);
//绘制裁剪线条
Random rand = new Random();
for (int i = 0; i < cy; i++)
{
Pen pen = new Pen(Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255)));
grph.DrawBezier(pen, new Point(0, i), new Point(cx / 3, i + cy / 3),
new Point(2 * cx / 3, i - cy / 3), new Point(cx, i));
}
}
}
}
效果图: