在Windows 8中采用一些新的鼠標事件以替代以前Silverlight的鼠標事件,其常用事件如下:
PointerWheelChanged:鼠標中鍵滑動事件。
PointerPressed:鼠標點擊下去的時候即觸發事件 。
PointerReleased:鼠標點擊下去的時候釋放鼠標時觸發事件。
PointerEntered:鼠標進入有 效范圍之時觸發一次。
PointerMoved:鼠標在有效范圍之內移動之時觸發事件。
PointerExited:鼠標退出有效范圍之時觸發事件。
在本文中將通過實例演示各種鼠標事件的響 應.下面我們看實例源碼如下:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Canvas Name="MainCanvas" Width="600" Height="300" Background="Cornsilk" PointerWheelChanged="MainCanvas_PointerWheelChanged" PointerReleased="MainCanvas_PointerReleased" PointerPressed="MainCanvas_PointerPressed" PointerMoved="MainCanvas_PointerMoved" PointerExited="MainCanvas_PointerExited" PointerEntered="MainCanvas_PointerEntered" ></Canvas> <TextBlock HorizontalAlignment="Left" Foreground="Gold" Margin="383,99,0,0" TextWrapping="Wrap" Name="tbLabel" Text="" VerticalAlignment="Top" Height="40" Width="418"/> <TextBlock HorizontalAlignment="Left" Foreground="Gold" Margin="774,99,0,0" TextWrapping="Wrap" Name="tbLabelNext" Text="" VerticalAlignment="Top" Height="40" Width="209"/> <TextBlock HorizontalAlignment="Left" Foreground="Gold" Margin="564,171,0,0" TextWrapping="Wrap" Name="tbLabelFore" Text="" VerticalAlignment="Top" Height="40" Width="209"/> </Grid>
其中Xaml.cs後台代碼如下,
/// <summary> /// 可用於自身或導航至 Frame 內部的空白頁。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } Point start = new Point(); /// <summary> /// 在此頁將要在 Frame 中顯示時進行調用。 /// </summary> /// <param name="e">描述如何訪問此頁的事件數據。Parameter /// 屬性通常用於配置頁。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { } //鼠標中鍵滑動事件 private void MainCanvas_PointerWheelChanged(object sender, PointerRoutedEventArgs e) { this.tbLabel.Text = "滑動了一次鼠標中箭"; } //鼠標點擊下去的事件 private void MainCanvas_PointerPressed(object sender, PointerRoutedEventArgs e) { start = e.GetCurrentPoint(MainCanvas).Position; this.tbLabel.Text = "點擊了一次鼠標或者屏幕"; } //鼠標松鍵事件 private void MainCanvas_PointerReleased(object sender, PointerRoutedEventArgs e) { Point end = e.GetCurrentPoint(MainCanvas).Position; double angle = 0; //判斷拖動鼠標角度 if (Math.Abs(end.X - start.X) < 1 && Math.Abs(end.Y - start.Y) < 1) { angle = 0; } else if (end.X > start.X) { if (end.Y > start.Y) { angle = 360 - Math.Atan((end.Y - start.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI; } else { angle = Math.Atan((start.Y - end.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI; } } else if (end.X < start.X) { if (end.Y > start.Y) { angle = Math.Atan((end.Y - start.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI + 180; } else { angle = 180 - Math.Atan((start.Y - end.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI; } } if (angle == 0) { this.tbLabel.Text = "點擊了一次鼠標或者屏幕"; } else if (angle >= 45 && angle < 135) { this.tbLabelFore.Text = "從下往上"; } else if (angle <= 45 || angle > 315) { this.tbLabelFore.Text = "從左向右滑"; } else if (angle >= 135 && angle < 225) { this.tbLabelFore.Text = "從右向左滑"; } else if (angle >= 225 && angle < 315) { this.tbLabelFore.Text = "從上往下"; } } //鼠標移動事件 private void MainCanvas_PointerMoved(object sender, PointerRoutedEventArgs e) { Point point = e.GetCurrentPoint(MainCanvas).Position; this.tbLabel.Text = "鼠標正在移動X:"+point.X+",Y:"+point.Y; } //鼠標移出范圍事件 private void MainCanvas_PointerExited(object sender, PointerRoutedEventArgs e) { this.tbLabelNext.Text = "鼠標移出作用范圍"; } //鼠標進入范圍事件 private void MainCanvas_PointerEntered(object sender, PointerRoutedEventArgs e) { this.tbLabelNext.Text = "鼠標進入范圍"; } }
下圖是效果演示,如需源碼請下載:http://files.cnblogs.com/chengxingliang/Win8Event.zip