簡體   English   中英

C# DrawLines 到 DrawBeziers

[英]C# DrawLines to DrawBeziers

我有這個任務,我必須通過點擊表格來創建點。 這些點存儲在一個數組中,我必須用它來畫線。

到現在為止還挺好。

但現在它要求我包括一個復選框,在選中時,應該立即將繪制的線路繪制到繪制的Beziers中。

有沒有人知道我可能會做什么?

這是我到目前為止的代碼:

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

namespace LijnTrekken
{
    public partial class Form1 : Form
    {
        int aantalKliks = 0;
        Point startpunt = new Point();
        Point eindpunt = new Point();

        Point[] points = new Point[20];
        int regelIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            aantalKliks = aantalKliks + 1;
            regelIndex = aantalKliks;

            try
            {
                Point[] bezierPoints = new Point[20]; 

                if (chbGolven.Checked)
                {
                    if (regelIndex < 5)
                    {
                        points[regelIndex] = e.Location;
                    }
                    else
                    {
                        Pen tekenpen = new Pen(Color.Red);
                        Graphics papier = this.CreateGraphics();
                        papier.DrawBeziers(tekenpen, bezierPoints);
                    }
                }
                else
                {
                    if (regelIndex < 20)
                    {
                        points[regelIndex] = e.Location;
                    }
                    else
                    {
                        Pen tekenpen = new Pen(Color.Red);
                        Graphics papier = this.CreateGraphics();
                        papier.DrawLines(tekenpen, points);
                    }
                }
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
        }
    }

此代碼顯示如何將線重新繪制為 BezierLines。 觀看下面的演示。

namespace BeziersLines 
{
    public partial class Form1 : Form
    {
        List<PointF> points;
        Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0), 3);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // you can push some start Points here, [ new List<PointF>() {new PointF(0,0), new PointF(100,100), ...} ]
            points = new List<PointF>(); 
        }

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

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            points.Add(e.Location);
            label1.Text = "Points count: " + points.Count().ToString();
            pictureBox1.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (points.Count() > 1)
            {
                var g = e.Graphics;
                if (!checkBox1.Checked)
                {
                    // Draw Lines
                    g.DrawLines(pen, points.ToArray());
                }
                else
                {
                    // Draw Bezier lines
                    var bezierPoints = points.Take(points.Count - (points.Count - 1) % 3).ToList();
                    g.DrawBeziers(pen, bezierPoints.ToArray());
                }
            }
        }
    }
}

演示

下次在英語上使用可讀的命名,它可以幫助人們更好地理解你,並且為所有代碼閱讀器/用戶命名它是一個好習慣,不僅僅是你。

聚苯乙烯

var bezierPoints = points.Take(points.Count - (points.Count - 1) % 3).ToList();

此行計算數組中的點數,基於 MSD arctile 關於 DrawBeziers(Pen, PointF[]) 的具體說明: “數組中的點數應為 3 加 1 的倍數,例如 4, 7 ,或 10。”

您可以在 Form_MouseClick 上填充點數組。 在 Form_Paint 上,PaintEventArgs 中將有圖形,您可以使用它進行繪制。 在 Form_Paint 上,您應該通過用背景顏色填充表單矩形來清除以前的繪畫,然后您應該根據是否選中復選框從點數組中繪制線條或貝塞爾曲線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM