2012-12-20 37 views
1

頂部我是silverlight的新手,所以我可能只是因爲缺乏知識而錯過了一些東西。我正在編寫一個使用矩形的應用程序,您可以在它們生成後單擊並拖動到屏幕周圍。我有一個生成的矩形函數:使文字移動與矩形是

public void formatBox(block b) 
    { 
     Rectangle Rec = new Rectangle(); 
     Rec.Height = 100; 
     Rec.Width = 200; 
     SolidColorBrush newBrush = new SolidColorBrush(); 
     newBrush.Color = b.blockColor; 
     SolidColorBrush blackBrush = new SolidColorBrush(); 
     blackBrush.Color = Colors.Black; 
     Rec.StrokeThickness = 4; 
     Rec.Stroke = blackBrush; 
     Rec.Fill = newBrush; 
     Canvas.SetTop(Rec, generateY(b.locationY)); 
     Canvas.SetLeft(Rec, generateX(b.locationX)); 
     TextBlock blockname = new TextBlock(); 
     blockname.Text = b.blockText; 
     blockname.FontSize = 25; 
     canvas1.Children.Add(Rec); 
     canvas1.Children.Add(blockname); 
     Binding topbinding = new Binding("Top"); 
     Binding leftbinding = new Binding("Left"); 
     topbinding.Mode = BindingMode.OneWay; 
     leftbinding.Mode = BindingMode.OneWay; 
     topbinding.Source = Rec.GetValue(Canvas.TopProperty); 
     leftbinding.Source = Rec.GetValue(Canvas.LeftProperty); 
     blockname.SetBinding(Canvas.TopProperty, topbinding); 
     blockname.SetBinding(Canvas.LeftProperty, leftbinding); 
     Rec.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown); 
     Rec.MouseMove += new MouseEventHandler(Handle_MouseMove); 
     Rec.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp); 
    } 

這些矩形從一個塊類

public class block 
    { 
     public double locationX { get; set; } 
     public double locationY { get; set; } 
     public Color blockColor { get; set; } 
     public string blockText { get; set; } 
     public block(double x, double y, Color c, string s) 
     { 
      this.locationX = x; 
      this.locationY = y; 
      this.blockColor = c; 
      this.blockText = s; 
     } 
    } 

建造和我的鼠標事件處理程序:

bool isMouseCaptured; 
    double mouseVerticalPosition; 
    double mouseHorizontalPosition; 

    public void Handle_MouseDown(object sender, MouseEventArgs args) 
    { 
     Rectangle item = sender as Rectangle; 
     mouseVerticalPosition = args.GetPosition(null).Y; 
     mouseHorizontalPosition = args.GetPosition(null).X; 
     isMouseCaptured = true; 
     item.CaptureMouse(); 
    } 

    public void Handle_MouseMove(object sender, MouseEventArgs args) 
    { 
     Rectangle item = sender as Rectangle; 
     if (isMouseCaptured) 
     { 

      // Calculate the current position of the object. 
      double deltaV = args.GetPosition(null).Y - mouseVerticalPosition; 
      double deltaH = args.GetPosition(null).X - mouseHorizontalPosition; 
      double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty); 
      double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty); 

      // Set new position of object. 
      item.SetValue(Canvas.TopProperty, newTop); 
      item.SetValue(Canvas.LeftProperty, newLeft); 

      // Update position global variables. 
      mouseVerticalPosition = args.GetPosition(null).Y; 
      mouseHorizontalPosition = args.GetPosition(null).X; 
     } 
    } 


    public void Handle_MouseUp(object sender, MouseEventArgs args) 
    { 
     Rectangle item = sender as Rectangle; 
     isMouseCaptured = false; 
     item.ReleaseMouseCapture(); 
     mouseVerticalPosition = -1; 
     mouseHorizontalPosition = -1; 
    } 

文本,我試圖移動在formatBox()中被稱爲blockname。我已經嘗試了Binding,因爲您可以在這裏看到,但我認爲在我的知識中存在一個更簡單的方法來做到這一點。無論哪種方式,當鼠標事件處理程序被觸發時,我們希望文本在塊移動時移動。我如何讓文字隨其移動?

+0

爲什麼標記WPF如果這是silverlight? – Paparazzi

+0

由於綁定工作方式相似,但現在已將其刪除以使其更具體。 –

回答

1

既然你已經有鼠標手,你可以跳過綁定和使用代碼。 (無論如何,綁定不會如預期的那樣工作:文本的座標與矩形的座標相同,因此它會被繪製在矩形的頂部/左邊線上,這看起來很難看,而且文本難以閱讀,您需要偏移文本以使其位於矩形內部或外部)。基本上你要做的就是在MouseDown上放置一個高標誌,鼠標被按下,並記錄鼠標所在的位置。然後在MouseMove中檢查標誌:如果它處於打開狀態,則計算矩形的新位置,因爲它的當前位置+從MouseDown中記錄的點移開的距離。文本的位置將成爲新的位置+一些偏移量。

btw我建議將formatBox等方法拆分爲多個較小的方法,併爲變量選擇更好的名稱:它將使代碼不僅更易讀,而且更易於維護。

編輯 找出要移動的矩形,對MouseDwon處理程序中的所有元素執行命中測試。事情是這樣的:

Rectangle rectangleUnderMouse = null; 
foreach(var rec in rectangles) 
{ 
    if(VisualTreeHelper.HitTest(rec, pointWhereMouseIs)) 
    { 
    rectangleUnderMouse = rec; 
    break; 
    } 
} 

編輯 對不起,我沒看到你問要移動的文本塊..這很容易:你可以保持一個Dictionary<Rectangle,TextBlock>主類中:

public void formatBox(block b) 
{ 
    //... 
    myDictionary[ Rec ] = textblock; 
} 

public void Handle_MouseMove(object sender, MouseEventArgs args) 
{ 
    //... 
    textBlockForThisRect = myDictionary[ item ]; 
    //move textBlockForThisRect 
} 
+0

問題是'MouseDown'會在矩形上觸發而不是文本。有沒有辦法在鼠標點擊矩形時觸發文本的'MouseDown'事件?我明白你對分手方法的看法。感謝您的反饋。 –

+0

您不需要在文本上使用鼠標事件:您將鼠標事件中的文本移動到矩形 – stijn

+0

,我將如何識別要移動的文本塊?如果這是一個愚蠢的問題,我很抱歉,我可以發佈我的事件處理程序來移動矩形。 –