.NET 4.x Обработка нажатия кнопки мыши в зависимости от условия - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Здраствуйте! У меня есть проект, в котором я использую сетку. Мне нужно рисовать точки только места, где оси пересекаются.Эти точки рисоват с помощью мыши.Мышь не следует делать в пустые коробки! Как запретить мыши не рисует в пустых коробках? Пожалуйста, если кто-нибудь может мне помочь.Простите меня для плохой русский язык!
Листинг программы
  1. Private Sub picTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseDown
  2.  
  3. Dim hstep As Double = picTest.Width / 12
  4. Dim vstep As Double = picTest.Height / 12
  5. Dim myPen As New Pen(Color.Red, 3)
  6. Dim g As Graphics = Graphics.FromImage(gBitmap)
  7. picAG.Refresh()
  8. If e.Button = System.Windows.Forms.MouseButtons.Left Then
  9. points.Add(e.Location) ' add point on left click
  10. For i As Integer = 0 To points.Count - 1
  11. g.DrawRectangle(myPen, points(i).X - 2, points(i).Y - 2, 5, 5)
  12. Next
  13.  
  14. End If
  15.  
  16. If (NewPolygon IsNot Nothing) Then
  17. If (e.Button = MouseButtons.Right) Then
  18. gBitmap = Nothing
  19. ' Finish this polygon.
  20. If (NewPolygon.Count > 0) Then Polygons.Add(NewPolygon)
  21. NewPolygon = Nothing
  22. picTest.Image = gBitmap
  23. Else
  24. ' Add a point to this polygon.
  25. If (NewPolygon(NewPolygon.Count - 1) <> e.Location) Then
  26. Dim i As Integer
  27. For i = 0 To 12
  28. NewPolygon.Add(New Point(e.X, e.Y )'Here I think to use'hstep,vstep and i',but I don`t know how to do ?!
  29. picTest.Image = gBitmap
  30. Next i
  31. End If
  32. End If
  33. Else
  34. ' Start a new polygon.
  35. NewPolygon = New List(Of Point)()
  36. NewPolygon.Add(e.Location)
  37. NewPoint = (e.Location)
  38. End If
  39.  
  40. ' Redraw.
  41. picTest.Invalidate()
  42. 'Dispose of objects
  43. myPen.Dispose()
  44. g.Dispose()
  45. Private Sub picTest_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseMove
  46. If (NewPolygon Is Nothing) Then Exit Sub
  47. NewPoint = e.Location
  48. ' Redraw.
  49. picTest.Invalidate()
  50. End Sub
  51. Private Sub picTest_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picTest.Paint
  52. Dim g As Graphics = e.Graphics
  53. Dim myPen As New Pen(Color.Red, 2)
  54. If (NewPolygon IsNot Nothing) Then
  55. ' Draw the new polygon.
  56. If (NewPolygon.Count > 1) Then
  57. e.Graphics.DrawLines(myPen, NewPolygon.ToArray())
  58.  
  59. For i As Integer = 0 To points.Count - 1
  60. e.Graphics.FillEllipse(Brushes.Green, points(i).X - 2, points(i).Y - 2, 5, 5)
  61. Next
  62. End If
  63. End If
  64.  
  65. Dim hstep As Double = picTest.Width / 12
  66. Dim vstep As Double = picTest.Height / 12
  67. 'Draw horizontal Line
  68. Dim X As Single = hstep
  69. For i As Integer = 0 To hstep
  70. g.DrawLine(New Pen(Color.Black, 2), New Point(X, 0), New Point(X, Height))
  71. X += hstep
  72. Next i
  73. Dim Y As Single = vstep
  74. For i As Integer = 0 To vstep
  75. 'Draw Vertical Line
  76. g.DrawLine(New Pen(Color.Black, 2), New Point(0, Y), New Point(Width, Y))
  77. Y += vstep
  78. Next i
  79. End Sub

Решение задачи: «.NET 4.x Обработка нажатия кнопки мыши в зависимости от условия»

textual
Листинг программы
  1. Private Sub Form6_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  2. '…
  3.     PictureBox1.Cursor = Cursors.Hand
  4. End Sub
  5. Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  6.     If e.Button = Windows.Forms.MouseButtons.Left Then
  7.         Dim x As Integer = (Math.Round(e.X / sd)) * sd
  8.         Dim y As Integer = (Math.Round(e.Y / sd)) * sd
  9.         If points.Count > 0 Then
  10.             If x < points(points.Count - 1).X Then
  11.                 isDown = False
  12.                 Exit Sub
  13.             ElseIf x = points(points.Count - 1).X Then
  14.                 points.RemoveAt(points.Count - 1)
  15.             End If
  16.         End If
  17.         points.Add(New Point(x, y))
  18.         isDown = True
  19.     End If
  20.     PictureBox1.Invalidate()
  21. End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.167 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы