稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件

稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件,第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(13) - 2.0交互之鼠标事件键盘事件 作者: webabcd 介绍 Silverlight 2.0 人机交互:响应用户的鼠标 *** 作和键盘 *** 作     MouseEnter - 鼠标进入时触发的事件(显然,此事件不能冒泡)     MouseLeave - 鼠标离开时触发的事件(显然,此事件不能冒泡)     MouseLeftButto [索引页]
[源码下载]


稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件

作者: webabcd


介绍
Silverlight 2.0 人机交互:响应用户的鼠标 *** 作和键盘 *** 作
    MouseEnter - 鼠标进入时触发的事件(显然,此事件不能冒泡)
    MouseLeave - 鼠标离开时触发的事件(显然,此事件不能冒泡)
    MouseleftbuttonDown - 鼠标左键单击按下时触发的事件
    MouseleftbuttonUp - 鼠标左键单击按下并放开时触发的事件
    MouseMove - 鼠标移动时触发的事件
    MouseEventArgs.Getposition() - 鼠标相对于指定元素的坐标
    MousebuttonEventArgs.Handled - 此事件是否已被处理
    KeyDown - 鼠标按下时触发的事件
    KeyUp - 鼠标按下并放开时触发的事件
    KeyEventArgs.Key - 与事件相关的键盘的按键 [System.windows.input.Key枚举]
    KeyEventArgs.Handled - 是否处理过此事件
    System.windows.input.Keyboard.ModifIErs - 当前按下的辅助键 [System.windows.input.ModifIErKeys枚举]


在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html  


示例
1、Mouse.xaml <UserControl x:Class="Silverlight20.Interactive.Mouse"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        
        <!--路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件-->
        
        <!--
        MouseleftbuttonDown,MouseleftbuttonUp和MouseMove均为向上冒泡的路由事件
        本例的事件路由为:Ellipse -> StackPanel -> UserControl 或 Rectangle -> Canvas -> StackPanel -> UserControl
        如果不想向上冒泡,则可以使用 MousebuttonEventArgs.Handled = true 告知事件已被处理
        -->
        <StackPanel HorizontalAlignment="left" MouseleftbuttonDown="StackPanel_MouseleftbuttonDown" >

                <!--
                MouseEnter - 鼠标进入时触发的事件(显然,此事件不能冒泡)
                MouseLeave - 鼠标离开时触发的事件(显然,此事件不能冒泡)
                
                MouseleftbuttonDown - 鼠标左键单击按下时触发的事件
                MouseleftbuttonUp - 鼠标左键单击按下并放开时触发的事件
                MouseMove - 鼠标移动时触发的事件
                -->
                <Ellipse x:name="ellipse" WIDth="200" Height="100" Fill="Red" margin="5"    
                        MouseEnter="ellipse_MouseEnter"    
                        MouseLeave="ellipse_MouseLeave"
                        MouseleftbuttonDown="ellipse_MouseleftbuttonDown"
                        MouseleftbuttonUp="ellipse_MouseleftbuttonUp"
                >
                </Ellipse>

                <Canvas margin="5">

                        <!--用于演示拖放的矩形-->
                        <Rectangle x:name="rectangle" Fill="Blue" WIDth="50" Height="50"
                                MouseleftbuttonDown="rectangle_MouseleftbuttonDown"
                                MouseleftbuttonUp="rectangle_MouseleftbuttonUp"
                                MouseMove="rectangle_MouseMove"
                        />
                        
                </Canvas>

        </StackPanel>
</UserControl>   Mouse.xaml.cs

using System;

using System.Collections.Generic;

using System.linq;

using System.Net;

using System.windows;

using System.windows.Controls;

using System.windows.documents;

using System.windows.input;

using System.windows.Media;

using System.windows.Media.Animation;

using System.windows.Shapes;


namespace Silverlight20.Interactive

{

         public partial class Mouse : UserControl

        {

                 public Mouse()

                {

                        InitializeComponent();

                }


                 voID ellipse_MouseEnter( object sender,MouseEventArgs e)

                {

                        ellipse.Fill = new SolIDcolorBrush(colors.Yellow);

                }


                 voID ellipse_MouseLeave( object sender,MouseEventArgs e)

                {

                        ellipse.Fill = new SolIDcolorBrush(colors.Red);

                }


                 private voID ellipse_MouseleftbuttonUp( object sender,MousebuttonEventArgs e)

                {

                        ellipse.Fill = new SolIDcolorBrush(colors.Yellow);

                }


                 private voID ellipse_MouseleftbuttonDown( object sender,MousebuttonEventArgs e)

                {

                        ellipse.Fill = new SolIDcolorBrush(colors.Blue);


                         // MousebuttonEventArgs.Handled - 此事件是否已被处理

                         //         false - 未被处理,事件的路由为向上冒泡

                         //         true - 已被处理,事件的路由为不再冒泡

                        e.Handled = true;

                }


                 private voID StackPanel_MouseleftbuttonDown( object sender,MousebuttonEventArgs e)

                {

                         // 如果鼠标单击 rectangle 对象,则 会 执行到此句

                         // 如果鼠标单击 ellipse 对象,则 不会 执行到此句,因为之前 ellipse 对象的 MouseleftbuttonDown 事件中已经设置 e.Handled = true ,所以事件不会冒泡至此

                        ellipse.Fill = new SolIDcolorBrush(colors.Black);

                }

                



                 // 是否正在捕获鼠标

                 private bool _isMouseCaptured;


                 // 鼠标垂直方向上的坐标

                 private double _mouseY;


                 // 鼠标水平方向上的坐标

                 private double _mouseX;


                 private voID rectangle_MouseleftbuttonDown( object sender,MousebuttonEventArgs e)

                {

                         // MousebuttonEventArgs.Getposition() - 鼠标相对于指定元素的坐标

                        _mouseY = e.Getposition( null).Y;

                        _mouseX = e.Getposition( null).X;


                         // CaptureMouse() - 在指定的元素上捕获鼠标

                        rectangle.CaptureMouse();

                        _isMouseCaptured = true;

                }


                 public voID rectangle_MouseMove( object sender,MouseEventArgs e)

                {

                         if (_isMouseCaptured)

                        {

                                 // 移动前和移动后的鼠标 垂直方向 和 水平方向 的位置的差值

                                 double v = e.Getposition( null).Y - _mouseY;

                                 double h = e.Getposition( null).X - _mouseX;


                                 // 移动后的 rectangle 对象相对于 Canvas 的坐标

                                 double newtop = v + ( double)rectangle.GetValue(Canvas.topProperty);

                                 double newleft = h + ( double)rectangle.GetValue(Canvas.leftProperty);


                                 // 设置 rectangle 对象的位置为新的坐标.

                                rectangle.SetValue(Canvas.topProperty,newtop);

                                rectangle.SetValue(Canvas.leftProperty,newleft);


                                 // 更新鼠标的当前坐标

                                _mouseY = e.Getposition( null).Y;

                                _mouseX = e.Getposition( null).X;

                        }

                }


                 private voID rectangle_MouseleftbuttonUp( object sender,MousebuttonEventArgs e)

                {

                         // ReleaseMouseCapture() - 如果指定的元素具有鼠标捕获,则释放该捕获

                        rectangle.ReleaseMouseCapture();

                        _isMouseCaptured = false;

                }

        }

}     2、Keyboard.xaml
<!--
KeyDown - 鼠标按下时触发的事件
KeyUp - 鼠标按下并放开时触发的事件
-->
<!--
KeyDown和KeyUp均为向上冒泡的路由事件,本例的事件路由为:TextBox -> Canvas -> UserControl
-->
<UserControl x:Class="Silverlight20.Interactive.Keyboard"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        x:name="userControl"
        KeyDown="userControl_KeyDown">
        
        <Canvas>
                        
                <TextBox x:name="textBox" Text="TextBox" />
                
        </Canvas>
        
</UserControl>   Keyboard.xaml.cs

using System;

using System.Collections.Generic;

using System.linq;

using System.Net;

using System.windows;

using System.windows.Controls;

using System.windows.documents;

using System.windows.input;

using System.windows.Media;

using System.windows.Media.Animation;

using System.windows.Shapes;


namespace Silverlight20.Interactive

{

         public partial class Keyboard : UserControl

        {

                 public Keyboard()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(Keyboard_Loaded);


                         // 为 UserControl 注册 KeyUp 事件

                        userControl.KeyUp += new KeyEventHandler(userControl_KeyUp);

                }


                 voID Keyboard_Loaded( object sender,RoutedEventArgs e)

                {

                         // 让 UserControl 获得焦点,这样该 UserControl 内的元素才能监听到键盘事件

                        userControl.Focus();

                }


                 private voID userControl_KeyDown( object sender,KeyEventArgs e)

                {

                         // 获取 textBox 对象的相对于 Canvas 的 x坐标 和 y坐标

                         double x = ( double)textBox.GetValue(Canvas.leftProperty);

                         double y = ( double)textBox.GetValue(Canvas.topProperty);


                         // KeyEventArgs.Key - 与事件相关的键盘的按键 [System.windows.input.Key枚举]

                         switch (e.Key)

                        {

                                 // 按 Up 键后 textBox 对象向 上 移动 1 个像素

                                 // Up 键所对应的 e.PlatformKeyCode == 38    

                                 // 当获得的 e.Key == Key.UnkNown 时,可以使用 e.PlatformKeyCode 来确定用户所按的键

                                 case Key.Up:

                                        textBox.SetValue(Canvas.topProperty,y - 1);

                                         break;


                                 // 按 Down 键后 textBox 对象向 下 移动 1 个像素

                                 // Down 键所对应的 e.PlatformKeyCode == 40

                                 case Key.Down:

                                        textBox.SetValue(Canvas.topProperty,y + 1);

                                         break;


                                 // 按 left 键后 textBox 对象向 左 移动 1 个像素

                                 // left 键所对应的 e.PlatformKeyCode == 37

                                 case Key.left:

                                        textBox.SetValue(Canvas.leftProperty,x - 1);

                                         break;


                                 // 按 Right 键后 textBox 对象向 右 移动 1 个像素

                                 // Right 键所对应的 e.PlatformKeyCode == 39    

                                 case Key.Right:

                                        textBox.SetValue(Canvas.leftProperty,x + 1);

                                         break;


                                 default:

                                         break;

                        }


                         // 同上:Key.W - 向上移动; Key.S - 向下移动; Key.A - 向左移动; Key.D - 向右移动

                         switch (e.Key)

                        {

                                 // KeyEventArgs.Handled - 是否处理过此事件


                                 // 如果在文本框内敲 W ,那么文本框会向上移动,而且文本框内也会被输入 W

                                 // 如果只想移动文本框,而不输入 W ,那么可以设置 KeyEventArgs.Handled = true 告知此事件已经被处理完毕

                                 case Key.W:

                                        textBox.SetValue(Canvas.topProperty,y - 1);

                                        e.Handled = true;

                                         break;

                                 case Key.S:

                                        textBox.SetValue(Canvas.topProperty,y + 1);

                                        e.Handled = true;

                                         break;

                                 case Key.A:

                                        textBox.SetValue(Canvas.leftProperty,x - 1);

                                        e.Handled = true;

                                         break;

                                 case Key.D:

                                        textBox.SetValue(Canvas.leftProperty,x + 1);

                                        e.Handled = true;

                                         break;

                                 default:

                                         break;

                        }

                }


                 private voID userControl_KeyUp( object sender,KeyEventArgs e)

                {

                         /*

                        System.windows.input.Keyboard.ModifIErs - 当前按下的辅助键 [System.windows.input.ModifIErKeys枚举]

                                ModifIErKeys.None - 无

                                ModifIErKeys.Alt - Alt 键

                                ModifIErKeys.Control - Ctrl 键

                                ModifIErKeys.Shift - Shift 键

                                ModifIErKeys.windows - windows 键

                                ModifIErKeys.Apple - Apple 键(苹果电脑)

                        */


                         // 按 Ctrl + M 则将 textBox 的位置设置为其初始位置

                         if (System.windows.input.Keyboard.ModifIErs == ModifIErKeys.Control && e.Key == Key.M)

                        {

                                textBox.SetValue(Canvas.leftProperty,0d);

                                textBox.SetValue(Canvas.topProperty,0d);

                        }

                }

        }

}     OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件全部内容,希望文章能够帮你解决稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存