广告位招租

ScintillaNET 显示行号

一些不熟悉闪烁体的人可能想知道,为什么显示行号会有自己的诀窍,而人们会认为这就像翻转一个布尔属性一样简单。不是的。行号的主题涉及到更大的边距主题。在闪烁体中,控件的左边缘最多有五个边距(0到4),其中行号只是其中之一。按照惯例闪烁设置Margin.Type边距0到的属性MarginType.Number,使其成为事实上的行号边距。任何边距都可以显示行号,如果Type属性设置为MarginType.Number. 通过设置Width从0到0的边距。要显示默认行号边距,请增加其宽度:

scintilla.Margins[0].Width = 16;

但是,您很快就会发现,一旦达到100范围内的行号,行号边距的宽度就不再足够了。闪烁不会自动增加或减少边距的宽度——包括行号边距。为什么?它可以追溯到这样一个事实,即边距可以显示行号,也可以显示其他完全不需要的动态增长和收缩特性。

行号边距可以动态地增长或缩小,它只需要一点额外的代码你的部分。在下面的配方中,我们处理TextChanged事件,以便我们可以知道行数何时更改。(我们可以使用其他几个事件来确定内容是否已更改,但是TextChanged会很好的)然后我们使用文本宽度方法。最后,设置Width行号边距。由于行数的变化远小于事件事件将触发

private int maxLineNumberCharLength;
private void scintilla_TextChanged(object sender, EventArgs e)
{
    // Did the number of characters in the line number display change?
    // i.e. nnn VS nn, or nnnn VS nn, etc...
    var maxLineNumberCharLength = scintilla.Lines.Count.ToString().Length;
    if (maxLineNumberCharLength == this.maxLineNumberCharLength)
        return;

    // Calculate the width required to display the last line number
    // and include some padding for good measure.
    const int padding = 2;
    scintilla.Margins[0].Width = scintilla.TextWidth(Style.LineNumber, new string('9', maxLineNumberCharLength + 1)) + padding;
    this.maxLineNumberCharLength = maxLineNumberCharLength;
}

注意:行号边距中显示的文本颜色可以通过Style.LineNumber样式定义

自定义行号

闪烁体没有内置的功能,可以将行号显示更改为其他文本或格式;但是,使用标准文本边距(而不是数字边距),我们可以使用我们想要的任何文本或格式来模拟行号。

在这个例子中,我们假设我们的用户要么是一个学者,要么是受虐狂,想要以十六进制格式显示行号。第一步是将页边距0(传统上用于行号)更改为右对齐的文本边距。

scintilla.Margins[0].Type = MarginType.RightText;
scintilla.Margins[0].Width = 35;

现在我们只需要将每行的边距文本设置为其行号的十六进制表示形式,并在行更改时保持最新。我将使用更改来监视Insert删除事件,因为它们包含指示添加或删除的行数的属性。使用TextChanged事件不会(轻易)告诉我的。我还做了一个小的优化,只更新受影响的行,而不是每行。什么都可以下列的改变了行,但不是在它之前——这些仍然有效。

private void UpdateLineNumbers(int startingAtLine)
{
    // Starting at the specified line index, update each
    // subsequent line margin text with a hex line number.
    for (int i = startingAtLine; i < scintilla.Lines.Count; i++)
    {
        scintilla.Lines[i].MarginStyle = Style.LineNumber;
        scintilla.Lines[i].MarginText = "0x" + i.ToString("X2");
    }
}

private void scintilla_Insert(object sender, ModificationEventArgs e)
{
    // Only update line numbers if the number of lines changed
    if (e.LinesAdded != 0)
        UpdateLineNumbers(scintilla.LineFromPosition(e.Position));
}

private void scintilla_Delete(object sender, ModificationEventArgs e)
{
    // Only update line numbers if the number of lines changed
    if (e.LinesAdded != 0)
        UpdateLineNumbers(scintilla.LineFromPosition(e.Position));
}

原文链接:https://www.exueyuan.top/515.html,转载请注明出处。

0
广告位招租

评论0

请先

广告位招租
易学手机界面分析工具上线!可用于安卓手机界面,模拟器界面分析,加速自动化开发!查看详情
没有账号? 注册  忘记密码?

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码

Warning: error_log(/www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/#log/log-2916.txt): failed to open stream: No such file or directory in /www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/spider.class.php on line 2900