Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / WPF

CodeBox

Rate me:
Please Sign up or sign in to vote.
4.91/5 (34 votes)
10 Mar 2009CPOL7 min read 99K   2.3K   95  
A fast WPF textbox control with support for text coloring, highlighting, underlines, and strikethroughs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows;
namespace CodeBoxControl
{
  public  class StackedRectangleGeometryHelper
    {
      private Geometry mOrginalGeometry;
      public StackedRectangleGeometryHelper(Geometry geom)
      {
          mOrginalGeometry = geom;
      }

       
      /// <summary>
      /// Creates List of geometries for underline
      /// </summary>
      /// <returns>List of Geometries</returns>
      public List<Geometry> BottomEdgeRectangleGeometries()
      {
          List<Geometry> geoms = new List<Geometry>();
          PathGeometry pg = (PathGeometry)mOrginalGeometry;
          foreach (PathFigure fg in pg.Figures)
          {
              PolyLineSegment pls = (PolyLineSegment)fg.Segments[0]; 
              PointCollectionHelper pch = new PointCollectionHelper(pls.Points, fg.StartPoint);
              List<double> distinctY = pch.DistinctY;
              for (int i = 0; i < distinctY.Count - 1; i++)
              {
                  double bottom = distinctY[i + 1] - 3;
                  double top = bottom + 2;
                  // ordered values of X that are present for both Y values
                  List<double> rlMatches = pch.XAtY(distinctY[i], distinctY[i + 1]); 
                  double left = rlMatches[0];
                  double right = rlMatches[rlMatches.Count - 1];

                  PathGeometry rpg = CreateGeometry(top, bottom, left, right);
                  geoms.Add(rpg);
              }
          }
          return geoms;
      }

      /// <summary>
      /// Creates List of geometries for Strikethru
      /// </summary>
      /// <returns>List of Geometries</returns>
      public List<Geometry> CenterLineRectangleGeometries()
      {
          List<Geometry> geoms = new List<Geometry>();
          PathGeometry pg = (PathGeometry)mOrginalGeometry;

          foreach (PathFigure fg in pg.Figures)
          {
              PolyLineSegment pls = (PolyLineSegment)fg.Segments[0];
              PointCollectionHelper pch = new PointCollectionHelper(pls.Points, fg.StartPoint);
              List<double> distinctY = pch.DistinctY;
              for (int i = 0; i < distinctY.Count - 1; i++)
              {
                  double top = (distinctY[i] + distinctY[i + 1])/ 2 + 1;
                  double bottom = (distinctY[i] + distinctY[i + 1]) / 2 - 1;
                  // ordered values of X that are present for both Y values
                  List<double> rlMatches = pch.XAtY(distinctY[i], distinctY[i + 1]);
                  double left = rlMatches[0];
                  double right = rlMatches[rlMatches.Count - 1];

                  PathGeometry rpg = CreateGeometry(top, bottom, left, right);
                  geoms.Add(rpg);
              }
          }
          return geoms;
      }

      private static PathGeometry CreateGeometry(double top, double bottom, double left, double right)
      {
          PolyLineSegment plr = new PolyLineSegment();

          plr.Points.Add(new Point(right, top));
          plr.Points.Add(new Point(right, bottom));
          plr.Points.Add(new Point(left, bottom));
          PathFigure rpf = new PathFigure();
          rpf.StartPoint = new Point(left, top);
          rpf.Segments.Add(plr);
          rpf.IsClosed = true;
          PathGeometry rpg = new PathGeometry();
          rpg.Figures.Add(rpf);
          return rpg;
      }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions