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

GetCharIndexFromPosition一度使用する代わりに、テキストボックスにメッセージを送信して一番上の行を取得することで、最初に表示される行を取得できますGetLineFromCharIndex。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 に答える