Windows 8实用窍门系列:4.Windows 8的常用鼠标事件

  在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 = "鼠标进入范围";
        }
    }

  下图是效果演示,如需源码请点击 Win8Event.zip 下载.

posted @ 2012-10-15 11:32  .NET架构  阅读(2752)  评论(0编辑  收藏  举报