2016-08-11 61 views
0

我有一个小的未装饰xaml窗口的触摸屏。用户必须能够使用触摸和拖动来移动窗口。目前触摸和拖动时,窗口朝拖动的方向移动,但仅部分移动;并且似乎有两个窗口而不是一个窗口,使得触摸和拖拽显得很跳跃。跳跃和不完整的XAML窗口拖动与C#TouchMove

此行为体现在开发系统(Surface Pro 3使用Visual Studio Professional 2015)以及生产系统(Windows 7,不包括键盘或鼠标)上。

我在Microsoft的example上以此C#为基础。

using System.Windows; 
using System.Windows.Input; 

namespace XAMLApp 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private TouchDevice windowTouchDevice; 
     private Point lastPoint; 

     private void Circle_TouchUp(object sender, TouchEventArgs e) 
     { 
      // Do stuff. 
     } 

     private void Window_TouchDown(object sender, TouchEventArgs e) 
     { 
      e.TouchDevice.Capture(this); 

      if (windowTouchDevice == null) 
      { 
       windowTouchDevice = e.TouchDevice; 
       lastPoint = windowTouchDevice.GetTouchPoint(null).Position; 
      } 

      e.Handled = true; 
     } 

     private void Window_TouchMove(object sender, TouchEventArgs e) 
     { 
      if (e.TouchDevice == windowTouchDevice) 
      { 
       var currentTouchPoint = windowTouchDevice.GetTouchPoint(null); 

       var deltaX = currentTouchPoint.Position.X - lastPoint.X; 
       var deltaY = currentTouchPoint.Position.Y - lastPoint.Y; 

       Top += deltaY; 
       Left += deltaX; 

       lastPoint = currentTouchPoint.Position; 

       e.Handled = true; 
      } 
     } 

     private void Window_TouchLeave(object sender, TouchEventArgs e) 
     { 
      if (e.TouchDevice == windowTouchDevice) 
       windowTouchDevice = null; 

      e.Handled = true; 
     } 
    } 
} 

这里是窗口的一些xaml。

<Window x:Name="AppWindow" x:Class="XAMLApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:XAMLApp" 
     mc:Ignorable="d" 
     Title="XAML Application" 
     Height="25" Width="25" 
     AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize" 
     ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
     ShowInTaskbar="False" ToolTip="XAML Application" Topmost="True" UseLayoutRounding="True" 
     MaxHeight="25" MaxWidth="25" MinHeight="25" MinWidth="25" 
     Left="0" Top="0" Background="Transparent" 
     TouchDown="Window_TouchDown" 
     TouchMove="Window_TouchMove" 
     TouchLeave="Window_TouchLeave"> 
    <Grid> 
     <Ellipse x:Name="Circle" Fill="Black" HorizontalAlignment="Left" 
       Height="24" Margin="0" Stroke="Black" VerticalAlignment="Top" 
       Width="24" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
       TouchUp="Circle_TouchUp" /> 
    </Grid> 
</Window> 

我已经试过GridCanvas更换。这没有什么区别。我也尝试使用微软的demonstrated操作。当试图拖动窗口时,我被告知该窗口的转换无效。

如何使用鼠标左键单击并拖动来使触摸和拖动的行为与DragMove()相同?

回答

1

TouchPoint通过TouchDevice.GetTouchPoint方法检索并且窗口的TopLeft属性不共享相同的坐标系。所有你需要做的是转换检索到屏幕上的X和Y值坐标:

private void Window_TouchMove(object sender, TouchEventArgs e) 
{ 
    if (e.TouchDevice == windowTouchDevice) 
    { 
     var currentTouchPoint = windowTouchDevice.GetTouchPoint(null); 

     var locationOnScreen = this.PointToScreen(new Point(currentTouchPoint.Position.X, currentTouchPoint.Position.Y)); 

     var deltaX = locationOnScreen.X - lastPoint.X; 
     var deltaY = locationOnScreen.Y - lastPoint.Y; 

     Top += deltaY; 
     Left += deltaX; 

     lastPoint = locationOnScreen; 

     e.Handled = true; 
    } 
} 

编辑:

当然,对事物的不同坐标系适用于整个项目,所以Window_TouchDown事件处理方法需要以类似的方式进行调整:

private void Window_TouchDown(object sender, TouchEventArgs e) 
{ 
    e.TouchDevice.Capture(this); 

    if (windowTouchDevice == null) 
    { 
     windowTouchDevice = e.TouchDevice; 
     var currentTouchPoint = windowTouchDevice.GetTouchPoint(null); 
     var locationOnScreen = this.PointToScreen(new Point(currentTouchPoint.Position.X, currentTouchPoint.Position.Y)); 
     lastPoint = locationOnScreen; 
    } 

    e.Handled = true; 
} 
+0

啊,几秒钟太晚了......但是,如果你还有兴趣,我的回答可以解释为什么你原来的解决方案did'nt工作。 – andreask

+0

我会继续向您颁奖,因为我感谢您澄清问题。但是,您提供的代码实际上会使窗口从屏幕消失,因此可以使用编辑。我将答案标记为答案,因为这是我解决问题的方式。非常感谢! –

+0

你是对的,解决方案中缺少一点东西。我编辑了答案给出了整个图片! – andreask

0

而不是

Top += deltaY; 
Left += deltaX; 

我只是需要

Top += currentTouchPoint.Position.Y; 
Left += currentTouchPoint.Position.X; 

唉。