2011-09-23 45 views
0

有人可以幫我解決這個問題:我怎樣才能將圖形分成不同的字段,這取決於哪個區域是鼠標點擊它會執行一個特定的事件?如何將數字劃分成使用SilverLight的字段

private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     //if (!isDragging) 
     { 
      //creating of my user control element 
      NodePicture node = new NodePicture(); 
      node.Width = 100; 
      node.Height = 100; 

      //use cursor position as the center of the figure 
      Point point = e.GetPosition(this); 
      node.SetValue(Canvas.TopProperty, point.Y - node.Height/2); 
      node.SetValue(Canvas.LeftProperty, point.X - node.Width/2); 
      node.MouseLeftButtonDown += controlReletionshipsLine; 
      LayoutRoot.Children.Add(node); 
     } 
    } 

    private void controlReletionshipsLine(object sender, MouseButtonEventArgs e) 
    { 
     //creating parant element of node 
     ParentNode parentNode = new ParentNode(); 

     //creating connected element of the node 
     ConnectedNode connectedNode = new ConnectedNode(); 

     //creating node element 
     NodePicture node = (NodePicture)sender; 

     //getting the relative position of the element 
     Point point = e.GetPosition(this); 
+0

需要更多細節。你是什​​麼意思的數字? –

+0

例如一個圓圈,我需要將它分成幾個部分。@ Myles J – revolutionkpi

+0

它與HTML地圖類似嗎? – baalazamon

回答

3

您可以劃分對象數學上,用鼠標位置「相對於對象」決定你點擊,或者也可以重疊若干多邊形的,每一個與所述色alpha通道設置爲1 %(因此它們可以通過測試,但不可見)。

正如您只想查看您所點擊的圓圈的哪個區域,請在LeftMouseButtonDown事件參數上調用GetPosition,將控件本身作爲參數傳遞。這將使您返回一個Point對象,其位置相對於控件的左上角。

然後,它僅僅是一個眼見的事它是季度:

private void ControlX_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    // Get the relative position of the element 
    Point point = e.GetPosition(sender as UIElement); 

    if (point.X > control.Width/2) 
    { 
     if (point.Y > control.Height/2) 
     { 
      // You are in the bottom right quarter 
     } 
     else 
     { 
      // You are in the top right quarter 
     } 
    } 
    else 
    { 
     if (point.Y > control.Height/2) 
     { 
      // You are in the bottom left quarter 
     } 
     else 
     { 
      // You are in the top left quarter 
     } 
    } 
} 

在示例代碼中你送我(controlReletionshipsLine)您有:

// getting the relative position of the element 
Point point = e.GetPosition(this); 

它應該是:

// getting the relative position of the element 
Point point = e.GetPosition(sender as UIElement); 
+0

我有一個圓形的圖形,裏面有4個按鈕:圓圈內的底部,頂部,左側和右側。當我點擊它的某個部分(圓圈內的底部,頂部,左側和右側)時,我需要生成一個事件@ HiTech Magic – revolutionkpi

+0

如果它只是宿舍,你只需要檢查相對鼠標位置是否在上面或低於控件寬度/高度的一半。我現在會更新我的答案。 –

+0

它不起作用,因爲point.X不是相對位置,它是鼠標光標的位置,所以在這種情況下:(if(point.Y> control.Height/2)) - 它將始終爲真@ HiTech Magic – revolutionkpi