Advertisement
Ang377ou

paint

Mar 23rd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace PicLoaderE1
  11. {
  12. public partial class paint : UserControl
  13. {
  14. private bool Brush = true; //Uses either Brush or Eraser. Default is Brush
  15. public Shapes DrawingShapes = new Shapes(); //Stores all the drawing data
  16. private bool IsPainting = false; //Is the mouse currently down (PAINTING)
  17. private bool IsEraseing = false; //Is the mouse currently down (ERASEING)
  18. private Point LastPos = new Point(0, 0); //Last Position, used to cut down on repative data.
  19. private Color CurrentColour = Color.Black; //Deafult Colour
  20. private float CurrentWidth = 10; //Deafult Pen width
  21. private int ShapeNum = 0; //record the shapes so they can be drawn sepratley.
  22. private Point MouseLoc = new Point(0, 0); //Record the mouse position
  23. private bool IsMouseing = false;
  24. private Color BackgroundColor;
  25.  
  26. public Color BackgroundColor1
  27. {
  28. get { return BackgroundColor; }
  29. set { BackgroundColor = value; }
  30. }
  31.  
  32. public paint()
  33. {
  34. InitializeComponent();
  35. panel1.GetType().GetMethod("SetStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(panel1, new object[] { System.Windows.Forms.ControlStyles.UserPaint | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.DoubleBuffer, true });
  36. panel1.BackColor = BackgroundColor1;
  37. }
  38.  
  39. private void panel1_MouseDown(object sender, MouseEventArgs e)
  40. {
  41. //If we're painting...
  42. if (Brush)
  43. {
  44. //set it to mouse down, illatrate the shape being drawn and reset the last position
  45. IsPainting = true;
  46. ShapeNum++;
  47. LastPos = new Point(0, 0);
  48. }
  49. //but if we're eraseing...
  50. else
  51. {
  52. IsEraseing = true;
  53. }
  54. }
  55.  
  56. protected void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  57. {
  58. MouseLoc = e.Location;
  59. //PAINTING
  60. if (IsPainting)
  61. {
  62. //check its not at the same place it was last time, saves on recording more data.
  63. if (LastPos != e.Location)
  64. {
  65. //set this position as the last positon
  66. LastPos = e.Location;
  67. //store the position, width, colour and shape relation data
  68. DrawingShapes.NewShape(LastPos, CurrentWidth, CurrentColour, ShapeNum);
  69. }
  70. }
  71. if (IsEraseing)
  72. {
  73. //Remove any point within a certain distance of the mouse
  74. DrawingShapes.RemoveShape(e.Location, 10);
  75. }
  76. //refresh the panel so it will be forced to re-draw.
  77. panel1.Refresh();
  78. }
  79.  
  80. private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  81. {
  82. if (IsPainting)
  83. {
  84. //Finished Painting.
  85. IsPainting = false;
  86. }
  87. if (IsEraseing)
  88. {
  89. //Finished Earsing.
  90. IsEraseing = false;
  91. }
  92. }
  93.  
  94. //DRAWING FUNCTION
  95. private void panel1_Paint(object sender, PaintEventArgs e)
  96. {
  97. //Apply a smoothing mode to smooth out the line.
  98. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  99. //DRAW THE LINES
  100. for (int i = 0; i < DrawingShapes.NumberOfShapes() - 1; i++)
  101. {
  102. Shape T = DrawingShapes.GetShape(i);
  103. Shape T1 = DrawingShapes.GetShape(i + 1);
  104. //make sure shape the two ajoining shape numbers are part of the same shape
  105. if (T.ShapeNumber == T1.ShapeNumber)
  106. {
  107. //create a new pen with its width and colour
  108. Pen p = new Pen(T.Colour, T.Width);
  109. p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
  110. p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
  111. //draw a line between the two ajoining points
  112. e.Graphics.DrawLine(p, T.Location, T1.Location);
  113. //get rid of the pen when finished
  114. p.Dispose();
  115. }
  116. }
  117. //If mouse is on the panel, draw the mouse
  118. if (IsMouseing)
  119. {
  120. e.Graphics.DrawEllipse(new Pen(Color.White, 0.5f), MouseLoc.X - (CurrentWidth / 2), MouseLoc.Y - (CurrentWidth / 2), CurrentWidth, CurrentWidth);
  121. }
  122. }
  123.  
  124. private void btnClear_Click(object sender, EventArgs e)
  125. {
  126. //Reset the list, removeing all shapes.
  127. DrawingShapes = new Shapes();
  128. panel1.Refresh();
  129. }
  130.  
  131. private void btnEraseBrush_Click(object sender, EventArgs e)
  132. {
  133.  
  134. }
  135.  
  136. private void btnPenColor_Click(object sender, EventArgs e)
  137. {
  138. ////Show and Get the result of the colour dialog
  139. //DialogResult D = colorDialog1.ShowDialog();
  140. //if (D == DialogResult.OK)
  141. //{
  142. // //Apply the new colour
  143. // CurrentColour = colorDialog1.Color;
  144. //}
  145. }
  146.  
  147. private void panel1_MouseEnter(object sender, EventArgs e)
  148. {
  149. //Hide the mouse cursor and tell the re-drawing function to draw the mouse
  150. Cursor.Hide();
  151. IsMouseing = true;
  152. }
  153.  
  154. private void panel1_MouseLeave(object sender, EventArgs e)
  155. {
  156. //show the mouse, tell the re-drawing function to stop drawing it and force the panel to re-draw.
  157. Cursor.Show();
  158. IsMouseing = false;
  159. panel1.Refresh();
  160. }
  161. }
  162.  
  163. public class Shape
  164. {
  165. public Point Location; //position of the point
  166. public float Width; //width of the line
  167. public Color Colour; //colour of the line
  168. public int ShapeNumber; //part of which shape it belongs to
  169.  
  170. //CONSTRUCTOR
  171. public Shape(Point L, float W, Color C, int S)
  172. {
  173. Location = L; //Stores the Location
  174. Width = W; //Stores the width
  175. Colour = C; //Stores the colour
  176. ShapeNumber = S; //Stores the shape number
  177. }
  178. }
  179. public class Shapes
  180. {
  181. private List<Shape> _Shapes; //Stores all the shapes
  182.  
  183. public Shapes()
  184. {
  185. _Shapes = new List<Shape>();
  186. }
  187. //Returns the number of shapes being stored.
  188. public int NumberOfShapes()
  189. {
  190. return _Shapes.Count;
  191. }
  192. //Add a shape to the database, recording its position, width, colour and shape relation information
  193. public void NewShape(Point L, float W, Color C, int S)
  194. {
  195. _Shapes.Add(new Shape(L, W, C, S));
  196. }
  197. //returns a shape of the requested data.
  198. public Shape GetShape(int Index)
  199. {
  200. return _Shapes[Index];
  201. }
  202. //Removes any point data within a certain threshold of a point.
  203. public void RemoveShape(Point L, float threshold)
  204. {
  205. for (int i = 0; i < _Shapes.Count; i++)
  206. {
  207. //Finds if a point is within a certain distance of the point to remove.
  208. if ((Math.Abs(L.X - _Shapes[i].Location.X) < threshold) && (Math.Abs(L.Y - _Shapes[i].Location.Y) < threshold))
  209. {
  210. //removes all data for that number
  211. _Shapes.RemoveAt(i);
  212.  
  213. //goes through the rest of the data and adds an extra 1 to defined them as a seprate shape and shuffles on the effect.
  214. for (int n = i; n < _Shapes.Count; n++)
  215. {
  216. _Shapes[n].ShapeNumber += 1;
  217. }
  218. //Go back a step so we dont miss a point.
  219. i -= 1;
  220. }
  221. }
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement