0

我有个问题。

我正在编写一个用手写笔在上面写字的程序。

首先,我创建了一个带有面板的 Windows 窗体。

在此处输入图像描述

二,这段代码:

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Drawing.Drawing2D;

namespace testWrite
{
    public partial class Form1 : Form
    {
        Graphics g;
        int x = -1;
        int y = -1;
        bool moving = false;
        Pen pen;

        public Form1()
        {
            InitializeComponent();
            g = panel1.CreateGraphics();
            pen = new Pen(Color.Black, 5);
            pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                g.DrawLine(pen, new Point(x, y), e.Location);
                x = e.X;
                y = e.Y;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            x = -1;
            y = -1;
            moving = false;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            moving = true;
        }
    }
}

我将这个应用程序与 Wacom intuos 一起使用

在此处输入图像描述

但是结果不是那么好,因为丢失了几句话...haizzz

toi tên la trần

光孝

你好heloo

在此处输入图像描述

特别是当我写得很快或文字很小的时候。

当我在 Microsoft Paint 中书写时,它非常好

在此处输入图像描述 使用像 wacom intuos 这样的数位板在 Windows 窗体中书写的最佳方法是什么?

更新 1: 使用来自TaW的 cmt 。

在此处输入图像描述

谢谢你的帮助。 但是,这不是我需要的...

我将我的代码更改为:

public partial class Form1 : Form
{
    List<Point> curPoints = new List<Point>();
    List<List<Point>> allPoints = new List<List<Point>>();

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        // here we should check if the distance is more than a minimum!
        curPoints.Add(e.Location);
        // let it show
        panel1.Invalidate();
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (curPoints.Count > 1)
        {
            // ToList creates a copy
            allPoints.Add(curPoints.ToList());
            curPoints.Clear();
        }
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (curPoints.Count > 1)
        {
            // begin fresh line or curve
            curPoints.Clear();
            // startpoint
            curPoints.Add(e.Location);
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        // here you can use DrawLines or DrawCurve
        // current line
        if (curPoints.Count > 1) e.Graphics.DrawCurve(Pens.Red, curPoints.ToArray());
        // other lines or curves
        foreach (List<Point> points in allPoints)
            if (points.Count > 1) e.Graphics.DrawCurve(Pens.Red, points.ToArray());
    }
}

但没有比这更好的了。结果更惨……

在此处输入图像描述

我试图写:“你好,我的名字是 Hieu”,但没有运行......

看起来手写板在使用时与鼠标不同。因为,有了鼠标,我觉得这段代码更好......

更新 2:

使用 Idle_Mind 的代码。如果我设置数位板就可以了:

在此处输入图像描述

设置“点击”,不行

在此处输入图像描述

如何解决它,我不想在我的笔上设置“双击”!

4

1 回答 1

1

这是我的版本……非常适合我。您可能需要调整您的平板电脑设置,以使其正确获取所有内容:

public partial class FormTablet : Form
{

    private Point lastPoint;
    private GraphicsPath GP = null;
    private List<GraphicsPath> GPs = new List<GraphicsPath>();

    public FormTablet()
    {
        InitializeComponent();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            lastPoint = new Point(e.X, e.Y);
            GP = new GraphicsPath();
            GP.AddLine(lastPoint, lastPoint);
            GPs.Add(GP);
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Point pt = new Point(e.X, e.Y);
            GP.AddLine(lastPoint, pt);
            lastPoint = pt;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        GP = null;
        pictureBox1.Invalidate();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (checkBox1.Checked)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        }
        using(Pen p = new Pen(Color.Black, (int)numericUpDown1.Value))
        {
            p.LineJoin = LineJoin.Round;
            p.MiterLimit = p.Width / 2;
            foreach (GraphicsPath path in GPs)
            {
                if (path.PathPoints.Count() > 2)
                {
                    // draw the path
                    e.Graphics.DrawPath(p, path);
                }
                else
                {
                    // just draw a single dot
                    Rectangle rc = new Rectangle(Point.Round(path.PathPoints[0]), new Size(1, 1));
                    rc.Inflate((int)numericUpDown1.Value, (int)numericUpDown1.Value);
                    e.Graphics.FillEllipse(Brushes.Black, rc);
                }
            }
        }            
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        GPs.Clear();
        pictureBox1.Invalidate();
    }

}
于 2020-06-24T14:34:51.603 回答