win8 app 鼠标操作和多点操作
win8支持多点触摸技术,而我们在屏幕上所做的各种操作,也最终转换为输入http://code.msdn.microsoft.com/windowsapps/Input-3dff271b
http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh465387#using_manipulation_events
在window runtime上响应触屏事件的方法分为两类:单点和多点。下面分别介绍:
鼠标操作事件:
PointerWheelChanged :鼠标滚轮转动时发生
PointerPressed :鼠标点击下去的时候即触发事件。
PointerReleased :鼠标点击下去的时候释放鼠标时触发事件。
PointerMoved :鼠标在有效范围之内移动之时触发事件。
PointerEntered :鼠标进入有效范围之时触发一次。
PointerExited :鼠标退出有效范围之时触发事件。
这些事件的参数都是 PointerRoutedEventArgs
多点事件包括:
ManipulationStarting
ManipulationStarted
ManipulationInertiaStarting
ManipulationDelta
ManipulationCompleted
当多点操作时,一般先触发 ManipulationStarted事件,然后是一连串的ManipulationDelta事件触发,最后是ManipulationCompleted事件.
注意实现多点操作的时候要设置多点操作对象的 ManipulationMode 属性
http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh465387#using_manipulation_events
下面看一个示例:
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.LayoutRoot.DoubleTapped += LayoutRoot_DoubleTapped;
this.OpeningMessage.Tapped += OpeningMessage_Tapped;
this.Photo.Tapped+=Photo_Tapped;
this.Photo.ManipulationMode=ManipulationModes.All;
this.Photo.ManipulationDelta+=Photo_ManipulationDelta;
}
///
/// Invoked when this page is about to be displayed in a Frame.
///
/// Event data that describes how this page was reached.The Parameter
/// property is typically used to configure the page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
async private void OpeningMessage_Tapped(object sender, TappedRoutedEventArgs e)
{
// Display a FileOpenPicker and allow the user to select a photo
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".bmp");
picker.FileTypeFilter.Add(".gif");
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
// Wrap a WriteableBitmap around the selected photo and display it
WriteableBitmap bitmap = new WriteableBitmap(1, 1);
bitmap.SetSource(stream);
Photo.Source = bitmap;
// Reset the CompositeTransform
PhotoTransform.TranslateX = PhotoTransform.TranslateY = 0.0;
PhotoTransform.ScaleX = PhotoTransform.ScaleY = 1.0;
// Hide the "Tap here" message in case it hasn't been removed already
OpeningMessage.Visibility = Visibility.Collapsed;
}
}
private void Photo_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// Translate the photo
PhotoTransform.TranslateX += e.Delta.Translation.X;
PhotoTransform.TranslateY += e.Delta.Translation.Y;
// Scale the photo
PhotoTransform.ScaleX *= e.Delta.Scale;
PhotoTransform.ScaleY *= e.Delta.Scale;
// Constrain scale factor
PhotoTransform.ScaleX = Math.Min(PhotoTransform.ScaleX, 4.0);
PhotoTransform.ScaleY = Math.Min(PhotoTransform.ScaleY, 4.0);
PhotoTransform.ScaleX = Math.Max(PhotoTransform.ScaleX, 1.0);
PhotoTransform.ScaleY = Math.Max(PhotoTransform.ScaleY, 1.0);
}
private void LayoutRoot_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
// Reset the CompositeTransform
PhotoTransform.TranslateX = PhotoTransform.TranslateY = 0.0;
PhotoTransform.ScaleX = PhotoTransform.ScaleY = 1.0;
}
private void Photo_Tapped(object sender, TappedRoutedEventArgs e)
{
}
}
页:
[1]