Как я могу разделить фигуру на поля, используя SilverLight - PullRequest
0 голосов
/ 23 сентября 2011

Может кто-нибудь помочь мне решить вопрос: как я могу разделить фигуру на поля, чтобы в зависимости от того, в какой области будет щелкать мышью, будет выполняться конкретное событие?

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

1 Ответ

3 голосов
/ 23 сентября 2011

Вы можете либо математически разделить объект, используя положение мыши «относительно объекта», чтобы решить, где вы щелкнули, либо вы можете наложить несколько полигонов, каждый с цветным альфа-каналом, установленным на 1% (чтобы ониможет быть проверен на удар, но не виден).

Поскольку вы просто хотите увидеть, в какой четверти круга вы щелкнули, вызовите GetPosition для аргументов события LeftMouseButtonDown, передав элемент управлениясам как параметр.Это вернет вам объект 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...