Robintje1
January 5th, 2010, 01:37 PM
Hiiii.
I have a problem with Syntax Highlighting in a RichTextBox. I'm using the following code, found in a thread here.
[DllImport("user32.dll")]
extern static int SendMessage(int hwnd, int message, int wparam, int lparam);
[DllImport("user32.dll")]
extern static int LockWindowUpdate(int hwnd);
int[] strComm = null;
KeyWords[] Words = new KeyWords[14];
public enum EditMessages
{
LineIndex = 187,
LineFromChar = 201,
GetFirstVisibleLine = 206,
CharFromPos = 215,
PosFromChar = 1062
}
public struct KeyWords
{
public string Word;
public Color Color;
}
public Form1()
{
InitializeComponent();
}
private void rtfText_TextChanged(object sender, EventArgs e)
{
//Handles rtfText.KeyPress
ColorVisibleLines(rtfText);
}
public void ColorVisibleLines(RichTextBox rtfText)
{
int FirstLine = FirstVisibleLine();
int LastLine = LastVisibleLine();
int FirstVisibleChar;
int i = FirstLine;
if ((FirstLine == 0) & (LastLine == 0))
{
// If there is no text in the control, it will run an error
// So, if there isn't any text, just exit
return; // TODO: might not be correct. Was : Exit Sub
}
else
{
while (i < LastLine)
{
FirstVisibleChar = GetCharFromLineIndex(FirstLine);
ColorLineNumber(rtfText, FirstLine, FirstVisibleChar);
FirstLine += 1;
i += 1;
}
}
}
public int FirstVisibleLine()
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.GetFirstVisibleLine, 0, 0);
}
public int GetCharFromLineIndex(int LineIndex)
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.LineIndex, LineIndex, 0);
}
public int LastVisibleLine()
{
int LastLine = FirstVisibleLine() + (rtfText.Height / rtfText.Font.Height);
if (LastLine > rtfText.Lines.Length | LastLine == 0)
{
LastLine = rtfText.Lines.Length;
}
return LastLine;
}
public void ColorRtb(RichTextBox rtftext)
{
int FirstVisibleChar;
int i = 0;
while (i < rtftext.Lines.Length)
{
FirstVisibleChar = GetCharFromLineIndex(i);
ColorLineNumber(rtftext, i, FirstVisibleChar);
i += 1;
}
}
public void ColorLineNumber(RichTextBox rtfText, int LineIndex, int lStart)
{
string Line = rtfText.Lines[LineIndex].ToLower();
int i = 0;
int Instance;
int SelectionAt = rtfText.SelectionStart;
// Lock the update
LockWindowUpdate(rtfText.Handle.ToInt32());
// Color the line black to remove any previous coloring
rtfText.SelectionStart = lStart;
rtfText.SelectionLength = Line.Length;
rtfText.SelectionColor = Color.Black;
// Loop through all the Keywords
while (i < Words.Length)
{
// See if the word is in the Line
//Instance = string.Compare(Line, Words[i].Word); // InStr(Line, Words[i].Word);
Instance = Line.IndexOf(Words[i].Word) + 1;
// If the lines contains the word, color it
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = Words[i].Word.Length;
rtfText.SelectionColor = Words[i].Color;
}
i += 1;
}
// Find any comments
Instance = Line.IndexOf("//") + 1;
// If there are comments, color them
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = (Line.Length - Instance + 1);
rtfText.SelectionColor = Color.Green;
}
if (Instance == 1)
{
// Unlock the update, restore the start and exit
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
LockWindowUpdate(0);
return; // TODO: might not be correct. Was : Exit Sub
}
// Restore the selectionstart
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
// Unlock the update
LockWindowUpdate(0);
}
public void InitalizeKeywords()
{
//KeyWords[] Words = new KeyWords[13];
Words[0].Word = "#clean";
Words[0].Color = Color.Blue;
Words[1].Word = "#remove";
Words[1].Color = Color.Blue;
Words[2].Word = "#removeall";
Words[2].Color = Color.Blue;
Words[3].Word = "#include";
Words[3].Color = Color.Blue;
Words[4].Word = "#dynamic";
Words[4].Color = Color.Blue;
Words[5].Word = "#org";
Words[5].Color = Color.Blue;
Words[6].Word = "#raw";
Words[6].Color = Color.Blue;
Words[7].Word = "word";
Words[7].Color = Color.Blue;
Words[8].Word = "end";
Words[8].Color = Color.Blue;
Words[9].Word = "lock";
Words[9].Color = Color.DarkBlue;
Words[10].Word = "faceplayer";
Words[10].Color = Color.DarkBlue;
Words[11].Word = "release";
Words[11].Color = Color.DarkBlue;
Words[12].Word = "message";
Words[12].Color = Color.FromArgb(90, 90, 255);
Words[13].Word = "msgbox";
Words[13].Color = Color.FromArgb(90, 90, 255);
}
private void Form1_Load(object sender, EventArgs e)
{
InitalizeKeywords();
this.rtfText.Text = string.Empty;
ColorRtb(rtfText);
}
Okay, now the problem/question: if there's many text in the RTB, it's going slow.
Second problem/question: if I paste text in the RTB, it only colors the last visible lines. I think it is because the 'ColorVisibleLines();'. But if I use 'ColorRtb(rtfText);' it is too slow...
Kind Regards.
I have a problem with Syntax Highlighting in a RichTextBox. I'm using the following code, found in a thread here.
[DllImport("user32.dll")]
extern static int SendMessage(int hwnd, int message, int wparam, int lparam);
[DllImport("user32.dll")]
extern static int LockWindowUpdate(int hwnd);
int[] strComm = null;
KeyWords[] Words = new KeyWords[14];
public enum EditMessages
{
LineIndex = 187,
LineFromChar = 201,
GetFirstVisibleLine = 206,
CharFromPos = 215,
PosFromChar = 1062
}
public struct KeyWords
{
public string Word;
public Color Color;
}
public Form1()
{
InitializeComponent();
}
private void rtfText_TextChanged(object sender, EventArgs e)
{
//Handles rtfText.KeyPress
ColorVisibleLines(rtfText);
}
public void ColorVisibleLines(RichTextBox rtfText)
{
int FirstLine = FirstVisibleLine();
int LastLine = LastVisibleLine();
int FirstVisibleChar;
int i = FirstLine;
if ((FirstLine == 0) & (LastLine == 0))
{
// If there is no text in the control, it will run an error
// So, if there isn't any text, just exit
return; // TODO: might not be correct. Was : Exit Sub
}
else
{
while (i < LastLine)
{
FirstVisibleChar = GetCharFromLineIndex(FirstLine);
ColorLineNumber(rtfText, FirstLine, FirstVisibleChar);
FirstLine += 1;
i += 1;
}
}
}
public int FirstVisibleLine()
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.GetFirstVisibleLine, 0, 0);
}
public int GetCharFromLineIndex(int LineIndex)
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.LineIndex, LineIndex, 0);
}
public int LastVisibleLine()
{
int LastLine = FirstVisibleLine() + (rtfText.Height / rtfText.Font.Height);
if (LastLine > rtfText.Lines.Length | LastLine == 0)
{
LastLine = rtfText.Lines.Length;
}
return LastLine;
}
public void ColorRtb(RichTextBox rtftext)
{
int FirstVisibleChar;
int i = 0;
while (i < rtftext.Lines.Length)
{
FirstVisibleChar = GetCharFromLineIndex(i);
ColorLineNumber(rtftext, i, FirstVisibleChar);
i += 1;
}
}
public void ColorLineNumber(RichTextBox rtfText, int LineIndex, int lStart)
{
string Line = rtfText.Lines[LineIndex].ToLower();
int i = 0;
int Instance;
int SelectionAt = rtfText.SelectionStart;
// Lock the update
LockWindowUpdate(rtfText.Handle.ToInt32());
// Color the line black to remove any previous coloring
rtfText.SelectionStart = lStart;
rtfText.SelectionLength = Line.Length;
rtfText.SelectionColor = Color.Black;
// Loop through all the Keywords
while (i < Words.Length)
{
// See if the word is in the Line
//Instance = string.Compare(Line, Words[i].Word); // InStr(Line, Words[i].Word);
Instance = Line.IndexOf(Words[i].Word) + 1;
// If the lines contains the word, color it
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = Words[i].Word.Length;
rtfText.SelectionColor = Words[i].Color;
}
i += 1;
}
// Find any comments
Instance = Line.IndexOf("//") + 1;
// If there are comments, color them
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = (Line.Length - Instance + 1);
rtfText.SelectionColor = Color.Green;
}
if (Instance == 1)
{
// Unlock the update, restore the start and exit
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
LockWindowUpdate(0);
return; // TODO: might not be correct. Was : Exit Sub
}
// Restore the selectionstart
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
// Unlock the update
LockWindowUpdate(0);
}
public void InitalizeKeywords()
{
//KeyWords[] Words = new KeyWords[13];
Words[0].Word = "#clean";
Words[0].Color = Color.Blue;
Words[1].Word = "#remove";
Words[1].Color = Color.Blue;
Words[2].Word = "#removeall";
Words[2].Color = Color.Blue;
Words[3].Word = "#include";
Words[3].Color = Color.Blue;
Words[4].Word = "#dynamic";
Words[4].Color = Color.Blue;
Words[5].Word = "#org";
Words[5].Color = Color.Blue;
Words[6].Word = "#raw";
Words[6].Color = Color.Blue;
Words[7].Word = "word";
Words[7].Color = Color.Blue;
Words[8].Word = "end";
Words[8].Color = Color.Blue;
Words[9].Word = "lock";
Words[9].Color = Color.DarkBlue;
Words[10].Word = "faceplayer";
Words[10].Color = Color.DarkBlue;
Words[11].Word = "release";
Words[11].Color = Color.DarkBlue;
Words[12].Word = "message";
Words[12].Color = Color.FromArgb(90, 90, 255);
Words[13].Word = "msgbox";
Words[13].Color = Color.FromArgb(90, 90, 255);
}
private void Form1_Load(object sender, EventArgs e)
{
InitalizeKeywords();
this.rtfText.Text = string.Empty;
ColorRtb(rtfText);
}
Okay, now the problem/question: if there's many text in the RTB, it's going slow.
Second problem/question: if I paste text in the RTB, it only colors the last visible lines. I think it is because the 'ColorVisibleLines();'. But if I use 'ColorRtb(rtfText);' it is too slow...
Kind Regards.