【问题标题】:Click event for ellipse in vb.net 2010vb.net 2010中椭圆的单击事件
【发布时间】:2013-01-15 05:55:02
【问题描述】:

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

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

【问题讨论】:

  • 您是如何创建椭圆的,您使用的是 Winforms、Wpf 还是 ?
  • 您可以使用面积公式检查给定点是否落在椭圆内:mouse_x * mouse_y <= A * B [假设中心为 (0, 0)],其中 A 和 B 是椭圆的半径。 Area of an Ellipse

标签: vb.net graphics ellipse


【解决方案1】:

这是一个示例应用程序,演示了如何使用面积来计算一个点是否落在椭圆内:

注意:创建一个新的 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

顺便说一句,我在这里找到了正确的公式:Check if a point is within an ellipse @ Mathematics

【讨论】:

  • tnx 很多...你的救命稻草...是时候通过navicat将此单击按钮连接到mysql了.....
  • 很高兴为您提供帮助@ReysterRautCarba
猜你喜欢
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
相关资源
最近更新 更多