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 ...
随机推荐
- JMeter 配置元件之随机变量(RandomVariable)介绍
配置元件之随机变量(Random Variable)介绍 by:授客 QQ:1033553122 测试环境 apache-jmeter-3.2 1. 计数器简介 允许用户创建一个在线程组范围之内都 ...
- 初步了解redux
redux作为react的状态状态管理工具,是十分重要的一部分,但是它在学习起来比较困难.它的写法一共分为三部分,而且他不像其他的东西一样可以写一步,在页面上查看一下.它必须三个部分全部完成之后,才能 ...
- java 大文件分割与组装
不多说,直接上代码 1 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; im ...
- 修改minifest使桌面软件支持高dpi
在VisualStudio中可以很方便的设置manifest以支持高dpi的用户界面.当然也可以手工修改manifest文件来添加对高dpi的支持. QQ在高dpi方面做的尤其差,对高dpi的支持迟迟 ...
- html5常见新增标签
本文内容: header nav article footer section aside datalist 音频标签: audio 视频标签: video 插入媒体标签: embed 新增input ...
- python leetcode 字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- Android Handler、Message、MessageQueue和Looper官方说明
Handler官方说明 官方API文档:https://developer.android.google.cn/reference/android/os/Handler Handler允许您发送和处理 ...
- selenium-获取一组数组进行操作(七)
selenium-获取一组数组进行操作 以 纵横中文网 中获取24小时畅销榜的书单为例 此文仅做 selenium 在自动化测试中怎么获取一组数据进行说明,不做网络爬虫解释 当然,使用爬虫得到本文 ...
- SpringBoot集成spring-data-jpa注入Bean失败
当项目结构正常(spring管理的Bean在SrpingBoot启动类平级或下级,支持spring扫描时),实现类上加 @Service注解,在实现类中注入dao层的Bean时,项目无法启动,无法找到 ...
- c/c++ 标准库 string
c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...