0
因此,我正在为我喜欢的语言编写简单的代码编辑器。我有语法高亮进行得非常好。 问题是,如果我在我已经写好的文本之前回过头来,它会把我整个指针上的所有事情都挂起来。 这里是我的代码,我的道歉张贴了这么多:C#编写代码编辑器问题
public partial class Form1 : Form
{
public string MainFontName = "Courier New";
public int MainFontSize = 12;
public Color MainFontColor = Color.Black;
[DllImport("user32.dll")] // import lockwindow to remove flashing
public static extern bool LockWindowUpdate(IntPtr hWndLock);
public Regex codeFunctions = new Regex("draw_line|draw_rectangle|draw_circle");
public Regex codeKeywords = new Regex("and|for|while|repeat|or|xor|exit|break|case|switch|if|then|with|true|false");
public Form1()
{
InitializeComponent();
CodeInput.Font = new Font(MainFontName, MainFontSize, FontStyle.Regular);
}
private void CodeInput_TextChanged(object sender, EventArgs e)
{
CodeInput.Font = new Font(MainFontName, MainFontSize, FontStyle.Regular);
try
{
LockWindowUpdate(CodeInput.Handle);
int selPos = CodeInput.SelectionStart;
CodeInput.Select(0, CodeInput.TextLength);
CodeInput.SelectionFont = new Font(MainFontName, MainFontSize, FontStyle.Regular);
CodeInput.SelectionColor = Color.Black;
CodeInput.SelectionLength = 0;
CodeInput.SelectionStart = selPos;
//Match the functions
foreach (Match keyWordMatch in codeFunctions.Matches(CodeInput.Text))
{
CodeInput.Select(keyWordMatch.Index, keyWordMatch.Length);
CodeInput.SelectionColor = Color.Red;
CodeInput.SelectionStart = selPos;
CodeInput.SelectionColor = MainFontColor;
CodeInput.SelectionLength = 0;
}
// Match the keywords
foreach (Match keyWordMatch in codeKeywords.Matches(CodeInput.Text))
{
Font oFont = new Font(MainFontName, MainFontSize, FontStyle.Bold);
Font nFont = new Font(MainFontName, MainFontSize, FontStyle.Regular);
CodeInput.Select(keyWordMatch.Index, keyWordMatch.Length);
CodeInput.SelectionColor = Color.Blue;
CodeInput.SelectionFont = oFont;
CodeInput.SelectionStart = selPos;
CodeInput.SelectionColor = MainFontColor;
CodeInput.SelectionFont = nFont;
CodeInput.SelectionLength = 0;
}
}
finally
{
LockWindowUpdate(IntPtr.Zero);
}
}
}
感谢您的帮助。
指针和win32 API调用停止闪烁?不暂停工作? – 2011-02-18 02:42:05