퇴근5분전

사용자 삽입 이미지
<< Textbox LineNumber >>

 스크롤 처리도 안되있고 글자 폰트는 9pt 밖에 라인에 맞게 그릴순 없는 소스지만
조금만더 노가다를 하묜... 비슷하게 구현되지 않을까 ? 싶어서 일단... 등록해둠.

문제점 : Textbox에 폰트크기와 판넬에 폰트크기가 다르게 표시되는 점.
           -> 이래서 글자 줄간격이 차이가 나는데 아래 9pt는 노가다로 1.79라는 수를 구해서 라인별로 곱하는데  다른 크기에서는 다른값이 요구되므로 각 값마다 찾을라면... 이건 미친 노가다라고 보임.

문제점 : Textbox에서 스크롤 되는 위치값 구해서 라인넘버 그려지는 영역에 대한 처리도 해야되는데... 그게 없음.


활용 가치? 그닥 없음. 한번 해본것이기 때문에...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TextBox_LineNumber
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            font = this.textBox1.Font;
        }

        Font font = null;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int totalLine = this.textBox1.Lines.Length;
            
            for (float loop = 1f; loop <= totalLine; loop++)
            {
                SizeF sizef = e.Graphics.MeasureString(loop.ToString(), font);

                //e.Graphics.DrawString(loop.ToString(), font, Brushes.Brown, 0f, (loop - 1f) * sizef.Height /*); // */ + 1.79f );
                e.Graphics.DrawString(loop.ToString(), font, Brushes.Brown, 0f, (loop - 1f) * this.textBox1.Font.GetHeight() - (loop-1)* 1.79f); // 9pt에서 되는놈
            }                
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.Invalidate(new  Rectangle(0, 0, this.Padding.Left, this.Height));
        }
    }
}