• 大小: 0.47M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: wpf  滑动  

资源简介

鼠标拖动时 控件滑动

截图:

核心代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace FlashPrac2
{
    /// <summary>  
    /// MainWindow.xaml 的交互逻辑  
    /// </summary>  
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>  
        /// 完成缓冲效果  
        /// </summary>  
        /// <param name="from">起始位置</param>  
        /// <param name="to">目标位置</param>  
        /// <param name="ar">加速加速度</param>  
        /// <param name="dr">减速加速度</param>  
        /// <param name="duration">持续时间</param>  
        private void DoMove(DependencyProperty dp, double to, double ar, double dr, double duration)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();//创建双精度动画对象  
            doubleAnimation.To = to;//设置动画的结束值  
            doubleAnimation.Duration = TimeSpan.FromSeconds(duration);//设置动画时间线长度  
            doubleAnimation.AccelerationRatio = ar;//动画加速  
            doubleAnimation.DecelerationRatio = dr;//动画减速  
            doubleAnimation.FillBehavior = FillBehavior.HoldEnd;//设置动画完成后执行的操作  
            grdTransfer.BeginAnimation(dp, doubleAnimation);//设置动画应用的属性并启动动画  
        }

        private double pressedX;
        /// <summary>  
        /// 点击鼠标,记录鼠标单击的位置  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void grdTest_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ////获得鼠标点击的X坐标  
            pressedX = e.GetPosition(cvsGround).X;
        }

        ////鼠标释放时的操作  
        private void grdTest_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            double transferLeft = Convert.ToDouble(grdTransfer.GetValue(Canvas.LeftProperty));
            //btn1.Content = transferLeft.ToString();
            if (transferLeft > 0)
            {
                transferLeft = 0;
            }
            if (this.Width - transferLeft > cvsGround.Width)
            {
                transferLeft = this.Width - cvsGround.Width;
            }
            ////获得鼠标释放时的位置  
            double releasedX = e.GetPosition(cvsGround).X;
            ////获得距离间隔  
            double interval = releasedX - pressedX;
            pressedX = 0;
            ////计算出传送带要的目标位置  
            double to = transferLeft   interval;
            ////移动  
            //btn1.Content = transferLeft.ToString()   " "   to.ToString();
            // btn1.Content = transferLeft.ToString()   "  "   to.ToString();  
            DoMove(Canvas.LeftProperty, to, 0.1, 0.5, 0.5);
        }
    }
}

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplication1
{
    /// 
    /// App.xaml 的交互逻辑
    /// 

    public partial class App : Application
    {
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-06-23 14:03  WpfApplication1\
     目录           0  2014-06-23 14:25  WpfApplication1\WpfApplication1\
     文件         935  2014-06-23 14:03  WpfApplication1\WpfApplication1.sln
     文件       32256  2014-06-23 16:34  WpfApplication1\WpfApplication1.v11.suo
     文件         187  2014-06-23 14:03  WpfApplication1\WpfApplication1\App.config
     文件         326  2014-06-23 14:05  WpfApplication1\WpfApplication1\App.xaml
     文件         342  2014-06-23 14:03  WpfApplication1\WpfApplication1\App.xaml.cs
     目录           0  2014-06-23 14:03  WpfApplication1\WpfApplication1\bin\
     目录           0  2014-06-23 14:05  WpfApplication1\WpfApplication1\bin\Debug\
     文件       12800  2014-06-23 14:35  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe
     文件         187  2014-06-23 14:03  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.config
     文件       36352  2014-06-23 14:35  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.pdb
     文件       22984  2014-06-24 10:55  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.vshost.exe
     文件         187  2014-06-23 14:03  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.vshost.exe.config
     文件         490  2010-03-17 22:39  WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.vshost.exe.manifest
     文件        1467  2014-06-24 11:00  WpfApplication1\WpfApplication1\FlashPrac2.xaml
     文件        3368  2014-06-23 14:28  WpfApplication1\WpfApplication1\FlashPrac2.xaml.cs
     文件         288  2014-06-23 14:03  WpfApplication1\WpfApplication1\MainWindow.xaml
     文件         661  2014-06-23 14:03  WpfApplication1\WpfApplication1\MainWindow.xaml.cs
     目录           0  2014-06-23 14:03  WpfApplication1\WpfApplication1\obj\
     目录           0  2014-06-23 14:35  WpfApplication1\WpfApplication1\obj\Debug\
     文件        2325  2014-06-23 14:05  WpfApplication1\WpfApplication1\obj\Debug\App.g.cs
     文件        2325  2014-06-23 14:05  WpfApplication1\WpfApplication1\obj\Debug\App.g.i.cs
     文件        7060  2014-06-24 11:00  WpfApplication1\WpfApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件        1908  2014-06-23 14:35  WpfApplication1\WpfApplication1\obj\Debug\FlashPrac2.baml
     文件        8598  2014-06-23 14:35  WpfApplication1\WpfApplication1\obj\Debug\FlashPrac2.g.cs
     文件        8598  2014-06-23 14:35  WpfApplication1\WpfApplication1\obj\Debug\FlashPrac2.g.i.cs
     文件         700  2014-06-23 14:05  WpfApplication1\WpfApplication1\obj\Debug\MainWindow.baml
     文件        3024  2014-06-23 14:05  WpfApplication1\WpfApplication1\obj\Debug\MainWindow.g.cs
     文件        3024  2014-06-23 14:05  WpfApplication1\WpfApplication1\obj\Debug\MainWindow.g.i.cs
     文件           0  2014-06-23 14:03  WpfApplication1\WpfApplication1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
............此处省略24个文件信息

评论

共有 条评论