0

我正在制作一个文本编辑器,我想知道是否有更快/更可靠的方法来执行这些功能。我在这里写的是我认为我能做的最好的并且可能能做到的,除了我有一种感觉 FirstVisibleLine 和 LineCount 已经完成但 VisibleLines 可以更快地返回......?

VisibleLines 应该返回在文本区域中可见的文本行数。FirstVisibleLine 应该返回文本区域中的第一条可见行 LineCount 应该返回文本区域中的行数

    private int VisibleLines()
    {
        int topIndex = this.GetCharIndexFromPosition(new Point(0, 0));
        int bottomIndex = this.GetCharIndexFromPosition(new Point(0, this.Height - 1));

        int topLine = this.GetLineFromCharIndex(topIndex);
        int bottomLine = this.GetLineFromCharIndex(bottomIndex);

        return bottomLine - topLine;
    }

    private int FirstVisibleLine()
    {
        return this.GetLineFromCharIndex(this.GetCharIndexFromPosition(new Point(0,0)));
    }

    public int LineCount
    {
        get
        {
            Message msg = Message.Create(this.Handle, EM_VISIBLELINES, IntPtr.Zero, IntPtr.Zero);
            base.DefWndProc(ref msg);
            return msg.Result.ToInt32();
        }
    }
4

1 回答 1

2

您可以通过向文本框发送消息以获取第一行而不是使用GetCharIndexFromPositionandGetLineFromCharIndex一次来获取第一条可见行。我不确定为什么 TextBox(或 TextBoxBase)类没有实现这一点。

private const int EM_GETFIRSTVISIBLELINE = 206;
private int GetFirstVisibleLine(TextBox textBox) 
{
    return SendMessage(textBox.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
}

至于 VisibleLines,我认为您将不得不继续计算bottomLine.

于 2012-08-20T19:22:30.473 回答