silverlight 中鼠标任意拖动控件的实例分享

目标: 实现一个效果: 在桌面实现任意拖动一个border元素里面的控件,并且规定 元素拖动的区域

环境:silverlight4.0

实现的步骤我就不啰嗦啦,代码奉上:

  xaml代码(注意,原来的界面很复杂,为了方便大家看,一些修饰的样式没了,仅供参考,可以自行的修改)

 

<Border x:Name="Border1" BorderThickness="1"   Visibility="Visible" Canvas.ZIndex="1"    Canvas.Left="0" Canvas.Top="0"  MouseLeftButtonDown="StackPanel_MouseLeftButtonDown" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" MouseMove="StackPanel_MouseMove" >
        <StackPanel  x:Name="sds" Orientation="Horizontal" Background="#424D85" Height="60"  >
            <Button x:Name="btnAdd" Height="50" Width="50"   Margin="2,0,2,0" >
                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="新建"  Width="22" Height="22" FontSize="9"/>
                    </StackPanel>
                </Button.Content>
            </Button>
            <Button  x:Name="btnUpd" Height="50" Width="50"  Margin="2,0,2,0" >

                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="修改" Width="22" Height="22" FontSize="9"/>
                    </StackPanel>
                </Button.Content>
            </Button>
            <Button  x:Name="btnSave" Height="50" Width="50"   Margin="2,0,2,0">
                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="保存"  Width="22" Height="22" FontSize="9"/>
                    </StackPanel>
                </Button.Content>
            </Button>
            <Button  x:Name="btnDel" Height="50" Width="50"  Margin="2,0,2,0"  >
                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="删除" Width="22" Height="22" FontSize="9"/>
                    </StackPanel>
                </Button.Content>

            </Button>
            <Button x:Name="btnQry" Height="50" Width="50"   Margin="2,0,2,0">
                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="查询" Width="22" Height="22" FontSize="9" />
                    </StackPanel>
                </Button.Content>

            </Button>
            <Button  x:Name="btnRefresh" Height="50" Width="50"    Margin="2,0,2,0">
                <Button.Content>
                    <StackPanel  Orientation="Horizontal">
                        <TextBlock Text="刷新" Width="22" Height="22" FontSize="9"/>
                    </StackPanel>
                </Button.Content>
            </Button>
            
            <Button Content="关闭" Height="50" Width="50" ></Button>
        </StackPanel>
    </Border>

 

 

看看后台啦:

 

        #region 工具栏随意拖动事件
        //桌面按钮拖放变量
        bool trackingMouseMove = false;
        Point mousePosition;
        Border BtnCurrent = new Border();  //当前移动的按钮
        bool isMoved = false; //是否曾经移动

        /// <summary>
        /// 鼠标左键被按下
        /// <author>Sun</author>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          
            FrameworkElement element = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            trackingMouseMove = true;
            BtnCurrent = (Border)sender;
            if (null != element)
            {
                element.CaptureMouse();
                element.Cursor = Cursors.Hand;
            }
        }
        /// <summary>
        /// 鼠标左键被弹起来
        /// <author>Sun</author>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();
            BtnCurrent = (Border)sender;
            double ileft = System.Convert.ToDouble(this.sds.GetValue(Canvas.LeftProperty));
            double iTop = System.Convert.ToDouble(this.sds.GetValue(Canvas.TopProperty));
            if (IsInRegions(mousePosition)) //如果超出了规定的区域,就让
            {
              Point p=  e.GetPosition((e.OriginalSource as FrameworkElement));
              BtnCurrent.SetValue(Canvas.LeftProperty,0.0);
              BtnCurrent.SetValue(Canvas.TopProperty,0.0);

            }
            else
            {
                mousePosition.X = mousePosition.Y = 0;
                this.sds.SetValue(Canvas.TopProperty, iTop + 52);
                this.sds.SetValue(Canvas.LeftProperty, ileft + 52);
           
            }
            if (!isMoved) //单击按钮 但是用户没有拖动 如果拖动了 这个不能运行
            { }
            isMoved = false;
           
        }
        /// <summary>
        /// <author>Sun</author>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                double deltaH = e.GetPosition(null).X - mousePosition.X;
                double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);
                element.SetValue(Canvas.TopProperty, newTop);
                element.SetValue(Canvas.LeftProperty, newLeft);
                isMoved = true;  //在移动时候 开关变量 为真
                mousePosition = e.GetPosition(null);
            }
           
        }
     /// <summary>
        /// <author>Sun</author>
     /// </summary>
     /// <param name="p"></param>
     /// <returns></returns>
        private bool IsInRegions(Point p)
        {
            double x = p.X;
            double y = p.Y;
            return y < 88 || x < 356|| x>1400 || y>660;//规定的临界范围
        }
        #endregion  工具栏拖动事件结束

 

 

 

就写到这,时间很紧,仓促的和大家分享啦

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值