【问题标题】:Moving and Resizing a Surface Window itself移动和调整表面窗口本身
【发布时间】:2013-10-29 12:42:21
【问题描述】:

如何通过用手指拖动/捏合来移动/调整SurfaceWindow 本身的大小,而不会出现异常行为?我有一个 Surface Window,选中了IsManipulationEnabled。在ManipulationStarting,我有:

    e.ManipulationContainer = this; <--- source of unpredictable behavior?
    e.Handled = true;

ManipulationDelta:

    this.Left += e.DeltaManipulation.Translation.X;
    this.Top += e.DeltaManipulation.Translation.Y;
    this.Width *= e.DeltaManipulation.Scale.X; <----zooming works properly
    this.Height *= e.DeltaManipulation.Scale.X;

    e.Handled = true;

移动的问题在于它会在两个完全不同的位置之间不断跳跃,从而产生闪烁的效果。我在控制台中打印了一些数据,似乎 e.ManipulationOrigin 一直在变化。以下数据是我在屏幕上拖动(仅打印 X 值)并在最后按住手指静止一秒钟后的值:

Window.Left    e.Manipulation.Origin.X    e.DeltaManipulation.Translation.X
---------------------------------------------------------------------------
1184           699.616                    0
1184           577.147                    -122.468
1062           577.147                    0
1062           699.264                    122.117
1184           699.264                    0
1184           576.913                    -122.351

and it goes on 

您可以从Window.Left 看到它在 2 个位置之间跳跃。在我停止用手指移动窗口后,如何让它保持静止?

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    下面的代码至少允许你拖动;它通过使用屏幕坐标解决了您遇到的问题 - 因为使用窗口坐标意味着参考点随着窗口的移动而变化。

    更多信息请见http://www.codeproject.com/Questions/671379/How-to-drag-window-with-finger-not-mouse..

     static void MakeDragging(Window window) {
                bool isDown = false;
                Point position = default(Point);
                window.MouseDown += (sender, eventArgs) => {
                    if (eventArgs.LeftButton != MouseButtonState.Pressed) return;
                    isDown = true;
                    position = window.PointToScreen(eventArgs.GetPosition(window));
                };
                window.MouseUp += (sender, eventArgs) => {
                    if (eventArgs.LeftButton != MouseButtonState.Released) return;
                    isDown = false;
                };
                window.MouseMove += (sender, eventArgs) => {
                    if (!isDown) return;
                    Point newPosition = window.PointToScreen(eventArgs.GetPosition(window));
                    window.Left += newPosition.X - position.X;
                    window.Top += newPosition.Y - position.Y;
                    position = newPosition;
                };
            } //MakeDragging
    

    【讨论】:

    • 我不认为用多个手指移动可以正常工作
    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多