移动和调整表面窗口本身的大小

时间:2013-07-30 03:43:07

标签: c# .net wpf

如何在没有异常行为的情况下用手指拖动/捏动来移动/调整SurfaceWindow本身的大小?我有一个表面窗口,检查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个位置之间跳跃。在我用手指停止移动窗户后,如何让它保持静止?

1 个答案:

答案 0 :(得分:0)

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

有关详细信息,请参阅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