在WPF中使用多点触控打开全屏图像

时间:2011-12-07 08:22:53

标签: wpf

我正在开发一个多点触控应用程序。当我做pinchzoom时,有一种方法可以在全屏模式下打开图像吗?

代码是这样的:

    <Window x:Class="TouchRect.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TouchRect" 
    Title="MainWindow" Height="700" Width="1000">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="189*"/>
        <RowDefinition Height="472*"/>
    </Grid.RowDefinitions>
    <!--<local:RulerCanvas x:Name="canvas"  >-->

        <Image x:Name="image3" Width="74" Height="49" IsManipulationEnabled="True" Source="flower3.jpg" Margin="446,-268,458,408">
            <Image.RenderTransform>
                <MatrixTransform Matrix="2.41806325085411,0,0,2.41806325085411,280.737615796121,292.420001677231" />
            </Image.RenderTransform>
        </Image>
        <Image x:Name="image2" Width="74" Height="49" IsManipulationEnabled="True" Source="flower2.jpg" Margin="110,-266,794,406" Stretch="Fill" >
            <Image.RenderTransform>
                <MatrixTransform  Matrix="2.41806325085411,0,0,2.41806325085411,280.737615796121,292.420001677231"/>
            </Image.RenderTransform>
        </Image>
        <Image x:Name="image" Width="74" Height="49" IsManipulationEnabled="True" Source="flower.jpg" Stretch=" fill" Margin="-248,-271,1152,411">
            <Image.RenderTransform>
                <MatrixTransform Matrix="2.41806325085411,0,0,2.41806325085411,280.737615796121,292.420001677231" />
            </Image.RenderTransform>
        </Image>
    <MediaElement x:Name="media" Source="C:\Users\Public\Videos\Sample Videos\Wildlife.wmv" Canvas.Left="183" Canvas.Top="151" LoadedBehavior="Manual" IsManipulationEnabled="True" Margin="37,18,38,38" Grid.Row="1" />
    <DataGrid AutoGenerateColumns="False" Height="0" HorizontalAlignment="Left" Margin="100,86,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="217" />
    <!--</local:RulerCanvas>-->

</Grid>

我在.cs

中使用这个类

我必须添加该代码吗?

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;
using System.Windows.Media.Animation;

namespace TouchRect
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            media.Volume = 100;
            media.Play();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

            canvas.ManipulationStarting += new EventHandler<ManipulationStartingEventArgs>(image_ManipulationStarting);
            canvas.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(image_ManipulationDelta);
             // inertia 
            canvas.ManipulationInertiaStarting += new EventHandler<ManipulationInertiaStartingEventArgs>(canvas_ManipulationInertiaStarting);


        }


void canvas_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{                
        // Decrease the velocity of the Rectangle's movement by 
        // 10 inches per second every second.
        // (10 inches * 96 DIPS per inch / 1000ms^2)
        e.TranslationBehavior = new InertiaTranslationBehavior()
        {
            InitialVelocity = e.InitialVelocities.LinearVelocity,
            DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0)
        };

        // Decrease the velocity of the Rectangle's resizing by 
        // 0.1 inches per second every second.
        // (0.1 inches * 96 DIPS per inch / (1000ms^2)
        e.ExpansionBehavior = new InertiaExpansionBehavior()
        {
            InitialVelocity = e.InitialVelocities.ExpansionVelocity,
            DesiredDeceleration = 0.1 * 96 / 1000.0 * 1000.0
        };

        // Decrease the velocity of the Rectangle's rotation rate by 
        // 2 rotations per second every second.
        // (2 * 360 degrees / (1000ms^2)
        e.RotationBehavior = new InertiaRotationBehavior()
        {
            InitialVelocity = e.InitialVelocities.AngularVelocity,
            DesiredDeceleration = 720 / (1000.0 * 1000.0)
        };
        e.Handled = true;                  
}

UIElement last;    


void image_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
    // lazy hack not in the blog post.. 
    var uie = e.OriginalSource as UIElement;    
    if (uie != null)
    {
        if (last != null) Canvas.SetZIndex(last, 0); 
        Canvas.SetZIndex(uie, 2);
        last = uie; 
    } 

    //canvas is the parent of the image starting the manipulation;
    //Container does not have to be parent, but that is the most common scenario
    e.ManipulationContainer = canvas;
    e.Handled = true; 
    // you could set the mode here too 
    // e.Mode = ManipulationModes.All;             
}

void image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    //this just gets the source. 
    // I cast it to FE because I wanted to use ActualWidth for Center. You could try RenderSize as alternate
    var element = e.Source as FrameworkElement; 
    if ( element != null ) 
    { 
        //e.DeltaManipulation has the changes 
        // Scale is a delta multiplier; 1.0 is last size,  (so 1.1 == scale 10%, 0.8 = shrink 20%) 
        // Rotate = Rotation, in degrees
        // Pan = Translation, == Translate offset, in Device Independent Pixels 

        var deltaManipulation = e.DeltaManipulation; 
        var matrix  = ((MatrixTransform)element.RenderTransform).Matrix;            
        // find the old center; arguaby this could be cached 
        Point center =  new Point ( element.ActualWidth/2, element.ActualHeight/2) ;
        // transform it to take into account transforms from previous manipulations 
        center = matrix.Transform(center); 
        //this will be a Zoom. 
        matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y); 
        // Rotation 
        matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);             
        //Translation (pan) 
        matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

        ((MatrixTransform)element.RenderTransform).Matrix = matrix; 

        e.Handled = true;

        // We are only checking boundaries during inertia 
        // in real world, we would check all the time 
        if (e.IsInertial)
        {
            Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

            Rect shapeBounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));

            // Check if the element is completely in the window.
            // If it is not and intertia is occuring, stop the manipulation.
            if (e.IsInertial && !containingRect.Contains(shapeBounds))
            {
                //Report that we have gone over our boundary 
                e.ReportBoundaryFeedback(e.DeltaManipulation); 
                // comment out this line to see the Window 'shake' or 'bounce' 
                // similar to Win32 Windows when they reach a boundary; this comes for free in .NET 4                
                e.Complete();
            }

        }          
    }
}


    }

1 个答案:

答案 0 :(得分:0)

你必须用后面的代码来做到这一点。执行以下步骤:

this.WindowState = WindowState.Normal;
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
this.Top = 0;
this.Left = 0;

看似毫无用处的第一行实际上是对WPF窗口奇怪行为的解决方法(如果已经最大化,那么最大化的窗口将不会覆盖任务栏。)

至于多点触控,here是教程。