WPF touch Scroll -触摸滚动
借鉴地址:http://matthamilton.net/touchscrolling-for-scrollviewer
改造后支持上下和左右鼠标拖动滚动:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; namespace TestTouchScroll
{
public class TouchScrolling : DependencyObject
{
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
} public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
} public bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set { SetValue(IsEnabledProperty, value); }
} public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(TouchScrolling), new UIPropertyMetadata(false, IsEnabledChanged)); static Dictionary<object, MouseCapture> _captures = new Dictionary<object, MouseCapture>(); static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as ScrollViewer;
if (target == null) return; if ((bool)e.NewValue)
{
target.Loaded += target_Loaded;
}
else
{
target_Unloaded(target, new RoutedEventArgs());
}
} static void target_Unloaded(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Target Unloaded"); var target = sender as ScrollViewer;
if (target == null) return; _captures.Remove(sender); target.Loaded -= target_Loaded;
target.Unloaded -= target_Unloaded;
target.PreviewMouseLeftButtonDown -= target_PreviewMouseLeftButtonDown;
target.PreviewMouseMove -= target_PreviewMouseMove; target.PreviewMouseLeftButtonUp -= target_PreviewMouseLeftButtonUp;
} static void target_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var target = sender as ScrollViewer;
if (target == null) return; _captures[sender] = new MouseCapture
{
VerticalOffset = target.VerticalOffset,
HorticalOffset = target.HorizontalOffset,
Point = e.GetPosition(target),
};
} static void target_Loaded(object sender, RoutedEventArgs e)
{
var target = sender as ScrollViewer;
if (target == null) return; System.Diagnostics.Debug.WriteLine("Target Loaded"); target.Unloaded += target_Unloaded;
target.PreviewMouseLeftButtonDown += target_PreviewMouseLeftButtonDown;
target.PreviewMouseMove += target_PreviewMouseMove; target.PreviewMouseLeftButtonUp += target_PreviewMouseLeftButtonUp;
} static void target_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var target = sender as ScrollViewer;
if (target == null) return; target.ReleaseMouseCapture();
} static void target_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!_captures.ContainsKey(sender)) return; if (e.LeftButton != MouseButtonState.Pressed)
{
_captures.Remove(sender);
return;
} var target = sender as ScrollViewer;
if (target == null) return; var capture = _captures[sender]; var point = e.GetPosition(target); var dy = point.Y - capture.Point.Y;
var dx = point.X - capture.Point.X; if (Math.Abs(dy) > 5)
{
target.CaptureMouse();
}
if (Math.Abs(dx) > 5)
{
target.CaptureMouse();
} target.ScrollToVerticalOffset(capture.VerticalOffset - dy);
target.ScrollToHorizontalOffset(capture.HorticalOffset - dx); } internal class MouseCapture
{
public Double VerticalOffset { get; set; }
public Double HorticalOffset { get; set; } public Point Point { get; set; }
}
}
}
UI:
<Window x:Class="TestTouchScroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:TestTouchScroll"
Title="MainWindow" Height="410" Width="888">
<Grid>
<ScrollViewer my:TouchScrolling.IsEnabled="True" HorizontalAlignment="Right" Width="351">
<StackPanel> <Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="120" Margin="12" Click="Button_Click">hello wgscd</Button>
</StackPanel> </ScrollViewer> <ScrollViewer my:TouchScrolling.IsEnabled="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Width="458" Background="#FFEBEBEB">
<StackPanel Orientation="Horizontal" > <Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
<Button Height="328" Width="182" Margin="12" Click="Button_Click">hello wgscd</Button>
</StackPanel> </ScrollViewer> </Grid>
</Window>
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; namespace TestTouchScroll
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello");
}
}
}
WPF touch Scroll -触摸滚动的更多相关文章
- NGUI系列教程十(Scroll View实现触摸滚动相册效果)
NGUI中提供了两种Scroll View 一种是通过手指或鼠标滑动视图时移动平面物体,另一种则是直接移动摄像机,他们各有各的好处.但是NGUI提供的Scroll View很难实现类似Android ...
- #747 –在WPF程序的触摸操作中使用惯性移动 (Implementing Inertia during Touch Manipulation)
原文:#747 –在WPF程序的触摸操作中使用惯性移动 (Implementing Inertia during Touch Manipulation) 原文地址:https://wpf.2000th ...
- 2019-11-29-WPF-开启-ScrollViewer-的触摸滚动
原文:2019-11-29-WPF-开启-ScrollViewer-的触摸滚动 title author date CreateTime categories WPF 开启 ScrollViewer ...
- taro scroll tabs 滚动标签 切换
taro scroll tabs 滚动标签 切换 https://www.cnblogs.com/lml-lml/p/10954069.html https://developers.weixin.q ...
- WPF 禁用实时触摸
原文:WPF 禁用实时触摸 微软想把 WPF 作为 win7 的触摸好用的框架,所以微软做了很多特殊的兼容.为了获得真实的触摸消息,微软提供了 OnStylusDown, OnStylusUp, 和 ...
- WPF 插拔触摸设备触摸失效
原文:WPF 插拔触摸设备触摸失效 最近使用 WPF 程序,在不停插拔触摸设备会让 WPF 程序触摸失效.通过分析 WPF 源代码可以找到 WPF 触摸失效的原因. 在 Windows 会将所有的 H ...
- WPF 超长文本的来回滚动
原文:WPF 超长文本的来回滚动 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/article/details/8362 ...
- DOM盒模型和位置 client offset scroll 和滚动的关系
DOM盒模型和位置 client offset scroll 和滚动的关系 概览 在dom里面有几个描述盒子位置信息的值, pading border margin width height clie ...
- 自定义ScrollViewer的Touch事件--触摸上下移动ScrollViewer滚动到指定位置
double mPointY;//触摸点的Y坐标 double mOffsetY;//滚动条当前位置 bool mIsTouch = false;//是否触摸 //触摸事件 private void ...
随机推荐
- 性能优化7--App瘦身
1. 前言 如果你对App优化比较敏感,那么Apk安装包的大小就一定不会忽视.关于瘦身的原因,大概有以下几个方面: 对于用户来说,在功能差别不大的前提下,更小的Apk大小意味更少的流量消耗,也意味着更 ...
- (后端)mybatis中使用Java8的日期LocalDate、LocalDateTime
原文地址:https://blog.csdn.net/weixin_38553453/article/details/75050632 MyBatis的型处理器是属性“createdtime参数映射为 ...
- 2015年6月6日,杨学明老师《IT技术人才管理角色转型与实践》专题培训在苏宁云商成功举办!
2015.6.6,在中国南京苏宁总部,研发资深顾问.资深讲师为苏宁易购IT事业部全体产品总监.研发总监进行了为期一天的<IT技术人才管理角色转型与实践>的内训服务. 杨学明老师分别从技术人 ...
- php post接口,登录功能
登录功能同注册功能一样,都是使用 post 方法,在执行 sql 语句时,同样要使用 "select * from 表名 where 键名 = 参数" 的查询方式,不同的是: 注册 ...
- python LeetCode 两数相除
近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...
- Win10更新
Turn: https://m.uczzd.cn/webview/news?app=meizubrw-iflow&aid=11529477703533248224&cid=100&am ...
- Linux修改挂载目录名称
Local系统管理员新增了一个VG,将一个原挂载点/u02改为了/u02-old, 如下所示. [root@mylnx01 ~]# df -h Filesystem Size ...
- 单用户实例添加DB账号
停止实例 net stop mssqlserver 以单用户启动实例,指定以sqlcmd连接 net start mssqlserver /m"SQLCMD" 以单用户启动实例,指 ...
- js强制不使用“兼容性视图”
在IE8浏览器以后版本,都有一个“兼容性视图”,让不少新技术无法使用.那么如何禁止浏览器自动选择“兼容性视图”,强制IE以最高级别的可用模式显示内容呢?下面就介绍一段HTML代码. X-UA-Comp ...
- c/c++ lambda 表达式 介绍
lambda 表达式 介绍 问题:假设有个需求是,在vector<string>找出所有长度大于等于4的元素.标准库find_if函数的第三参数是函数指针,但是这个函数指针指向的函数只能接 ...