//判断是否是中文
        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. javascript 面向对象(转)

    1.使用[]调用对象的属性和方法 function User() { this.age = 21; this.sex = "男?"; } var user = new User() ...

  2. oracle分析函数

    在工作中使用到的分析函数主要有两种,一个是sum () over (partition by ……order by ……)另外一个就是 lead(lag)over (|partition by|ord ...

  3. MyBatis简介

  4. storm-kafka组件中KafkaOffsetMetric相关统计指标说明

    storm-kafka组件中KafkaOffsetMetric相关统计指标说明 storm-kafka是storm提供的一个读取kakfa的组件,用于从kafka队列中消费数据.KafkaOffset ...

  5. jQuery核心之那些有效的方法

    jQuery提供了一些很有效的方法,这些方法都在$命名空间之下,对常规的编码很有帮助,完整的api详见:utilities documentation on api.jquery.com $.trim ...

  6. java练习题:输出100以内与7有关的数、百马百担、打分(去掉最高、最低分)、二分法查找数据

    1.输出100以内与7有关的数 注: 这些数分为三类:(1)7的倍数,(2)个位数字是7的数,(3)十位数字是7的数 int i=1; System.out.println("输出100以内 ...

  7. Java Performance - 如何调查解决 CPU 问题

    随着硬件的发展,往往服务器会配置足够的 CPUs, Java Server/服务器不太有 CPU 问题:但是偶尔因为 代码海量循环 或者 线程安全性(thread safe), 还是会带来 CPU 问 ...

  8. Xcode7 创建HTTP请求报错

    最近在Xcode 7中向服务器发送请求访问JSON数据时, 控制台打印了以下错误信息: Application Transport Security has blocked a cleartext H ...

  9. 使用spawn-fcgi管理php-cgi

    spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f php-cgi

  10. Laravel 流程分析——整体概论

    从整体上来看(不考虑细节),Laravel流程相当简单,我们分析一下index.php文件(下面的第几行为实际代码,不是指文件的行) 第一行定义自动加载 require __DIR__.'/../bo ...