日期:2014-05-18  浏览次数:21156 次

在C#(.net)中实现文字按指定的路径排列(沿线排版)

先看一下效果图:

沿线排版

这个的难点在于排版的算法上,其他的就是对GDI+中GraphicsPath的应用。以下为核心部分的源代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Lgms.net.Drawing
{
    public enum PathAlign //文字在路径方向的对齐方向
    {
        Left = 0,  //从第一点开始排列
        Center = 1,  //居中排列
        Right = 2 //居右排列
    }
    public enum TextPosition  //文字排列在路径上的位置
    {
        OverPath = 0,  //路径之上
        CenterPath = 1,  //路径中间,文字中心即路径经过区域
        UnderPath = 2  //路径下方
    }

    //基础类
    public class TextOnPath
    {
        private PathData _pathdataTOP;
        private string _textTOP;
        private Font _fontTOP;
        private Color _pathcolorTOP = Color.Red;
        private Color _colorTOP = Color.Black;
        private Color _fillcolorTOP = Color.Black;
        private PathAlign _pathalignTOP = PathAlign.Center;
        private int _letterspacepercentageTOP = 100;
        private bool _addsvg = false;
        private System.Text.StringBuilder _SVG;
        private int _currentPathID;
        private TextPosition _textPathPosition = TextPosition.CenterPath;
        public Exception LastError = null;
        public bool ShowPath = true;
        public TextPosition TextPathPosition
        {
            get { return _textPathPosition; }
            set { _textPathPosition = value; }
        }
        public PathData PathDataTOP
        {
            get { return _pathdataTOP; }
            set { _pathdataTOP = value; }
        }

        public string TextTOP
        {
            get { return _textTOP; }
            set { _textTOP = value; }
        }


        public Font FontTOP
        {
            get { return _fontTOP; }
            set { _fontTOP = value; }
        }


        public Color PathColorTOP
        {
            get { return _pathcolorTOP; }
            set { _pathcolorTOP = value; }
        }
        public Color ColorTOP
        {
            get { return _colorTOP; }
            set { _colorTOP = value; }
        }


        public Color FillColorTOP
        {
            get { return _fillcolorTOP; }
      &nb