2011-09-23 46 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