【问题标题】:How can I divide the figure into the fields using SilverLight如何使用 SilverLight 将图形划分为字段
【发布时间】:2011-09-23 08:41:35
【问题描述】:

有人可以帮我解决这个问题:如何将图形划分为字段,以便根据鼠标单击的区域来执行特定事件?

    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);

【问题讨论】:

  • 需要更多详细信息。你说的图是什么意思??
  • 比如一个圆圈,我需要把它分成几个部分。@Myles J
  • 是不是和html地图有点像?
  • 可能是这样的,但是当你点击它时,它应该被称为一个事件。@baalazamon
  • 圈子?好的,我们谈论的是椭圆形对象或图像吗?

标签: c# asp.net silverlight silverlight-4.0 silverlight-3.0


【解决方案1】:

您可以在数学上划分对象,使用“相对于对象”的鼠标位置来决定您单击的位置,或者您可以叠加多个多边形,每个多边形的颜色 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);

【讨论】:

  • 我有一个带有 4 个按钮的圆圈图:圆圈内的底部、顶部、左侧和右侧。当我单击它的某个部分(圆圈内的底部、顶部、左侧和右侧)时,我需要生成一个事件。@HiTech Magic
  • 如果只有四分之一,你只需要检查相对鼠标位置是高于还是低于控件宽度/高度的一半。我现在将更新我的答案。
  • 它不起作用,因为point.X不是相对位置它是鼠标光标的位置,所以在这种情况下:(if (point.Y > control.Height/2)) - 永远都是真的@HiTech Magic
  • 请仔细阅读答案:“在 LeftMouseButtonDown 事件参数上调用 GetPosition,将控件本身作为参数传递。”。如果将控件传递给 GetPosition,则坐标将相对于控件。 如果您提供代码,将更容易向您展示并避免误解。
  • 我们可以通过邮件连接吗,因为我无法添加代码的主要部分,因为验证不完善? @高科技魔术
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
相关资源
最近更新 更多