首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在用户单击地图时绘制点

如何在用户单击地图时绘制点
EN

Stack Overflow用户
提问于 2014-02-25 16:11:18
回答 1查看 839关注 0票数 1
代码语言:javascript
复制
    Dim HaveToDraw As New Boolean
    Dim xMouse As Integer
    Dim yMouse As Integer

    Private Sub foo(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        If HaveToDraw = True Then
            e.Graphics.FillEllipse(Brushes.Green, xMouse, yMouse, 10, 10)
        End If
        HaveToDraw = False
    End Sub

    Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
        If RadioButton1.Checked = True Then
            xMouse = e.X
            yMouse = e.Y
            HaveToDraw = True
            PictureBox1.Refresh()
        End If
    End Sub

这段代码允许用户在地图上的任意一点上单击时绘制一个启示录,但其中有两个问题:1-用户只能绘制一种启示录;2-用户不能删除先前创建的启示录。

那么,我怎样才能解决这两个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-25 17:39:34

正如@Idle_Mind建议的那样,您可以使用一个列表来存储您的点,使用一个right-click事件来删除这些点:

代码语言:javascript
复制
Dim radius as Integer = 5
Private points As New List(Of Point)()
Private Sub pictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
    If e.Button = System.Windows.Forms.MouseButtons.Left Then
        points.Add(e.Location) ' add point on left click
    ElseIf e.Button = System.Windows.Forms.MouseButtons.Right Then 
        For i As Integer = 0 To points.Count - 1 ' remove points on right-click
            If distance(points(i).X, points(i).Y, e.Location) < radius Then
                points.RemoveAt(i)
            End If
        Next
    End If
    pictureBox1.Refresh()
End Sub


'helper function
Private Function distance(x__1 As Integer, y__2 As Integer, mousep As Point) As Integer
    Dim X__3 As Integer = CInt(Math.Pow(CDbl(x__1 - mousep.X), 2))
    Dim Y__4 As Integer = CInt(Math.Pow(CDbl(y__2 - mousep.Y), 2))

    Return CInt(Math.Sqrt(CDbl(X__3 + Y__4)))
End Function

Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs)
    For i As Integer = 0 To points.Count - 1
        e.Graphics.FillEllipse(Brushes.Green, points(i).X - radius, points(i).Y - radius, radius * 2, radius * 2)
    Next
End Sub

我还更改了油漆代码,以绘制圆圈,使它们在鼠标点击下居中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22019836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档