2010年6月4日 星期五

WPF 選取物件範例

Public Sub SelObj(ByVal e As System.Windows.Input.MouseEventArgs)
Dim OPos As Point = e.GetPosition(Me.Canvas_Draw)
Dim i As Integer
Dim s1 As String = ""
Dim sObj As String = ""
Dim x11, ww, y11, hh As Double

'找出選取物件
For i = 0 To Canvas_Draw.Children.Count - 1
s1 = Canvas_Draw.Children(i).GetValue(NameProperty)
Select Case s1.Substring(0, 2)
Case "Pt"
x11 = Canvas_Draw.Children(i).GetValue(Canvas.LeftProperty)
ww = x11 + Canvas_Draw.Children(i).GetValue(Canvas.WidthProperty)
y11 = Canvas_Draw.Children(i).GetValue(Canvas.TopProperty)
hh = y11 + Canvas_Draw.Children(i).GetValue(Canvas.HeightProperty)
If ((x11 <> OPos.X) And (y11 <> OPos.Y)) Then
sObj = i.ToString() + ","
SelNo = i
SelNm = s1
End If
Case "Ln"
x11 = Canvas_Draw.Children(i).GetValue(Canvas.LeftProperty)
ww = x11 + Canvas_Draw.Children(i).GetValue(Line.X2Property)
y11 = Canvas_Draw.Children(i).GetValue(Canvas.TopProperty)
hh = y11 + Canvas_Draw.Children(i).GetValue(Line.Y2Property)
If ((x11 <> OPos.X) And (y11 <> OPos.Y)) Or _
((x11 <> OPos.X) And (y11 > OPos.Y And hh <> OPos.X And ww <> OPos.Y)) Or _
((x11 > OPos.X And ww <> OPos.Y And hh < sobj =" i.ToString()" selno =" i" selnm =" s1" x11 =" Canvas_Draw.Children(i).GetValue(Canvas.LeftProperty)" ww =" x11" y11 =" Canvas_Draw.Children(i).GetValue(Canvas.TopProperty)" hh =" y11"> OPos.X) And (y11 <> OPos.Y)) Then
sObj = i.ToString() + ","
SelNo = i
SelNm = s1
End If
Case "Ep"
x11 = Canvas_Draw.Children(i).GetValue(Canvas.LeftProperty)
ww = x11 + Canvas_Draw.Children(i).GetValue(Canvas.WidthProperty)
y11 = Canvas_Draw.Children(i).GetValue(Canvas.TopProperty)
hh = y11 + Canvas_Draw.Children(i).GetValue(Canvas.HeightProperty)
If ((x11 <> OPos.X) And (y11 <> OPos.Y)) Then
sObj = i.ToString() + ","
SelNo = i
SelNm = s1
End If
Case "Re"
x11 = Canvas_Draw.Children(i).GetValue(Canvas.LeftProperty)
ww = x11 + Canvas_Draw.Children(i).GetValue(Canvas.WidthProperty)
y11 = Canvas_Draw.Children(i).GetValue(Canvas.TopProperty)
hh = y11 + Canvas_Draw.Children(i).GetValue(Canvas.HeightProperty)
If ((x11 <> OPos.X) And (y11 <> OPos.Y)) Then
sObj = i.ToString() + ","
SelNo = i
SelNm = s1
End If
End Select
Next
'如果多個被選取,取出距離最小
If sObj <> "" Then
sObj = sObj.Substring(0, sObj.Length - 1)
Dim ss1() As String
ss1 = sObj.Split(",")
Dim xc() As Double = New Double(ss1.Count) {}
Dim yc() As Double = New Double(ss1.Count) {}
Dim Lst1 As New List(Of Double)
Dim Lst2 As New List(Of Double)
Dim Lst3 As New List(Of Integer)
Dim minV As Double = 0

