2
我正在開發多點觸控應用程序。有一種方法可以在全屏模式下打開圖像,當我做pinchzoom時?在WPF中使用多點觸控打開全屏圖像
的代碼是這樣的:
<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();
}
}
}
}
}
如果您需要發佈的說明太大而不適合發表評論,則應將其包含在您的問題中。 –