Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to develop an app for windows desktop that support touch screens
so i want let user zoom or move pics with touch
but my problem is with this code the picture will move around the windows not just in its parent (Canvas)
i want to limit picture (rectangle) in it's parent (canvas)

this is a sample code to move or zoom a rectangle :

xaml

XML
<Canvas Background="Green"  x:Name="_canvas"
       ManipulationStarting="_canvas_ManipulationStarting"
       ManipulationDelta="_canvas_ManipulationDelta" Margin="76,105,117,46">
           <rectangle ismanipulationenabled="True" fill="Red" width="100" height="100"></rectangle>
       </Canvas>



C#
C#
private void _canvas_ManipulationStarting(object sender,
                      ManipulationStartingEventArgs e)
{
    e.ManipulationContainer = _canvas;
    e.Handled = true;
}


private void _canvas_ManipulationDelta(object sender,
                         ManipulationDeltaEventArgs e)
{

    var element = e.OriginalSource as UIElement;

    var transformation = element.RenderTransform
                                         as MatrixTransform;
    var matrix = transformation == null ? Matrix.Identity :
                                   transformation.Matrix;

    matrix.ScaleAt(e.DeltaManipulation.Scale.X,
                   e.DeltaManipulation.Scale.Y,
                   e.ManipulationOrigin.X,
                   e.ManipulationOrigin.Y);

    matrix.RotateAt(e.DeltaManipulation.Rotation,
                    e.ManipulationOrigin.X,
                    e.ManipulationOrigin.Y);

    matrix.Translate(e.DeltaManipulation.Translation.X,
                     e.DeltaManipulation.Translation.Y);

    element.RenderTransform = new MatrixTransform(matrix);
    e.Handled = true;
}


What I have tried:

i try to add touch support to my application
Posted
Updated 23-Jun-16 10:54am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900