修改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;
}
http://files.cnblogs.com/MyNameIsMT/FastColoredTextBox20140116.zip
修改FastColoredTextBox控件完成选择的更多相关文章
- Dbgrid控件多重选择的怪问题。BookMarkList,BookMark,使用书签,用的ADOQuery控件。100分送上,急阿!!!请高手帮忙!
Dbgrid控件多重选择的怪问题.BookMarkList,BookMark,使用书签,用的ADOQuery控件.100分送上,急阿!!!请高手帮忙! ( 积分: 100 )<br />p ...
- QTableView表格控件区域选择-自绘选择区域
目录 一.开心一刻 二.概述 三.效果展示 四.实现思路 1.绘制区域 2.绘制边框 3.绘制 五.相关文章 原文链接:QTableView表格控件区域选择-自绘选择区域 一.开心一刻 陪完客户回到家 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- C# WPF 使用委托修改UI控件
近段时间在自学WPF,是一个完全不懂WPF的菜鸟,对于在线程中修改UI控件使用委托做一个记录,给自已以后查询也给需要的参考: 界面只放一个RichTextBox,在窗体启动时开起两个线程,调用两个函数 ...
- 在.net中修改Webbrowser控件的IE版本
根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...
- android中动态修改ImageView控件的宽高度
本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...
- 修改static控件背景颜色和文字颜色
当 static 控件或具有 ES_READONLY 风格的 edit 控件被绘制时,会向父窗口发送 WM_CTLCOLORSTATIC 消息.如果我们在窗口过程中处理该消息,就必须返回一个画刷句柄, ...
随机推荐
- 通过配置的方式Autofac 《第三篇》
一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...
- [Mysql] 一些记录
1> 修改表的字段 alter table trade_market change reqype reqtype int(10) unsigned not null;alter table tr ...
- 从webRoot中下载Excel
@RequestMapping("downLoad") public void downLoad(Offsupervise off1,HttpServletRequest requ ...
- Beginning Windows Azure Development Guide
目 录 一 初始化Windows Azure 二 云应用程序的编写. 2.1云应用程序的创建. 2.2一个简单的云应用程序. 2.3 托管云程序. 三 云程序的数据库操作. 3.1通过Cloud平台 ...
- sqlalchemy 优化count()……
一.sqlalchemy 中的count() count()统计数据特别慢: session.query(cls).count() 8W 数据花费了近50s 但是在数据库中直接查询: select ...
- 用Redis Desktop Manager连接Redis
Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...
- POSTGRESQL 数据库导入导出
导入整个数据库 psql -U postgres(用户名) 数据库名(缺省时同用户名) < /data/dum.sql 导出整个数据库 pg_dump -h localhost -U po ...
- Design pattern---观察者模式
观察者模式:发布/订阅模式,当某对象(被观察者)状态发生改变时所有订阅该对象的观察者对象(观察者)都将更新自己 成员(4种): 1.抽象被观察者:将所有的观察者对象的引用存入一个集合,并且定义了添加 ...
- ModalPopup
原文地址:http://ajax.asp.net/ajaxtoolkit/ModalPopup/ModalPopup.aspx ModalPopup 描述 ModalPopup 能够使页面以设计对话框 ...
- 禁止Android 横屏竖屏切换
在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就可以了. 在AndroidManifest.xml的ac ...