• 修改FastColoredTextBox控件完成选择


     //判断是否是中文
            public bool IsChina(char c)
            {

                bool BoolValue = false;
                if (Convert.ToInt32(c) < Convert.ToInt32(Convert.ToChar(128)))
                {
                    BoolValue = false;
                }
                else
                {
                    return BoolValue = true;
                }
                return BoolValue;
            }
            //获取字符串中文数量
            public int GetLineIsChinese(Line list)
            {
                int isChinese = 0;
                for (int num = 0; num < list.Count;num++ )
                {
                    if (IsChina(list[num].c))
                    {
                        isChinese++;
                    }
                }
                return isChinese;
            }
            //获取中文和英文差异宽度
            public int GetCharDiffWidth()
            {
                SizeF sizef = GetCharSize(this.Font, '中');//new Font("宋体", 10)
                int len = (int)sizef.Width - CharWidth;
                return len;
            }

     

     

       //draw selection
                if (!Selection.IsEmpty && lastChar >= firstChar)
                {
                    e.Graphics.SmoothingMode = SmoothingMode.None;
                    var textRange = new Range(this, from + firstChar, iLine, from + lastChar + 1, iLine);
                    textRange = Selection.GetIntersectionWith(textRange);
                    if (textRange != null && SelectionStyle != null)
                    {
                        int chineseCount= GetLineIsChinese(line);
                        int diffWidths = GetCharDiffWidth()*chineseCount;
                        diffWidth = diffWidths;
                        SelectionStyle.Draw(e.Graphics, new Point(startX + (textRange.Start.iChar - from) * CharWidth, y),
                                            textRange);
                        diffWidth = 0;
                    }
                }

     

    public override void Draw(Graphics gr, Point position, Range range)
            {
                //draw background
                if (BackgroundBrush != null)
                {
                 
                    Rectangle rect = new Rectangle(position.X, position.Y, (range.End.iChar - range.Start.iChar) * range.tb.CharWidth+FastColoredTextBox.diffWidth, range.tb.CharHeight);
                    if (rect.Width == 0)
                        return;
                    gr.FillRectangle(BackgroundBrush, rect);
                }
            }

    diffWidth  这是公共变量,放在FastColoredTextBox类的公共变量声明里面

    public static int diffWidth = 0;

    /*****************************/

    /*  该代码用于准确定位字,主要是因为中文不能准确定位  */

    /*****************************/

     /// <summary>
            /// Gets nearest line and char position from coordinates
            /// </summary>
            /// <param name="point">Point</param>
            /// <returns>Line and char position</returns>
            public Place PointToPlace(Point point)
            {
    #if debug
                var sw = Stopwatch.StartNew();
    #endif
                point.Offset(HorizontalScroll.Value, VerticalScroll.Value);
                point.Offset(-LeftIndent - Paddings.Left, 0);
                int iLine = YtoLineIndex(point.Y);
                int y = 0;

                for (; iLine < lines.Count; iLine++)
                {
                    y = LineInfos[iLine].startY + LineInfos[iLine].WordWrapStringsCount * CharHeight;
                    if (y > point.Y && LineInfos[iLine].VisibleState == VisibleState.Visible)
                        break;
                }
                if (iLine >= lines.Count)
                    iLine = lines.Count - 1;
                if (LineInfos[iLine].VisibleState != VisibleState.Visible)
                    iLine = FindPrevVisibleLine(iLine);
                //
                int iWordWrapLine = LineInfos[iLine].WordWrapStringsCount;
                do
                {
                    iWordWrapLine--;
                    y -= CharHeight;
                } while (y > point.Y);
                if (iWordWrapLine < 0) iWordWrapLine = 0;
                //
                int start = LineInfos[iLine].GetWordWrapStringStartPosition(iWordWrapLine);
                int finish = LineInfos[iLine].GetWordWrapStringFinishPosition(iWordWrapLine, lines[iLine]);
                var x = (int)Math.Round((float)point.X / CharWidth);
                x = x < 0 ? start : start + x;
                if (x > finish)
                    x = finish + 1;
                if (x > lines[iLine].Count)
                    x = lines[iLine].Count;

                if (lines[iLine].Count > 0)
                {
                    x = GetXWidth(lines[iLine], point.X);
                }

    #if debug
                Console.WriteLine("PointToPlace: " + sw.ElapsedMilliseconds);
    #endif

                return new Place(x, iLine);
            }

            private Place PointToPlaceSimple(Point point)
            {
                point.Offset(HorizontalScroll.Value, VerticalScroll.Value);
                point.Offset(-LeftIndent - Paddings.Left, 0);
                int iLine = YtoLineIndex(point.Y);
                var x = (int)Math.Round((float)point.X / CharWidth);
                Line line = lines[iLine];
                if (x < 0) x = 0;

                if (line.Count > 0)
                {
                    x = GetXWidth(lines[iLine], point.X);
                }
                return new Place(x, iLine);
            }
            //计算x轴应该显示第几个字的宽度
            public int GetXWidth(Line line,int x)
            {
                int iChar = 0;
                int len = 0;
                for (int num = 0; num < line.Count; num++)
                {
                    if (len >= x)
                    {
                        break;
                    }
                    if (IsChina(line[num].c))
                    {
                        iChar += 2;
                        len += (GetCharDiffWidth() + CharWidth);
                    }
                    else
                    {
                        iChar++;
                        len += CharWidth;
                    }
                }
                return iChar;
            }

    https://files.cnblogs.com/MyNameIsMT/FastColoredTextBox20140116.zip

  • 相关阅读:
    BZOJ_3171_[Tjoi2013]循环格_最小费用最大流
    BZOJ_1150_[CTSC2007]数据备份Backup_堆+贪心
    BZOJ_1576_[Usaco2009 Jan]安全路经Travel&&BZOJ_3694_最短路_树链剖分+线段树
    BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数
    BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho
    BZOJ_1584_[Usaco2009 Mar]Cleaning Up 打扫卫生_DP
    BZOJ_1229_[USACO2008 Nov]toy 玩具_三分+贪心
    BZOJ_2160_拉拉队排练_manacher
    [转载]Linux软件包及dpkgapt等方法
    服务器基础认知杂谈及一小点运维相关
  • 原文地址:https://www.cnblogs.com/MyNameIsMT/p/3521877.html
Copyright © 2020-2023  润新知