2014-07-24 20 views

回答

0

我覺得你需要的是這樣的: -

If picturebox1.SelectionLength > 0 Then 
    Dim firstLine As Integer = picturebox1.GetLineFromCharIndex(picturebox1.SelectionStart) 
    Dim lastLine As Integer = picturebox1.GetLineFromCharIndex(picturebox1.SelectionStart + picturebox1.SelectionLength) 
    For line As Integer = firstLine To lastLine 
     Dim txt = picturebox1.Lines(line) 
     'do something here or run some command as u want... 
    Next 
End If 
0

我想你想要的是一個方法來確定是否被點擊或不是一些行。

一條線由兩點定義。直線由斜率(m)和截距(b)定義。 首先從兩點(也就是經過兩點的直線)得到線性方程。

將兩個點插入線性方程。你可以得到兩個線性方程。

y1 = m * x1 + b 
y2 = m * x2 + b 

解決b和設置相等。

y1 - m * x1 = y2 - m * x2 

求解m

--> m = -(y1 - y2)/(x1 - x2) 

插入m進入第一方程和解決b

--> b = y1 - m * x1 

現在就可以了,對於給定的X coordiante,例如鼠標點擊的x座標,確定哪個y值您的線路具有在此位置:

Dim m, b As Double 
GetSlopeIntercept(p1, p2, m, b) 'See above 

Dim YLine as Double = m * MouseLocation.X + b 
If Math.Abs(MouseLocation.Y - YLine) < 5 AndAlso _ 
    MouseLocation.X > Math.Min(p1.X, p2.X) AndAlso _ 
    MouseLocation.X < Math.Max(p1.X, p2.X) _ 
    Then MessageBox.Show("Line was clicked!") 

在這個例子中p1和p2是描述線上的點。 兩個避免點擊旁邊的實際線,但在直線,你需要檢查你的點的X座標以及。調整5兩個定義線路必須被擊中的確切程度。

實際上,您需要一些存儲的行,並在每次鼠標單擊後檢查並應用更多的邏輯。最好是先定義線條類

Public Class MyLine 
    Public Property P1 As Point 
    Public Property P2 as Point 
    Public Property IsSelected As Boolean 

    Public Sub New(P1 as Point, P2 as Point) 
     Me.P1 = P1 
     Me.P2 = P2 
     IsSelected = False 
    End Sub 

    Public Sub DrawMe(ByRef g as Graphics, p As Pen) 
     If IsSelected = False Then 
      g.DrawLine(p, P1, P2) 
     Else 
      g.DrawLine(Pens.Red, P1, P2) 'Draw the line differently if it is selected 
     EndIf 
    End Sub 

    Public Sub TrySelect(MouseLocation As Point) 
     Dim m, b As Double 
     GetSlopeIntercept(p1, p2, m, b) 'See above 

     Dim YLine as Double = m * MouseLocation.X + b 
     If Math.Abs(MouseLocation.Y - YLine) < 5 AndAlso _ 
      MouseLocation.X > Math.Min(p1.X, p2.X) AndAlso _ 
      MouseLocation.X < Math.Max(p1.X, p2.X) _ 
      Then Me.IsSelected = True Else Me.IsSelected = False 
    End Sub 
End Class 

保留線對象的集合進行繪製和處理。

相關問題