1

我正在使用 Windows 窗体应用程序并使用一个按钮生成随机数并在表单上绘图。单击按钮时,它正在使用 Graphics.Drawing 方法添加一个随机数。问题是当我第一次点击按钮时它工作正常并添加一个随机数,即 11111。当我再次点击按钮时,它会添加一个新的随机数(在下一个位置),但它也会将以前的数字更改为新生成的随机数。

更新:(添加完整代码)

编辑:我已经将随机数移到勺子之外,所以现在它不会生成相同的数字,但仍然会将旧随机数更改为其他随机数。

主类:

using System;
using System.Collections;
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 DrawingText
{
    public partial class Form1 : Form
    {

        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseMovePosition = e.Location;
            if (e.Button == MouseButtons.Left)
                mousePressdDown = 1;

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDownPosition = e.Location;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition);
            drawnItemsList.Add(a);
            mousePressdDown = 0;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a.old, a.cur);

            }
         draw(e.Graphics, mouseDownPosition, mouseMovePosition);


        }
        private void draw(Graphics e, Point mold, Point mcur)
        {
            Pen p = new Pen(Color.Black, 2);

                    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
                    {
                            string header2 = rnd.Next().ToString();
                            RectangleF header2Rect = new RectangleF();
                            int moldX = mold.X - 5;
                            int moldY = mold.Y;

                            header2Rect.Location = new Point(moldX, moldY);
                            header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                            e.DrawString(header2, useFont, Brushes.Black, header2Rect);
                        }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

绘图数据类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DrawingText
{
    [Serializable]
    class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
        }
        public DrawingData(Point old, Point cur)
        {
            mold = old;
            mcur = cur;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }


    }
}

单击按钮 3 次,它将旧值替换为新值: 在此处输入图像描述

4

3 回答 3

1

You need to store the random value with the point values in the DrawingData class, like this:

Main Class:

namespace DrawingText
{
    public partial class Form1 : Form
    {
        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition, rnd.Next().ToString());
            drawnItemsList.Add(a);
            mousePressdDown = 0;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a);
            }
            draw(e.Graphics, mouseDownPosition, mouseMovePosition);
        }

        private void draw(Graphics e, DrawingData a)
        {
            Pen p = new Pen(Color.Black, 2);

            using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
            {
                RectangleF header2Rect = new RectangleF();
                int moldX = a.old.X - 5;
                int moldY = a.old.Y;

                header2Rect.Location = new Point(moldX, moldY);
                header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                e.DrawString(a.Rand, useFont, Brushes.Black, header2Rect);
            }
        }
    }
}

Drawing Data Class:

namespace DrawingText
{
    [Serializable]
    public class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition
        private string randValue; // random data value

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
            randValue = String.Empty;
        }

        public DrawingData(Point old, Point cur, string rand)
        {
            mold = old;
            mcur = cur;
            randValue = rand;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }

        public sting Rand
        {
            get
            {
                return randValue;
            }
            set
            {
                randValue = value;
            }
     }
}
于 2013-08-26T01:04:15.770 回答
0

这是使用图形路径即时制作的:

    GraphicsPath gp;
    int moldX = 10;
    int moldY = 10;

    public Form1()
    {
        InitializeComponent();
        gp = new GraphicsPath();  
    }


    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.FillPath(Brushes.Black, gp);
        // if you want the numbers outlined do e.Graphics.DrawPath
    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddToPath();
        Invalidate();   
    }

    private void AddToPath()
    {
        using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
        {
            Random rnd = new Random();
            string header2 = rnd.Next().ToString();
            int strsize = TextRenderer.MeasureText(header2, useFont).Height;
            StringFormat format = StringFormat.GenericDefault;
            gp.AddString(header2, useFont.FontFamily, 1, 28, new Point(moldX, moldY), format);

            moldX += 5;
            moldY += strsize;
        }
    }
于 2013-08-26T00:17:55.503 回答
0

您每次都在循环中重新创建随机数,这将导致它具有相同的种子和相同的第一个数字。这就是为什么你所有的数字都是一样的。你应该。

  1. 将您的随机数移到方法和循环之外,并改用它。将 Random rnd = new Random() 行更改为 rnd = new Random()。您已经在类中有一个变量来保存随机数。

  2. 如果您希望之前的随机数与上次相同,则需要将它们存储在某个列表中并在油漆上绘制它们。您当前每次都在创建一组新的随机数。

于 2013-08-26T00:00:32.457 回答