Silverlgiht鼠标画直线方法

来源:互联网 发布:人工蜂群算法及其应用 编辑:程序博客网 时间:2024/05/20 14:41

 Silverlgiht中画线的方法,起初也是在网上到处寻找,后来在csdn上下了个,但是一看,发现不是很适用,也不方便。故只有自己实现了一个,原理很简单,创建Line 对象,初始设置其可见属性为不可见,鼠标按下时设置其起始X、Y值,并把该对象添加到上层Canvas的子节点中,鼠标移动时设置为可见,并动态改变其末端X、Y值。

代码如下:

  初始化中:

            line = new Line();
            line.Visibility = Visibility.Collapsed;
            line.StrokeThickness = 1;
            line.Stroke = new SolidColorBrush(Colors.Red);

 

 

protected override void MouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            isMove = true;

            mapviewer.LayoutRoot.Children.Add(line);
            sp = e.GetPosition(mapviewer.LayoutRoot);
            line.X1 = sp.X;
            line.Y1 = sp.Y;

        }

        protected override void MouseLeftUp(object sender, MouseButtonEventArgs e)
        {
            isMove = false;
            mapviewer.LayoutRoot.Children.Remove(line);
            

        }

        protected override void MouseMove(object sender, MouseEventArgs e)
        {
            if (isMove)
            {
                line.Visibility = Visibility.Visible;
                ep = e.GetPosition(mapviewer.LayoutRoot);
                line.X2 = ep.X;
                line.Y2 = ep.Y;

                

            }
        }

原创粉丝点击