Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Mouse_Test
- {
- public partial class Form1 : Form
- {
- private bool draw = false;
- private bool clearPoints = false;
- private bool drawOnlyOnMove = false;
- private bool connectDrawToLastPoint = false;
- private bool drawAutomatic = true;
- private Timer timer = new Timer();
- private IList<List<Point>> _paths = new List<List<Point>>();
- private List<Point> _currentPath = null; // Initialize to null
- private Random rnd = new Random();
- public Form1()
- {
- InitializeComponent();
- this.DoubleBuffered = true;
- timer.Interval = 10; // Set interval for smoother drawing
- timer.Tick += Timer_Tick;
- if (drawAutomatic)
- {
- _currentPath = new List<Point>();
- _paths.Add(_currentPath); // Initialize the path list
- timer.Start();
- }
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- if (_currentPath == null || _currentPath.Count == 0)
- {
- int xStart = rnd.Next(pictureBox1.Location.X, pictureBox1.Location.X + pictureBox1.Width);
- int yStart = rnd.Next(pictureBox1.Location.Y, pictureBox1.Location.Y + pictureBox1.Height);
- _currentPath = new List<Point> { new Point(xStart, yStart) };
- _paths.Add(_currentPath);
- }
- Point lastPoint = _currentPath[_currentPath.Count - 1];
- int x = lastPoint.X + rnd.Next(-20, 21);
- int y = lastPoint.Y + rnd.Next(-20, 21);
- if (x < pictureBox1.Location.X) x = pictureBox1.Location.X;
- if (x > pictureBox1.Location.X + pictureBox1.Width) x = pictureBox1.Location.X + pictureBox1.Width;
- if (y < pictureBox1.Location.Y) y = pictureBox1.Location.Y;
- if (y > pictureBox1.Location.Y + pictureBox1.Height) y = pictureBox1.Location.Y + pictureBox1.Height;
- Point randomPoint = new Point(x, y);
- _currentPath.Add(randomPoint);
- // Simulate random "mouse up" and "mouse down" events
- if (rnd.NextDouble() < 0.05) // 5% chance to lift the "pen"
- {
- _currentPath = new List<Point>(); // Lift the pen
- _paths.Add(_currentPath); // Start a new path
- }
- pictureBox1.Invalidate();
- }
- private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (clearPoints)
- {
- _paths.Clear(); // Clear previous paths
- }
- if (_currentPath == null || _currentPath.Count == 0 || connectDrawToLastPoint)
- {
- if (_currentPath == null || _currentPath.Count == 0)
- {
- _currentPath = new List<Point>();
- _paths.Add(_currentPath);
- }
- AddPointAndInvalidate(e.Location, true); // Add the starting point and invalidate
- }
- else
- {
- // Start a new path without connecting to the last point
- _currentPath = new List<Point> { e.Location };
- _paths.Add(_currentPath); // Add the new path to the list of paths
- pictureBox1.Invalidate(); // Force a repaint
- }
- draw = true;
- }
- }
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- Pen pen = new Pen(Color.Red, 5);
- foreach (var path in _paths)
- {
- if (path.Count > 1)
- {
- e.Graphics.DrawLines(pen, path.ToArray());
- }
- }
- }
- private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
- {
- draw = false;
- pictureBox1.Invalidate(); // Ensure the points are drawn
- }
- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- if (draw && e.Button == MouseButtons.Left)
- {
- AddPointAndInvalidate(e.Location, true); // Add point and invalidate
- }
- if (drawOnlyOnMove)
- {
- AddPointAndInvalidate(e.Location, true); // Add point and invalidate
- }
- }
- private void AddPointAndInvalidate(Point point, bool invalidate)
- {
- if (_currentPath == null)
- {
- _currentPath = new List<Point>();
- _paths.Add(_currentPath);
- }
- _currentPath.Add(point);
- if (invalidate)
- {
- pictureBox1.Invalidate(); // Force a repaint
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment