在Windows 8中采用一些新的鼠標(biāo)事件以替代以前Silverlight的鼠標(biāo)事件,其常用事件如下:
PointerWheelChanged:鼠標(biāo)中鍵滑動(dòng)事件。
PointerPressed:鼠標(biāo)點(diǎn)擊下去的時(shí)候即觸發(fā)事件。
PointerReleased:鼠標(biāo)點(diǎn)擊下去的時(shí)候釋放鼠標(biāo)時(shí)觸發(fā)事件。
PointerEntered:鼠標(biāo)進(jìn)入有效范圍之時(shí)觸發(fā)一次。
PointerMoved:鼠標(biāo)在有效范圍之內(nèi)移動(dòng)之時(shí)觸發(fā)事件。
PointerExited:鼠標(biāo)退出有效范圍之時(shí)觸發(fā)事件。
在本文中將通過實(shí)例演示各種鼠標(biāo)事件的響應(yīng).下面我們看實(shí)例源碼如下:
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后臺(tái)代碼如下,
/// summary>
/// 可用于自身或?qū)Ш街?Frame 內(nèi)部的空白頁。
/// /summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
Point start = new Point();/p>
p> /// summary>
/// 在此頁將要在 Frame 中顯示時(shí)進(jìn)行調(diào)用。
/// /summary>
/// param name="e">描述如何訪問此頁的事件數(shù)據(jù)。Parameter
/// 屬性通常用于配置頁。/param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{/p>
p> }/p>
p> //鼠標(biāo)中鍵滑動(dòng)事件
private void MainCanvas_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
this.tbLabel.Text = "滑動(dòng)了一次鼠標(biāo)中箭";
}/p>
p> //鼠標(biāo)點(diǎn)擊下去的事件
private void MainCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
start = e.GetCurrentPoint(MainCanvas).Position;
this.tbLabel.Text = "點(diǎn)擊了一次鼠標(biāo)或者屏幕";/p>
p> }/p>
p> //鼠標(biāo)松鍵事件
private void MainCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
Point end = e.GetCurrentPoint(MainCanvas).Position;
double angle = 0;
//判斷拖動(dòng)鼠標(biāo)角度
if (Math.Abs(end.X - start.X) 1 Math.Abs(end.Y - start.Y) 1)
{
angle = 0;
}
else if (end.X > start.X)
{/p>
p> 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 = "點(diǎn)擊了一次鼠標(biāo)或者屏幕";
}
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 = "從上往下";
}/p>
p> }
//鼠標(biāo)移動(dòng)事件
private void MainCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Point point = e.GetCurrentPoint(MainCanvas).Position;
this.tbLabel.Text = "鼠標(biāo)正在移動(dòng)X:"+point.X+",Y:"+point.Y;
}/p>
p> //鼠標(biāo)移出范圍事件
private void MainCanvas_PointerExited(object sender, PointerRoutedEventArgs e)
{
this.tbLabelNext.Text = "鼠標(biāo)移出作用范圍";
}/p>
p> //鼠標(biāo)進(jìn)入范圍事件
private void MainCanvas_PointerEntered(object sender, PointerRoutedEventArgs e)
{
this.tbLabelNext.Text = "鼠標(biāo)進(jìn)入范圍";
}
}
下圖是效果演示,如需源碼請(qǐng)點(diǎn)擊 Win8Event.zip 下載