0

我在 vb.net 的图片框中创建一个椭圆,我希望如果我点击这个椭圆,它将打开另一个表单。图片框包含一张地图的图像,我只是简单地在地图中找到的建筑物上画了一个椭圆,我猜你知道它会向我显示有关该建筑物的信息......

注意:如果您有更简单的方法在 vb.net 中创建交互式地图,其中有关地图的信息和图像存储在 mysql 数据库中。

4

1 回答 1

3

这是一个示例应用程序,演示了使用该区域来计算一个点是否落在椭圆内:

注意:创建一个新的 WinForms 应用程序并将代码粘贴到Form1.vb

Public Class Form1
    Private ellipse_center As Point

    Private A As Integer = 140
    Private B As Integer = 90

    Private isInsideEllipse As Boolean

    Private canvasRect As Rectangle

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)

        Me.Font = New Font(Me.Font.FontFamily, 12, FontStyle.Bold)
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        isInsideEllipse = (e.Location.X - ellipse_center.X) ^ 2 / (A / 2) ^ 2 + (e.Location.Y - ellipse_center.Y) ^ 2 / (B / 2) ^ 2 <= 1
        Me.Invalidate()
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics

        Using p As New Pen(If(isInsideEllipse, Brushes.Blue, Brushes.Red), 2)
            g.DrawEllipse(p, ellipse_center.X - A \ 2, ellipse_center.Y - B \ 2, A, B)
        End Using

        g.DrawString("Pointer is" + If(isInsideEllipse, " ", " not ") + "inside the ellipse", Me.Font, Brushes.Black, 5, 5)
    End Sub

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        canvasRect = Me.DisplayRectangle
        canvasRect.Inflate(-1, -1)

        ellipse_center = New Point(canvasRect.Width / 2, canvasRect.Height / 2)
    End Sub
End Class

顺便说一句,我在这里找到了正确的公式:检查点是否在椭圆内@数学

于 2013-01-15T13:35:45.817 回答