VB缩小坐标

时间:2015-12-01 22:02:18

标签: vb.net line gdi+ contains

我需要知道鼠标点击gdi +绘制线的时间。

我已经设计了这个函数,它在所有现有行的循环中使用,函数的作用是:

  1. 它构成了行容器大小的缓冲区
  2. 它使整个事情变黑
  3. 以绿色绘制线条
  4. 它获取鼠标位置的像素
  5. 如果像素与黑色a.k.a绿色不同,则已成功点击该行,然后该函数将返回true。
  6. 这很好用,没有误解,但是当我的表格处于全屏状态时(由于缓冲区很大),我担心会有一点延迟(不是很明显)。

    我正在寻找一种优化方法,我的第一个想法是缩小所有内容。所以我的意思是将缓冲区设置为20x20,然后使用数学在缩小版本中绘制线条。问题是,我吮吸数学,所以我基本上问你如何做到这一点,最好是对傻瓜的解释。

    这是功能:

    Public Function Contains(ByVal e As Point) As Boolean
        Dim Width As Integer = Container.Size.Width
        Dim Height As Integer = Container.Size.Height
    
        Dim Buffer As Bitmap = New Bitmap(Width, Height)
        Using G As Graphics = Graphics.FromImage(Buffer)
            G.Clear(Color.Black)
    
            Dim Start As Point = New Point(ParentNode.Location.X + ParentNode.Size.Width / 2, ParentNode.Location.Y + ParentNode.Size.Height / 2)
            Dim [End] As Point = New Point(ChildNode.Location.X + ChildNode.Size.Width / 2, ChildNode.Location.Y + ChildNode.Size.Height / 2)
    
            Dim Control1 As Point
            Dim Control2 As Point
            Control1.X = Start.X + GetAngle(ChildNode.Location, ParentNode.Location, ChildNode.Location.X - ParentNode.Location.X, ChildNode.Location.Y - ParentNode.Location.Y)
            Control1.Y = Start.Y
    
            Control2.X = [End].X
            Control2.Y = Start.Y
    
            G.DrawBezier(New Pen(Color.Green, 4), Start, Control1, Control2, [End])
        End Using
    
        If Buffer.GetPixel(e.X, e.Y).ToArgb() <> Color.Black.ToArgb() Then
            Return True
        End If
    
        Return False
    End Function
    

    这是我试图使该功能使用上述想法之一:

    Public Function Contains(ByVal e As Point) As Boolean
        Dim Width As Integer = 20
        Dim Height As Integer = 20
    
        Dim Buffer As Bitmap = New Bitmap(Width, Height)
        Using G As Graphics = Graphics.FromImage(Buffer)
        G.Clear(Color.Black)
    
        Dim Start As Point = New Point(ParentNode.Location.X + ParentNode.Size.Width / 2, ParentNode.Location.Y + ParentNode.Size.Height / 2)
        Dim [End] As Point = New Point(ChildNode.Location.X + ChildNode.Size.Width / 2, ChildNode.Location.Y + ChildNode.Size.Height / 2)
    
        Dim Control1 As Point
        Dim Control2 As Point
        Control1.X = Start.X + GetAngle(ChildNode.Location, ParentNode.Location, ChildNode.Location.X - ParentNode.Location.X, ChildNode.Location.Y - ParentNode.Location.Y)
        Control1.Y = Start.Y
    
        Control2.X = [End].X
        Control2.Y = Start.Y
    
        G.DrawBezier(New Pen(Color.Green, 4), New Point(Start.X / Width, Start.Y / Height), New Point(Control1.X / Width, Control1.Height / Height), New Point(Control2.X / Width, Control2.Y / Height), New Point([End].X / Width, [End].Y / Height))
    End Using
    
    If Buffer.GetPixel(Width, Height).ToArgb() <> Color.Black.ToArgb() Then
        Return True
    End If
    
    Return False
    End Function
    

1 个答案:

答案 0 :(得分:0)

尝试使用GraphicsPath进行内置IsOutlineVisible功能的绘图和测试:

Public Function Contains(ByVal e As Point) As Boolean
  Dim result as Boolean = False
  Using gp As New GraphicsPath
    gp.AddBezier(your four points)
    Using p As New Pen(Color.Empty, 4)
      result = gp.IsOutlineVisible(e, p)
    End Using
  End Using
  Return result
End Function

附注:创建位图时需要处理位图和图形对象。