Title | Draw a rubberband line with XAML in Visual Basic 2008 |
Description | This example shows how to draw a rubberband line with XAML in Visual Basic 2008. |
Keywords | XAML, WPF, Visual Basic 2008, rubberband, rubberband line |
Categories | Graphics |
|
|
When the user presses the left mouse button, the following code creates a new Line object.
The call to e.GetPosition returns the mouse position relative to the control you pass it, in this case the Canvas canLine.
After creating the Line, the code adds it to the Canvas control canLine.
|
|
Private new_line As Line = Nothing
' Create the new Line.
Private Sub canLine_MouseLeftButtonDown(ByVal sender As _
Object, ByVal e As _
System.Windows.Input.MouseButtonEventArgs) Handles _
canLine.MouseLeftButtonDown
new_line = New Line()
Dim pt As Point = e.GetPosition(canLine)
new_line.Stroke = Brushes.Green
new_line.X1 = pt.X
new_line.Y1 = pt.Y
new_line.X2 = pt.X
new_line.Y2 = pt.Y
canLine.Children.Add(new_line)
End Sub
|
|
When the mouse moves, the following code gets the mouse's position and moves the line's second endpoint to that spot.
|
|
' Move the line's second end point.
Private Sub canLine_MouseMove(ByVal sender As Object, ByVal _
e As System.Windows.Input.MouseEventArgs) Handles _
canLine.MouseMove
If new_line IsNot Nothing Then
Dim pt As Point = e.GetPosition(canLine)
new_line.X2 = pt.X
new_line.Y2 = pt.Y
End If
End Sub
|
|
When the user releases the left mouse button, the following code sets new_line to Nothing so future MouseMove events don't move it.
|
|
' Finish the line.
Private Sub canLine_MouseLeftButtonUp(ByVal sender As _
Object, ByVal e As _
System.Windows.Input.MouseButtonEventArgs) Handles _
canLine.MouseLeftButtonUp
new_line = Nothing
End Sub
|
|
|
|