'判定被選取物件
For i = 0 To ss1.Count - 1
Dim nn As Integer = CInt(ss1(i))
Lst3.Add(nn)
Select Case SelNm.Substring(0, 2)
Case "Pt"
xc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.LeftProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.WidthProperty)) / 2
yc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.TopProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.HeightProperty)) / 2
Dim dd As Double = System.Math.Sqrt((OPos.X - xc(i)) * (OPos.X - xc(i)) + (OPos.Y - yc(i)) * (OPos.Y - yc(i)))
Lst1.Add(dd)
Lst2.Add(dd)
Case "Ln"
xc(i) = Canvas_Draw.Children(nn).GetValue(Line.X1Property) + Canvas_Draw.Children(nn).GetValue(Line.X2Property) / 2
yc(i) = Canvas_Draw.Children(nn).GetValue(Line.Y1Property) + Canvas_Draw.Children(nn).GetValue(Line.Y2Property) / 2
Dim dd As Double = System.Math.Sqrt((OPos.X - xc(i)) * (OPos.X - xc(i)) + (OPos.Y - yc(i)) * (OPos.Y - yc(i)))
Lst1.Add(dd)
Lst2.Add(dd)
Case "Ci"
xc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.LeftProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.WidthProperty)) / 2
yc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.TopProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.HeightProperty)) / 2
Dim dd As Double = System.Math.Sqrt((OPos.X - xc(i)) * (OPos.X - xc(i)) + (OPos.Y - yc(i)) * (OPos.Y - yc(i)))
Lst1.Add(dd)
Lst2.Add(dd)
Case "Ep"
xc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.LeftProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.WidthProperty)) / 2
yc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.TopProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.HeightProperty)) / 2
Dim dd As Double = System.Math.Sqrt((OPos.X - xc(i)) * (OPos.X - xc(i)) + (OPos.Y - yc(i)) * (OPos.Y - yc(i)))
Lst1.Add(dd)
Lst2.Add(dd)
Case "Re"
xc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.LeftProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.WidthProperty)) / 2
yc(i) = (Canvas_Draw.Children(nn).GetValue(Canvas.TopProperty) + Canvas_Draw.Children(nn).GetValue(Canvas.HeightProperty)) / 2
Dim dd As Double = System.Math.Sqrt((OPos.X - xc(i)) * (OPos.X - xc(i)) + (OPos.Y - yc(i)) * (OPos.Y - yc(i)))
Lst1.Add(dd)
Lst2.Add(dd)
End Select


Next
'排序
Lst1.Sort()
Dim n0 As Integer = Lst2.IndexOf(Lst1(0))
'取出物件號碼
Dim n1 As Integer = Lst3(n0)
'設定被選取物件為紅色
For i = 0 To Canvas_Draw.Children.Count - 1
If i <> n1 Then
Dim s7 As String = Canvas_Draw.Children(i).GetValue(NameProperty)
Select Case s7.Substring(0, 2)
Case "Pt"
Canvas_Draw.Children(i).SetValue(Ellipse.FillProperty, Ys)
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Ys)
Case "Ln"
Canvas_Draw.Children(i).SetValue(Line.StrokeProperty, Ys)
Case "Ci"
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Ys)
Case "Ep"
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Ys)
Case "Re"
Canvas_Draw.Children(i).SetValue(Rectangle.StrokeProperty, Ys)
End Select
Else
Select Case SelNm.Substring(0, 2)
Case "Pt"
Canvas_Draw.Children(i).SetValue(Ellipse.FillProperty, Rs)
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Rs)
Case "Ln"
Canvas_Draw.Children(i).SetValue(Line.StrokeProperty, Rs)
Case "Ci"
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Rs)
Case "Ep"
Canvas_Draw.Children(i).SetValue(Ellipse.StrokeProperty, Rs)
Case "Re"
Canvas_Draw.Children(i).SetValue(Rectangle.StrokeProperty, Rs)
End Select

End If
Next
End If
End Sub

沒有留言:

張貼留言

追蹤者