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