C# 在文本框C后面绘制线条#

C# 在文本框C后面绘制线条#,c#,drawing,richtextbox,C#,Drawing,Richtextbox,我正在创建一个简单的笔记本应用程序。我被要求将输入区域设置为一张笔记本纸,文本位于浅蓝色线条上。我正在努力使这项工作成功,但似乎失败得很惨 到目前为止,我已经创建了一个位于面板顶部的透明RichTextBox。文本框为: using System; using System.Windows.Forms; public class TransparentTextBox : RichTextBox { public TransparentTextBox() { th

我正在创建一个简单的笔记本应用程序。我被要求将输入区域设置为一张笔记本纸,文本位于浅蓝色线条上。我正在努力使这项工作成功,但似乎失败得很惨

到目前为止,我已经创建了一个位于面板顶部的透明RichTextBox。文本框为:

using System;
using System.Windows.Forms;

public class TransparentTextBox : RichTextBox
{
    public TransparentTextBox()
    {
        this.SetStyle(ControlStyles.Opaque, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parms = base.CreateParams;
            parms.ExStyle |= 0x20;  // Turn on WS_EX_TRANSPARENT
            return parms;
        }
    }
}
面板的油漆代码:

private void paper_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.Clear(Color.White);
    g.DrawLine(new Pen(Brushes.LightPink, 2), 20, 0, 20, paper.Height);
    int h = TextRenderer.MeasureText("Testj", txtBody.Font).Height;
    for (int x = 2 + h; x < paper.Height; x += h)
    {
        g.DrawLine(new Pen(Brushes.LightSkyBlue, 2), 0, x, paper.Width, x);
    }
}
然后对上面的油漆进行了修改,以使用:

for (int x = inf.nPos % h; x < paper.Height; x += h)
{
    g.DrawLine(new Pen(Brushes.LightSkyBlue, 2), 0, x, paper.Width, x);
}    
for(int x=inf.nPos%h;x

我也试着使用nTrackPos,但两人似乎都没有像我希望的那样跟随文本。我对C#不太熟悉,所以我想知道我错过了什么/可以做得更好。我正在使用Visual Studio 2008和Visual C#2008。NETFramework3.5SP1

所以,下面是我在谷歌搜索之后得出的结论。我决定进一步了解Gusman对我的问题的评论,并再次查看文本框上的绘图。打了几场比赛后,我意识到我没有正确计算起跑线的位置。因此,我将自定义RichTextBox重新配置为:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Journal
{
    class CustomRichTextBox : RichTextBox
    {
        private const int WM_HSCROLL      = 0x114;
        private const int WM_VSCROLL      = 0x115;
        private const int WM_MOUSEWHEEL   = 0x20A;
        private const int WM_PAINT        = 0x00F;
        private const int EM_GETSCROLLPOS = 0x4DD;
        public int lineOffset = 0;

       [DllImport("user32.dll")]
        public static extern int SendMessage(
              IntPtr hWnd,
              int Msg,
              IntPtr wParam,
              ref Point lParam 
              );

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_PAINT)
            {
                using (Graphics g = base.CreateGraphics())
                {
                    Point p = new Point();

                    //get the position of the scrollbar to calculate the offset
                    SendMessage(this.Handle, EM_GETSCROLLPOS, IntPtr.Zero, ref p);

                    //draw the pink line on the side
                    g.DrawLine(new Pen(Brushes.LightPink, 2), 0, 0, 0, this.Height);

                    //determine how tall the text will be per line
                    int h = TextRenderer.MeasureText("Testj", this.Font).Height;

                    //calculate where the lines need to start
                    lineOffset = h - (p.Y % h);

                    //draw lines until there is no more box
                    for (int x = lineOffset; x < Height; x += h)
                    {
                        g.DrawLine(new Pen(Brushes.LightSkyBlue, 2), 0, x, Width, x);
                    }

                    //force the panel under us to draw itself.
                    Parent.Invalidate();
                }
            }

        }

        public CustomRichTextBox()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

    }
}
使用系统;
使用系统图;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
名称空间日志
{
类CustomRichTextBox:RichTextBox
{
私有常量int wmhscroll=0x114;
私有常量int WM_VSCROLL=0x115;
私有常量int WM_鼠标轮=0x20A;
私有常量int WM_PAINT=0x00F;
私有常量int EM_GETSCROLLPOS=0x4DD;
公共int lineOffset=0;
[DllImport(“user32.dll”)]
公共静态外部内部发送消息(
IntPtr hWnd,
int Msg,
IntPtr wParam,
参考点LPRAM
);
受保护的覆盖无效WndProc(参考消息m)
{
基准WndProc(参考m);
如果(m.Msg==WM_油漆)
{
使用(Graphics g=base.CreateGraphics())
{
点p=新点();
//获取滚动条的位置以计算偏移量
SendMessage(this.Handle,EM_GETSCROLLPOS,IntPtr.Zero,ref p);
//在侧面画一条粉红色的线
g、 抽绳(新笔(画笔。浅粉色,2),0,0,0,这个高度);
//确定每行文本的高度
inth=TextRenderer.MeasureText(“Testj”,this.Font).Height;
//计算线需要从哪里开始
lineOffset=h-(同比%h);
//画线直到没有更多的盒子
对于(int x=线偏移;x<高度;x+=h)
{
g、 抽绳(新笔(画笔。浅天蓝,2),0,x,宽度,x);
}
//迫使我们下面的小组自行撤退。
Parent.Invalidate();
}
}
}
公共CustomRichTextBox()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
}
}
}

然后我把这个盒子放在一个面板里面,得到我想要的填充物。面板被迫使用文本框重新绘制自身。

也许您可以在richtextbox内绘制线条……我也尝试过这样做,问题是它们仍然没有跟随文本的线条。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Journal
{
    class CustomRichTextBox : RichTextBox
    {
        private const int WM_HSCROLL      = 0x114;
        private const int WM_VSCROLL      = 0x115;
        private const int WM_MOUSEWHEEL   = 0x20A;
        private const int WM_PAINT        = 0x00F;
        private const int EM_GETSCROLLPOS = 0x4DD;
        public int lineOffset = 0;

       [DllImport("user32.dll")]
        public static extern int SendMessage(
              IntPtr hWnd,
              int Msg,
              IntPtr wParam,
              ref Point lParam 
              );

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_PAINT)
            {
                using (Graphics g = base.CreateGraphics())
                {
                    Point p = new Point();

                    //get the position of the scrollbar to calculate the offset
                    SendMessage(this.Handle, EM_GETSCROLLPOS, IntPtr.Zero, ref p);

                    //draw the pink line on the side
                    g.DrawLine(new Pen(Brushes.LightPink, 2), 0, 0, 0, this.Height);

                    //determine how tall the text will be per line
                    int h = TextRenderer.MeasureText("Testj", this.Font).Height;

                    //calculate where the lines need to start
                    lineOffset = h - (p.Y % h);

                    //draw lines until there is no more box
                    for (int x = lineOffset; x < Height; x += h)
                    {
                        g.DrawLine(new Pen(Brushes.LightSkyBlue, 2), 0, x, Width, x);
                    }

                    //force the panel under us to draw itself.
                    Parent.Invalidate();
                }
            }

        }

        public CustomRichTextBox()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

    }
}