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 ...
随机推荐
- Android SharedPreferences增,删,查操作
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...
- Android为TV端助力 StringBuffer 和StringBuilder
如果我们的程序是在单线程下运行,或者是不必考虑到线程同步问题,我们应该优先使用StringBuilder类:如果要保证线程安全,自然是StringBuffer. 除了对多线程的支持不一样外,这两个类的 ...
- 虚拟现实的头戴式设备的视野(FOV)原理
本文原址https://www.cnblogs.com/zhangmiao14/p/5836664.html. 对于VR,它做得最好的就是它对生活的变化,有一些关键因素需要调整的恰如其分.如果做得正确 ...
- MyBatis-Plus初步使用
在使用mybatis的过程中,我们会发现需要自己写很多的mapper和mapper.xml配置文件,很多时候会写到相当多的重复代码,特别是普通的增删改查,这样不仅会影响我们的开发效率,也会使得代码变的 ...
- 淘宝开放平台使用WebClient,WebRequest访问时的错误提示导致麻烦
淘宝开放平台(TOP)提供OAuth2.0支持 通过C#的WebClient/WebRequest直接访问时会提示grant type is empty,这是一个非常恼人的错误,你会发现即使传了这个参 ...
- python之模块使用
1.入口 """ 模块测试入口 """ import show_message as sm # 导入方式一 sm.show(sm.__nam ...
- MHA快速搭建
很早之前写过MHA的文章,但是常常在技术群看到有同学问MHA搭建的问题,不是权限问题就是配置问题,我在这里就再次一写下配置过程以及快速的搭建.如果想知道更多的细节与原理,请参考:MySQL高可用架构之 ...
- Oracle EBS 查看执行计划
explain plan forSELECT MMT.TRANSACTION_ID,GIR.JE_HEADER_ID,GIR.JE_LINE_NUMFROM GL_IMPORT_REFERENCE ...
- 【底层原理】深入理解Cache (上)
存储器是分层次的,离CPU越近的存储器,速度越快,每字节的成本越高,同时容量也因此越小.寄存器速度最快,离CPU最近,成本最高,所以个数容量有限,其次是高速缓存(缓存也是分级,有L1,L2等缓存),再 ...
- c/c++ 图的创建及图的相关函数(链表法)
c/c++ 图的创建及图的相关函数(链表法) 图的概念 图由点和线组成 知道了图中有多少个点,和哪些点之间有线,就可以把一张图描绘出来 点之间的线,分有方向和无方向 创建图 创建图,实际就是创建出节点 ...