2014-02-16 51 views
0

所以我使用Visual Basic Power Pack來做一些基本的簡單圖形。我有能力在需要的地方繪製多條線,並且VB動力包允許我選擇我繪製的實際線條,但是我不知道如何實際選擇這些線條時如何實現代碼。Visual Basic Power Pack - 選擇線

這裏是我的代碼:

Imports Microsoft.VisualBasic.PowerPacks 

Public Class Form1 

    Dim ptA, ptB As Point      ' starting and ending point 
    Dim down = False 
    Dim lines As New List(Of LineShape) 
    Dim temp As LineShape      ' temporary line to be drawn 
    Dim canvas As New ShapeContainer   'shape container 

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown 
     down = True 
     canvas.Parent = Me 
     temp = New LineShape 
     temp.Parent = canvas 
     ptA = New Point(e.X, e.Y) 
     temp.StartPoint = ptA 
     temp.EndPoint = ptA 
    End Sub 

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp 
     down = False 
     ptB = New Point(e.X, e.Y) 
     lines.Add(temp) 
     temp = Nothing 
    End Sub 

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove 
     If down = True Then 
      temp.X2 = e.X 
      temp.Y2 = e.Y 
     End If 
    End Sub 

End Class 

當我運行和編譯這個,每次我按住鼠標不放,移動和釋放,我可以畫一條線。我可以選擇這些行,我只是不知道如何添加代碼,因此當我選擇它時,它會執行某些操作。如果有人可以請幫助我,我將不勝感激。如果有人可能只是告訴我如何使一個消息框顯示出來,當點擊一條線的起點和終點時。

我正在創建一個結構分析程序,應該允許用戶繪製一個建築框架,然後點擊這些線條並添加屬性,比如它所製作的材質等。

非常感謝!

JD

+0

順便說一句,如果有人試圖在VS2013運行它,不要忘了添加Microsoft.VisualBasic程序。 PowerPack參考該項目。 – user2863626

回答

0

添加一個單擊處理程序,以您的臨時行...

Imports Microsoft.VisualBasic.PowerPacks 

Public Class Form1 

    Dim ptA, ptB As Point      ' starting and ending point 
    Dim down = False 
    Dim lines As New List(Of LineShape) 
    Dim temp As LineShape      ' temporary line to be drawn 
    Dim canvas As New ShapeContainer   'shape container 

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown 
     down = True 
     canvas.Parent = Me 
     temp = New LineShape 
     temp.Parent = canvas 
     ptA = New Point(e.X, e.Y) 
     temp.StartPoint = ptA 
     temp.EndPoint = ptA 
    End Sub 

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp 
     down = False 
     ptB = New Point(e.X, e.Y) 

     AddHandler temp.Click, AddressOf LineClickHandler 

     lines.Add(temp) 
     temp = Nothing 
    End Sub 

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove 
     If down = True Then 
      temp.X2 = e.X 
      temp.Y2 = e.Y 
     End If 
    End Sub 

    Private Sub LineClickHandler(sender As Object, e As MouseEventArgs) 
     Dim MyLine As LineShape = DirectCast(sender, LineShape) 

     MsgBox("Start = " & MyLine.StartPoint.ToString & " End Point = " & MyLine.EndPoint.ToString) 
    End Sub 

End Class 
+0

非常感謝。實際上我早就知道了。我創建了一個類,並在該類中實現了可以返回更多有用的東西的處理程序方法。再次感謝。 – user2863626