The lack of XOR Drawing feature in GDI+ was not certainly welcome in the programmer's community. I guess it will be hard to survive with this handicap.

In spite of this, I would like to show how we can draw rubber-band lines and shapes in GDI+ with just a few lines of code.

RubberBandShapesImg1.jpg

  1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  2. Dim size As Size = SystemInformation.PrimaryMonitorMaximizedWindowSize
  3. bitmap = New Bitmap(size.Width, size.Height)
  4. gB = Graphics.FromImage(bitmap)
  5. Dim bckColor As Color = Me.BackColor
  6. gB.Clear(bckColor)
  7. End Sub
  8. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs)
  9. Dim p As Point = New Point(e.X, e.Y)
  10. x0 = p.X
  11. y0 = p.Y
  12. drag = True
  13. End Sub
  14. Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs)
  15. Dim p As Point = New Point(e.X, e.Y)
  16. x= p.X
  17. y = p.Y
  18. Dim cx As Integer = x - x0
  19. Dim cy As Integer = y - y0
  20. If drag Then
  21. Dim gx As Graphics = CreateGraphics()
  22. gx.DrawImage(bitmap, 0, 0)
  23. gx.Dispose()
  24. Dim g As Graphics = CreateGraphics()
  25. Dim pen As Pen = New Pen(Color.Blue)
  26. Select Case DrawMode
  27. Case 1
  28. g.DrawLine(pen, x0, y0, x, y)
  29. Exit Select
  30. Case 2
  31. g.DrawEllipse(pen, x0, y0, cx, cy)
  32. Exit Select
  33. Case 3
  34. g.DrawRectangle(pen, x0, y0, cx, cy)
  35. Exit Select
  36. End Select
  37. g.Dispose()
  38. pen.Dispose()
  39. End If
  40. End Sub
  41. Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs)
  42. Dim cx As Integer = x - x0
  43. Dim cy As Integer = y - y0
  44. Dim pen As Pen = New Pen(Color.Blue)
  45. Select Case DrawMode
  46. Case 1
  47. gB.DrawLine(pen, x0, y0, x, y)
  48. Exit Select
  49. Case 2
  50. gB.DrawEllipse(pen, x0, y0, cx, cy)
  51. Exit Select
  52. Case 3
  53. gB.DrawRectangle(pen, x0, y0, cx, cy)
  54. Exit Select
  55. End Select
  56. drag = False
  57. pen.Dispose()
  58. End Sub
  59. Private Sub Form1_Paint(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.PaintEventArgs)
  60. Dim gx As Graphics = CreateGraphics()
  61. gx.DrawImage(bitmap, 0, 0)
  62. gx.Dispose()
  63. End Sub
  64. Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)button1.ForeColor = Color.Red
  65. button2.ForeColor = Color.Black
  66. button3.ForeColor = Color.Black
  67. DrawMode = 1
  68. End Sub
  69. Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  70. button2.ForeColor = Color.Red
  71. button1.ForeColor = Color.Black
  72. button3.ForeColor = Color.Black
  73. DrawMode = 2
  74. End Sub
  75. Private Sub button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  76. button3.ForeColor = Color.Red
  77. button1.ForeColor = Color.Black
  78. button2.ForeColor = Color.Black
  79. DrawMode = 3
  80. End Sub
  81. Private Sub panel1_Paint(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.PaintEventArgs)
  82. button1.ForeColor = Color.Red
  83. button1.Focus()
  84. End Sub