winforms 画一条线,C#将其转换为直线?

gmol1639  于 2023-05-01  发布在  C#
关注(0)|答案(1)|浏览(185)

我如何画一条线,然后用C#把它转换成一条直线?面板A是我的绘画,面板B是结果
我希望程序理解我画了一条线,然后在面板B中将其转换为直线。或者如果我画一个正方形,它就变成了一个完美的正方形
like this
密码

public partial class frmPaint : Form
    {
        public Point x = new();
        public Point y = new();
        public Pen penA = new(Color.Red, 2);
        public Pen Eraser = new(Color.White, 10);
        public Graphics graphics;
        public frmPaint()
        {
            InitializeComponent();
            graphics = panelDraw.CreateGraphics();
        }

        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            y = e.Location;

            if (rbLineWidth5.Checked)
                penA.Width = 5;
            if (rbLineWidth10.Checked)
                penA.Width = 10;
            if (rbLineWidth15.Checked)
                penA.Width = 15;
        }

        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.Location;
                graphics.DrawLine(penA, x, y);
                y = e.Location;
            }

            if (e.Button == MouseButtons.Right)
            {
                x = e.Location;
                graphics.DrawLine(Eraser, x, y);
                y = e.Location;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new();
            if (colorDialog.ShowDialog() == DialogResult.OK)
                penA.Color = colorDialog.Color;
        }
    }
oug3syen

oug3syen1#

此代码使用MouseLeave事件来确定绘制的结束,它还有一个sizeDiff变量来确定开始绘制的点与结束点之间的距离,从而确定它是正方形还是直线。在确定之后,如果是直线,则在起始点和终点之间绘制直线,如果是正方形,则绘制从图形的最低点开始并延伸到图形的最高点的正方形。
希望这对我有帮助,如果你有任何问题,请告诉我。

bool drawing = false; // Drawing Var

    Point xGlobal = new Point(); 
    Point yGlobal = new Point();

    int sizeDiff = 50; //Var pixels to difference square to line  

private void panelDraw_MouseUp(object sender, MouseEventArgs e)
    {
        xGlobal = e.Location; // X Point for draw in the MouseLeave

        int XDiff = (yGlobal.X - xGlobal.X); // Difference start point and end point int width
        int YDiff = (yGlobal.Y - xGlobal.Y); // Difference start point and end point int height

        // Convert difference to positive integer
        if (XDiff < 0)
        {
            XDiff = XDiff * -1;
        }
        if (YDiff < 0)
        {
            YDiff = YDiff * -1;
        }

        bool isSquare = false; // isSquare Var

        // Verify start point and end point 
        if (XDiff < sizeDiff)
        {
            if (YDiff < sizeDiff)
            {
                isSquare = true; // defines whether it is a square or not based on the start and end point
            }
        }

        if (drawing == true)
        {
            if (isSquare)
            {
                //Draw Square in Panel B
                graphicsPanelB.DrawRectangle(penA, new Rectangle(upPoint, new Size(downPoint.X - upPoint.X, downPoint.Y - upPoint.Y))); //Graphics panel B
            } else
            {
                //Draw Line in Panel B
                graphicsPanelB.DrawLine(penA, xGlobal, yGlobal); //Graphics panel B
            }
            

            drawing = false;
        }

    }

    Point upPoint = new Point(); //Point A for Square
    Point downPoint = new Point(); //Point B for Square

    private void panelDraw_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            drawing = true; //On drawing var to MouseLeave event
        }
        y = e.Location;
        yGlobal = y; // Y Point for draw in the MouseLeave
        upPoint = y; //Reference point for Square
        downPoint = y; //Reference point for Square

        if (rbLineWidth5.Checked)
            penA.Width = 5;
        if (rbLineWidth10.Checked)
            penA.Width = 10;
        if (rbLineWidth15.Checked)
            penA.Width = 15;
    }

    private void panelDraw_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            x = e.Location;
            graphics.DrawLine(penA, x, y);
            y = e.Location;

            //Verify to decrease upPoint
            if (x.X < upPoint.X)
            {
                upPoint.X = x.X;
            }
            if (x.Y < upPoint.Y)
            {
                upPoint.Y = x.Y;
            }

            //Verify to increment downPoint
            if (x.X > downPoint.X)
            {
                downPoint.X = x.X;
            }
            if (x.Y > downPoint.Y)
            {
                downPoint.Y = x.Y;
            }
        }

        if (e.Button == MouseButtons.Right)
        {
            x = e.Location;
            graphics.DrawLine(Eraser, x, y);
            y = e.Location;
        }
    }

相关问题