Advertisement
Guest User

GDI draw method

a guest
Feb 10th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3.  
  4. /*
  5. Created by: CodeAssassin
  6. Date: 10/02/2016
  7. Use: To create a simple draw mechanics example using GDI and a panel
  8. */
  9.  
  10. namespace SimpleDraw_Panel
  11. {
  12. internal class SimpleDraw
  13. {
  14. private Panel _p;
  15. private bool MouseIsDown = false;
  16. private Graphics G;
  17.  
  18. public SimpleDraw(Panel p)
  19. {
  20. if (p != null)
  21. {
  22. _p = p;
  23. _p.MouseDown += _p_MouseDown;
  24. _p.MouseUp += _p_MouseUp;
  25. _p.MouseMove += _p_MouseMove;
  26. Clear();
  27. }
  28. }
  29.  
  30. public void Clear()
  31. {
  32. if (_p != null)
  33. {
  34. G = _p.CreateGraphics();
  35. G.Clear(Color.White);
  36. G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  37. G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  38. G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  39. _p.Invalidate();
  40. }
  41. }
  42.  
  43. private void Draw(MouseEventArgs e)
  44. {
  45. if (G != null)
  46. {
  47. G.DrawRectangle(Pens.Black, new Rectangle(e.X, e.Y, 1, 1));
  48. }
  49. }
  50.  
  51. private void _p_MouseMove(object sender, MouseEventArgs e)
  52. {
  53. if (MouseIsDown)
  54. {
  55. Draw(e);
  56. }
  57. }
  58.  
  59. private void _p_MouseUp(object sender, MouseEventArgs e)
  60. {
  61. MouseIsDown = false;
  62. }
  63.  
  64. private void _p_MouseDown(object sender, MouseEventArgs e)
  65. {
  66. Draw(e);
  67. MouseIsDown = true;
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement