Chocolade19721309

Untitled

Jun 18th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace Mouse_Test
  7. {
  8. public partial class Form1 : Form
  9. {
  10. private bool draw = false;
  11. private bool clearPoints = false;
  12. private bool drawOnlyOnMove = false;
  13. private bool connectDrawToLastPoint = false;
  14. private bool drawAutomatic = true;
  15. private Timer timer = new Timer();
  16. private IList<List<Point>> _paths = new List<List<Point>>();
  17. private List<Point> _currentPath = null; // Initialize to null
  18. private Random rnd = new Random();
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. this.DoubleBuffered = true;
  24.  
  25. timer.Interval = 10; // Set interval for smoother drawing
  26. timer.Tick += Timer_Tick;
  27.  
  28. if (drawAutomatic)
  29. {
  30. _currentPath = new List<Point>();
  31. _paths.Add(_currentPath); // Initialize the path list
  32. timer.Start();
  33. }
  34. }
  35.  
  36. private void Timer_Tick(object sender, EventArgs e)
  37. {
  38. if (_currentPath == null || _currentPath.Count == 0)
  39. {
  40. int xStart = rnd.Next(pictureBox1.Location.X, pictureBox1.Location.X + pictureBox1.Width);
  41. int yStart = rnd.Next(pictureBox1.Location.Y, pictureBox1.Location.Y + pictureBox1.Height);
  42. _currentPath = new List<Point> { new Point(xStart, yStart) };
  43. _paths.Add(_currentPath);
  44. }
  45.  
  46. Point lastPoint = _currentPath[_currentPath.Count - 1];
  47. int x = lastPoint.X + rnd.Next(-20, 21);
  48. int y = lastPoint.Y + rnd.Next(-20, 21);
  49.  
  50. if (x < pictureBox1.Location.X) x = pictureBox1.Location.X;
  51. if (x > pictureBox1.Location.X + pictureBox1.Width) x = pictureBox1.Location.X + pictureBox1.Width;
  52. if (y < pictureBox1.Location.Y) y = pictureBox1.Location.Y;
  53. if (y > pictureBox1.Location.Y + pictureBox1.Height) y = pictureBox1.Location.Y + pictureBox1.Height;
  54.  
  55. Point randomPoint = new Point(x, y);
  56. _currentPath.Add(randomPoint);
  57.  
  58. // Simulate random "mouse up" and "mouse down" events
  59. if (rnd.NextDouble() < 0.05) // 5% chance to lift the "pen"
  60. {
  61. _currentPath = new List<Point>(); // Lift the pen
  62. _paths.Add(_currentPath); // Start a new path
  63. }
  64.  
  65. pictureBox1.Invalidate();
  66. }
  67.  
  68. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  69. {
  70. if (e.Button == MouseButtons.Left)
  71. {
  72. if (clearPoints)
  73. {
  74. _paths.Clear(); // Clear previous paths
  75. }
  76.  
  77. if (_currentPath == null || _currentPath.Count == 0 || connectDrawToLastPoint)
  78. {
  79. if (_currentPath == null || _currentPath.Count == 0)
  80. {
  81. _currentPath = new List<Point>();
  82. _paths.Add(_currentPath);
  83. }
  84. AddPointAndInvalidate(e.Location, true); // Add the starting point and invalidate
  85. }
  86. else
  87. {
  88. // Start a new path without connecting to the last point
  89. _currentPath = new List<Point> { e.Location };
  90. _paths.Add(_currentPath); // Add the new path to the list of paths
  91. pictureBox1.Invalidate(); // Force a repaint
  92. }
  93.  
  94. draw = true;
  95. }
  96. }
  97.  
  98. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  99. {
  100. Pen pen = new Pen(Color.Red, 5);
  101. foreach (var path in _paths)
  102. {
  103. if (path.Count > 1)
  104. {
  105. e.Graphics.DrawLines(pen, path.ToArray());
  106. }
  107. }
  108. }
  109.  
  110. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  111. {
  112. draw = false;
  113. pictureBox1.Invalidate(); // Ensure the points are drawn
  114. }
  115.  
  116. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  117. {
  118. if (draw && e.Button == MouseButtons.Left)
  119. {
  120. AddPointAndInvalidate(e.Location, true); // Add point and invalidate
  121. }
  122.  
  123. if (drawOnlyOnMove)
  124. {
  125. AddPointAndInvalidate(e.Location, true); // Add point and invalidate
  126. }
  127. }
  128.  
  129. private void AddPointAndInvalidate(Point point, bool invalidate)
  130. {
  131. if (_currentPath == null)
  132. {
  133. _currentPath = new List<Point>();
  134. _paths.Add(_currentPath);
  135. }
  136.  
  137. _currentPath.Add(point);
  138. if (invalidate)
  139. {
  140. pictureBox1.Invalidate(); // Force a repaint
  141. }
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment