C# 如何使用RichTextBox突出显示语法并禁用自动滚动?
因此,我花了大约三天时间在互联网上搜索——谷歌、Stack Overflow、微软的C#文档——但没有得到任何有用的结果。我的问题是,我最近创建了一个非常灵活和快速的语法突出显示RichTextBox,它保存在一个UserControl中。我用WinForms创建了这个 然而,尽管我的项目速度快、灵活性强,但仍然存在一个明显且极其令人讨厌的小故障。这个小毛病是我的RichTextBox会一直自动滚动。我不希望发生这种自动滚动。当我突出显示文本时,我希望用户看到的不是移动或过渡,而是彩色字母和符号。以下是影响我的项目语法突出显示的代码:C# 如何使用RichTextBox突出显示语法并禁用自动滚动?,c#,winforms,richtextbox,syntax-highlighting,C#,Winforms,Richtextbox,Syntax Highlighting,因此,我花了大约三天时间在互联网上搜索——谷歌、Stack Overflow、微软的C#文档——但没有得到任何有用的结果。我的问题是,我最近创建了一个非常灵活和快速的语法突出显示RichTextBox,它保存在一个UserControl中。我用WinForms创建了这个 然而,尽管我的项目速度快、灵活性强,但仍然存在一个明显且极其令人讨厌的小故障。这个小毛病是我的RichTextBox会一直自动滚动。我不希望发生这种自动滚动。当我突出显示文本时,我希望用户看到的不是移动或过渡,而是彩色字母和符号
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private void DoHighlight()
{
if (TextEditor.Text.Length <= 0)
return;
int visibleStart = TextEditor.GetCharIndexFromPosition(new Point(1, 1));
int visibleEnd = TextEditor.GetCharIndexFromPosition(new Point(1, TextEditor.Height - 1)) + TextEditor.Lines[BottomLine].Length;
int[] curSelection = new [] {TextEditor.SelectionStart,TextEditor.SelectionLength};
LineCounter.Focus();
SendMessage(TextEditor.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
TextEditor.SelectAll();
TextEditor.SelectionColor = TextEditor.ForeColor;
if (parser != null)
parser.HighlightText(this, visibleStart, visibleEnd);
TextEditor.SelectionStart = curSelection[0];
TextEditor.SelectionLength = curSelection[1];
SendMessage(TextEditor.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
TextEditor.Invalidate();
TextEditor.Focus();
}
[DllImport(“user32.dll”)]
专用静态外部IntPtr SendMessage(IntPtr hWnd、int msg、IntPtr wp、IntPtr lp);
私有void DoHighlight()
{
if(TextEditor.Text.Length将DoHighlight()方法放在后台进程中。
像这样:
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += DoHighlight();
bgw.RunWorkerAsync();
有了这个技巧,DoHighlight()方法将并行运行&可以进行自动滚动
您还可以在DoHighlight()方法中使用以下代码:
这将暂停您的方法并执行其他事件,然后返回到您的方法。“&自动克隆可能发生”但是…我根本不希望自动克隆发生。这就是您的意思吗?谢谢您的帮助!非常感谢!
Application.DoEvents();