winforms C# Graphics类要制作签名面板:绘图板输入

envsm3lx  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(155)

我试图使签名面板在c# windowsform应用程序中输入是从绘图板
我的代码如下这段代码工作的线条画不创建。
因此,请建议如何创建和线。

{
        Graphics graphics;
        Boolean cusorMoving = false;
        Pen cursorPen;
        int cursorX = -1;
        int cursorY = -1;
       
        public SignPad()
        {
            InitializeComponent();
            graphics = panel2.CreateGraphics();
            cursorPen = new Pen(Color.Black, 2);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        }

鼠标按下事件

private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            cusorMoving = true;
            cursorX = e.X;
            cursorY = e.Y;
        }

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            cusorMoving = false;
           
            cursorX = -1;
            cursorY = -1;

        }

鼠标移动事件

private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            if (cursorX != -1 && cursorY != -1 && cusorMoving == true)
            {
                graphics.DrawLine(cursorPen, new Point(cursorX, cursorY), e.Location);
                cursorX = e.X;
                cursorY = e.Y;
            }            
                         
        }
ncecgwcz

ncecgwcz1#

你需要将各个点存储在集合中,然后在Paint处理程序中分别绘制它们。每次向集合中添加一个点时,你还需要告诉面板在添加新线段的区域中进行绘制。类似于:

using System.Collections.Generic;
using System.Drawing;

namespace Lines
{
    public partial class SignPad : Form
    {
        Pen cursorPen = SystemPens.ControlText;
        List<Point> points = new List<Point>();
        bool cursorMoving = false;

        public SignPad()
        {
            InitializeComponent();

            cursorPen = new Pen(Color.Black, 2);
            cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        }

        private void panel2_Paint(object? sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            for (int i = 1; i < points.Count; ++i)
                g.DrawLine(cursorPen, points[i - 1], points[i]);
        }

        private void panel2_MouseDown(object? sender, MouseEventArgs e)
        {
            if (!cursorMoving)
            {
                cursorMoving = true;
                points.Clear();
                points.Add(e.Location);
                panel2.Invalidate();
            }
        }

        private void panel2_MouseMove(object? sender, MouseEventArgs e)
        {
            if (cursorMoving && points.Count > 0)
            {
                var p = e.Location;
                var q = points[points.Count - 1];
                var r = Rectangle.FromLTRB(Math.Min(p.X, q.X), Math.Min(p.Y, q.Y), Math.Max(p.X, q.X), Math.Max(p.Y, q.Y));
                r = Rectangle.Inflate(r, (int)cursorPen.Width, (int)cursorPen.Width);

                points.Add(p);
                panel2.Invalidate(r);
            }
        }

        private void panel2_MouseUp(object? sender, MouseEventArgs e)
        {
            cursorMoving = false;
        }
    }
}

不要忘记在设计器中添加Paint处理程序,方法与添加MouseMove、MouseDown和MouseUp处理程序相同。

相关问题