//判断是否是中文
        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;
        }

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

修改FastColoredTextBox控件完成选择的更多相关文章

  1. Dbgrid控件多重选择的怪问题。BookMarkList,BookMark,使用书签,用的ADOQuery控件。100分送上,急阿!!!请高手帮忙!

    Dbgrid控件多重选择的怪问题.BookMarkList,BookMark,使用书签,用的ADOQuery控件.100分送上,急阿!!!请高手帮忙! ( 积分: 100 )<br />p ...

  2. QTableView表格控件区域选择-自绘选择区域

    目录 一.开心一刻 二.概述 三.效果展示 四.实现思路 1.绘制区域 2.绘制边框 3.绘制 五.相关文章 原文链接:QTableView表格控件区域选择-自绘选择区域 一.开心一刻 陪完客户回到家 ...

  3. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  4. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  5. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. C# WPF 使用委托修改UI控件

    近段时间在自学WPF,是一个完全不懂WPF的菜鸟,对于在线程中修改UI控件使用委托做一个记录,给自已以后查询也给需要的参考: 界面只放一个RichTextBox,在窗体启动时开起两个线程,调用两个函数 ...

  7. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...

  8. android中动态修改ImageView控件的宽高度

    本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...

  9. 修改static控件背景颜色和文字颜色

    当 static 控件或具有 ES_READONLY 风格的 edit 控件被绘制时,会向父窗口发送 WM_CTLCOLORSTATIC 消息.如果我们在窗口过程中处理该消息,就必须返回一个画刷句柄, ...

随机推荐

  1. C# 4.0中dynamic的作用

    internal sealed class Coffee { public string GetName() { return "You selected Maxwell coffee.&q ...

  2. hadoop运维经验

    0.优化:http://dongxicheng.org/mapreduce/hadoop-optimization-0/ http://dongxicheng.org/mapreduce/hadoop ...

  3. Oracle、Microsoft SQL Server、Mysql

    数据库对比.----1.Oracle:最贵,功能最多,安装最不方便,Oracle环境里的其他相关组件最多,支持平台数量一般,使用中等方便,开发中等方便,运维中等方便,不开源,速度最慢,最安全.---- ...

  4. jQuery 效果 —— 滑动

    jQuery 效果 -- 滑动 1.向下滑动元素 (1)使用slideDown()方法可以用于向下滑动元素 $("button").click(function(){ $(&quo ...

  5. jquery.validate使用 - 2

    jQuery.validate.js API说明 参考http://ideabean.javaeye.comPlugin methods Name Type validate( options ) R ...

  6. 2. Swift元组|可选值|断言

    1. 元组英文名字 Tuple,将多个数据类型(任意类型)组合成一个数据,与c语言的中的机构体有几分相似,功能也是非常强大的,尤其是在定义请求参数,状态之类的地方经常用到. let http404Er ...

  7. Linux sticky bit 目录权限 rwt权限

    [linux权限分为 user group others三组] 今天看到有个目录的权限是rwxrwxrwt 很惊讶这个t是什么,怎么不是x或者-呢?搜了下发现: 这个t代表是所谓的sticky bit ...

  8. 《BI那点儿事》Cube的存储

    关系 OLAP (ROLAP)ROLAP的基本数据和聚合数据均存放在关系数据库中:ROLAP 存储模式使得分区的聚合存储在关系数据库的表(在分区数据源中指定)中.但是,可为分区数据使用 ROLAP 存 ...

  9. TCP/IP协议学习(四) 协议概述

    生活中有舒适区,借口成为懒惰的护身符,学习也有舒适区,逃避便是阻止进步的最大障碍. 经过半年多嵌入式方面的工作和学习,我提高了很多,但同时我也对自己所面临的问题逐渐清晰: 1. 偏于实践,理论基础不牢 ...

  10. How to remove k__BackingField from Json data

    当用DataContractJsonSerializer类序列化对象为Json时,发现序列化后的json对象,每个属性都带有k__BackingField后缀,像这样: 解决办法:为要被序列化的类打上 ...