.NET 4.x Обработка нажатия кнопки мыши в зависимости от условия - Visual Basic .NET
Формулировка задачи:
Здраствуйте!
У меня есть проект, в котором я использую сетку.
Мне нужно рисовать точки только места,
где оси пересекаются.Эти точки рисоват с помощью мыши.Мышь не следует делать в пустые коробки!
Как запретить мыши не рисует в пустых коробках?
Пожалуйста, если кто-нибудь может мне помочь.Простите меня для плохой русский язык!
Листинг программы
- Private Sub picTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseDown
- Dim hstep As Double = picTest.Width / 12
- Dim vstep As Double = picTest.Height / 12
- Dim myPen As New Pen(Color.Red, 3)
- Dim g As Graphics = Graphics.FromImage(gBitmap)
- picAG.Refresh()
- If e.Button = System.Windows.Forms.MouseButtons.Left Then
- points.Add(e.Location) ' add point on left click
- For i As Integer = 0 To points.Count - 1
- g.DrawRectangle(myPen, points(i).X - 2, points(i).Y - 2, 5, 5)
- Next
- End If
- If (NewPolygon IsNot Nothing) Then
- If (e.Button = MouseButtons.Right) Then
- gBitmap = Nothing
- ' Finish this polygon.
- If (NewPolygon.Count > 0) Then Polygons.Add(NewPolygon)
- NewPolygon = Nothing
- picTest.Image = gBitmap
- Else
- ' Add a point to this polygon.
- If (NewPolygon(NewPolygon.Count - 1) <> e.Location) Then
- Dim i As Integer
- For i = 0 To 12
- 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 ?!
- picTest.Image = gBitmap
- Next i
- End If
- End If
- Else
- ' Start a new polygon.
- NewPolygon = New List(Of Point)()
- NewPolygon.Add(e.Location)
- NewPoint = (e.Location)
- End If
- ' Redraw.
- picTest.Invalidate()
- 'Dispose of objects
- myPen.Dispose()
- g.Dispose()
- Private Sub picTest_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseMove
- If (NewPolygon Is Nothing) Then Exit Sub
- NewPoint = e.Location
- ' Redraw.
- picTest.Invalidate()
- End Sub
- Private Sub picTest_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picTest.Paint
- Dim g As Graphics = e.Graphics
- Dim myPen As New Pen(Color.Red, 2)
- If (NewPolygon IsNot Nothing) Then
- ' Draw the new polygon.
- If (NewPolygon.Count > 1) Then
- e.Graphics.DrawLines(myPen, NewPolygon.ToArray())
- For i As Integer = 0 To points.Count - 1
- e.Graphics.FillEllipse(Brushes.Green, points(i).X - 2, points(i).Y - 2, 5, 5)
- Next
- End If
- End If
- Dim hstep As Double = picTest.Width / 12
- Dim vstep As Double = picTest.Height / 12
- 'Draw horizontal Line
- Dim X As Single = hstep
- For i As Integer = 0 To hstep
- g.DrawLine(New Pen(Color.Black, 2), New Point(X, 0), New Point(X, Height))
- X += hstep
- Next i
- Dim Y As Single = vstep
- For i As Integer = 0 To vstep
- 'Draw Vertical Line
- g.DrawLine(New Pen(Color.Black, 2), New Point(0, Y), New Point(Width, Y))
- Y += vstep
- Next i
- End Sub
Решение задачи: «.NET 4.x Обработка нажатия кнопки мыши в зависимости от условия»
textual
Листинг программы
- Private Sub Form6_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- '…
- PictureBox1.Cursor = Cursors.Hand
- End Sub
- Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
- If e.Button = Windows.Forms.MouseButtons.Left Then
- Dim x As Integer = (Math.Round(e.X / sd)) * sd
- Dim y As Integer = (Math.Round(e.Y / sd)) * sd
- If points.Count > 0 Then
- If x < points(points.Count - 1).X Then
- isDown = False
- Exit Sub
- ElseIf x = points(points.Count - 1).X Then
- points.RemoveAt(points.Count - 1)
- End If
- End If
- points.Add(New Point(x, y))
- isDown = True
- End If
- PictureBox1.Invalidate()
- End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д