TextBoxをMultilineにした時、何か寂しいので、ノートのように罫線を付けてみました。
難しいことはしてませんが、「OnPaintBackground」をオーバーライドしようとしたら、呼ばれないみたいなので「WndProc」で処理しました。
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// 描画メッセージ or IMEウインドウの変化メッセージ
// + 罫線が必要な場合処理します
if (m.Msg == WM_PAINT || m.Msg == WM_IME_NOTIFY)
{
Graphics g = this.CreateGraphics();
StringFormat sf = new StringFormat();
// 端との間に隙間がないと変なので、少し隙間を空けます
int lineWidth = this.ClientSize.Width - 3;
// 文字列の高さから行の高さを計算します
CharacterRange[] characterRanges ={ new CharacterRange(0, 1) };
sf.SetMeasurableCharacterRanges(characterRanges);
RectangleF layoutRect = new RectangleF(0, 0, 100, 100);
Region[] stringRegions = g.MeasureCharacterRanges("|", this.Font, layoutRect, sf);
RectangleF charRect = stringRegions[0].GetBounds(g);
int lineHeight = (int)charRect.Height;
// 行罫線をクライアント領域内に描画します
Pen pen = new Pen(_ruredColor);
int clientHeight = this.ClientSize.Height;
for (int i = 1; i * lineHeight < clientHeight; i++)
{
g.DrawLine(pen, new Point(2, lineHeight * i), new Point(lineWidth, lineHeight * i));
}
pen.Dispose();
}
}
こんな感じになります。

0 件のコメント:
コメントを投稿