RichTextBox を下までスクロール の続き。
ScrollToCaret を使わない方法で、前の記事と違い、目で確認しなくてもよくなった。Focus も必要ない。
[System.Runtime.InteropServices.DllImport("USER32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wp, out Point lp);
private const int EM_SETSCROLLPOS = 0x04DE;
int lineHeight = this.richTextBox1.GetPositionFromCharIndex(
this.richTextBox1.GetFirstCharIndexFromLine(1)).Y - this.richTextBox1.GetPositionFromCharIndex(
this.richTextBox1.GetFirstCharIndexFromLine(0)).Y;
Point p = new Point(0, this.richTextBox1.Lines.Length * lineHeight - this.richTextBox1.ClientSize.Height);
// BorderStyle が None 以外だと、1 ずれる
if (this.richTextBox1.BorderStyle != BorderStyle.None)
{
++p.Y;
}
if (p.Y > 0)
{
SendMessage(this.richTextBox1.Handle, EM_SETSCROLLPOS, 0, out p);
}
lineHeight は一行の高さで、DOBON.NETプログラミング道掲示板 [One Topic All View / Re[2]: RichTextBoxで1行あたりの高さの求め方 / Page: 0] に書いてある方法で計算しています。
12/18 追記 続き を書いた。
2013年11月14日
RichTextBox を下までスクロール 続き
この記事へのコメント
コメントを書く
この記事へのトラックバック