求html文本编辑控件
不需要很复杂,只要关键字着色就好,速度要快,我自己写了一个速度很慢,由于使用的是richtextbox,用这个控件的selectioncolor属性改变关键字的颜色,其他都很快,只有执行richtextbox的select()方法和使用selectioncolor属性着色时候非常非常的慢,能解决这个问题也可以,^_^
问题点数:100、回复次数:13Top
1 楼xiandaliu(仙达流,菜鸟先飞)回复于 2005-01-10 09:42:31 得分 0
附代码如下:
类:拆分字符串
public class Html
{
public struct ColorText
{
public Color TextColor;
public int Index;
public int Length;
public ColorText( Color textcolor,int index,int length )
{
TextColor = Color.Black; Index = -1; Length = -1;
TextColor = textcolor;
Index = index;
Length = length;
}
}
public static ArrayList Tag()
{
ArrayList arr = new ArrayList();
System.IO.FileStream fs = new System.IO.FileStream("d:\\WORDFILE.TXT", System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.StreamReader srm = new System.IO.StreamReader(fs);
string TheFile = srm.ReadToEnd();
srm.Close();
fs.Close();
System.IO.StringReader sr = new System.IO.StringReader(TheFile);
string nextLine;
nextLine = sr.ReadLine();
nextLine = nextLine.Trim().ToLower();
while (nextLine != null)
{
nextLine = nextLine.Trim().ToLower();
arr.AddRange(nextLine.Split(' '));
nextLine = sr.ReadLine();
}
System.IO.StreamWriter sw = new System.IO.StreamWriter("d:\\test.txt");
foreach (string st in arr)
sw.WriteLine(st);
sw.Flush();
sw.Close();
return arr;
}
static ArrayList tag;
public static ArrayList DrawText( string s )
{
ArrayList wr = new ArrayList();
System.Text.RegularExpressions.Regex r;
System.Text.RegularExpressions.Match m;
r = new System.Text.RegularExpressions.Regex(@"<\w+>|<\w+|"+"\""+"\\w+"+"\""+@"|</\w+>|[^A-Za-z0-9_<> \f\t\v]|\w+", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Compiled);
if (tag == null)
{
tag = Tag();
tag.Sort();
}
int count = 0;
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
if (tag.BinarySearch(m.Value.ToLower()) >= 0)
wr.Add(new ColorText(Color.DarkRed, m.Index, m.Length));
else
wr.Add(new ColorText(Color.Black, m.Index, m.Length));
count++;
}
return wr;
}
}
RichTextBox CustomControl
public class RichTextBox : System.Windows.Forms.RichTextBox
{
public RichTextBox()
{
}
bool _Paint = true;
string _texttype = "html";
public string TextType
{
get { return _texttype; }
set { _texttype = value; }
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x00f)
{
if (_Paint)
base.WndProc(ref m);
else
m.Result = IntPtr.Zero;
}
else
base.WndProc(ref m);
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
}
protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{
}
public void ChangeColor_Line()
{
_Paint = false;
int CurrentSelectionStart = SelectionStart;
int CurrentSelectionLength = SelectionLength;
int pos = CurrentSelectionStart;
while ((pos > 0) && (Text[pos - 1] != '\n'))
pos--;
int pos2 = CurrentSelectionStart;
while ((pos2 < Text.Length) &&
(Text[pos2] != '\n'))
pos2++;
string s = Text.Substring(pos, pos2 - pos);
Select(pos, pos2);
SelectionColor = System.Drawing.Color.Black;
Select(pos, 0);
System.Collections.ArrayList arr = Html.DrawText(s);
if (arr != null)
for (int i = 0; i < arr.Count; i++)
{
Select(((Html.ColorText)arr[i]).Index + pos, ((Html.ColorText)arr[i]).Length);
SelectionColor = ((Html.ColorText)arr[i]).TextColor;
}
if (CurrentSelectionStart >= 0)
Select(CurrentSelectionStart, CurrentSelectionLength);
_Paint = true;
}
public void ChangeColor_All()
{
_Paint = false;
int CurrentSelectionStart = SelectionStart;
int CurrentSelectionLength = SelectionLength;
SelectAll();
SelectionColor = System.Drawing.Color.Black;
Select(0, 0);
System.Collections.ArrayList arr = Html.DrawText(Text);
if (arr != null)
{
Html.ColorText[] c = new Html.ColorText[arr.Count];
arr.CopyTo(c);
for (int i = 0; i < c.Length; i++)
{
Select(c[i].Index, c[i].Length);
SelectionColor = c[i].TextColor;
}
}
if (CurrentSelectionStart >= 0)
Select(CurrentSelectionStart, CurrentSelectionLength);
_Paint = true;
}
protected override void OnTextChanged(EventArgs e)
{
ChangeColor_Line();
base.OnTextChanged(e);
}
}
因为是使用的2005写的,不知道有没有什么关系,应该是没有,^_^Top
2 楼xiandaliu(仙达流,菜鸟先飞)回复于 2005-01-10 09:44:59 得分 0
打开文件使用change_all方法非常的慢Top
3 楼xiandaliu(仙达流,菜鸟先飞)回复于 2005-01-11 09:04:49 得分 0
顶一个Top
4 楼liulxmooo(娃娃)回复于 2005-01-11 09:10:21 得分 0
upTop
5 楼terryshi(terryshi)回复于 2005-01-11 09:26:57 得分 0
freetextbox~~免费Top
6 楼RockyZhang(Rocky)回复于 2005-01-11 09:28:31 得分 45
微软提供的HTML编辑控件.试试这个,还不错.
http://windowsforms.net/articles/htmleditor.aspxTop
7 楼owg(OMG)回复于 2005-01-11 09:55:33 得分 10
http://www.evget.com/view/viewCategoryProduct.asp?language=&mode=&productTypeId=&categoryId=9Top
8 楼mooddecode1980(心情解码)回复于 2005-01-11 09:59:02 得分 0
up
:)Top
9 楼joelbh(ILoveYou)回复于 2005-01-11 10:47:00 得分 0
freetextbox不错啊Top
10 楼LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^)回复于 2005-01-11 10:51:09 得分 0
upTop
11 楼zhzuo(秋枫)回复于 2005-01-11 12:40:53 得分 45
http://www.windowsforms.net/ControlGallery/default.aspx?Category=13&tabindex=10Top
12 楼3gold(新丁)回复于 2005-01-11 13:03:57 得分 0
FCKedit推荐Top
13 楼xiandaliu(仙达流,菜鸟先飞)回复于 2005-01-11 18:47:49 得分 0
对不起,我没说清楚,是windows的控件Top




