这一篇主要是通过实例讲解对Silverlight中鼠标事件的处理进行系统全面的学习性总结。
Silverlight中鼠标事件主要有:
1、MouseEnter
2、MouseLeave
3、MouseLeftButtonDown
4、MouseLeftButtonUp
5、MouseMove
接下来通过例子讲解,首先新建一个Silverlight应用程序
出现下图,选择默认完成即可
现在开始对MainPage文件进行操作,这边我们主要通过两个按钮来实现这五个事件处理。
首先打开MainPage.xaml文件进行编辑,代码如下:
< UserControl x:Class = " Silverlightkey.MainPage "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d = " http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 "
mc:Ignorable = " d "
d:DesignHeight = " 800 " d:DesignWidth = " 600 " >
< Canvas >
< Button MouseLeftButtonDown = " Button_MouseLeftButtonDown "
MouseLeftButtonUp = " Button_MouseLeftButtonUp "
MouseMove = " Button_MouseMove "
Canvas.Left = " 50 "
Canvas.Top = " 50 "
Background = " Red "
FontSize = " 25 "
Width = " 200 "
Height = " 100 "
Name = " button1 "
Content = " drag me " >
</ Button >
< Button Canvas.Left = " 350 "
Canvas.Top = " 50 "
Content = " enter me "
Height = " 100 "
Name = " button2 "
Width = " 200 "
Background = " Blue "
FontSize = " 25 "
MouseEnter = " button2_MouseEnter "
MouseLeave = " button2_MouseLeave " />
</ Canvas >
</ UserControl >
然后对后台文件MainPage.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 Silverlightkey
{
public partial class MainPage : UserControl
{
bool trackingMouseMove = false ;
Point mousePosition;
public MainPage()
{
InitializeComponent();
button1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Button_MouseLeftButtonDown), true );
button1.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Button_MouseLeftButtonUp), true );
}
private void Button_MouseLeftButtonDown( object sender, MouseButtonEventArgs e)
{
mousePosition = e.GetPosition( null );
trackingMouseMove = true ;
}
private void Button_MouseLeftButtonUp( object sender, MouseButtonEventArgs e)
{
trackingMouseMove = false ;
}
private void Button_MouseMove( object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
double moveH = e.GetPosition( null ).Y - mousePosition.Y;
double moveW = e.GetPosition( null ).X - mousePosition.X;
double newTop = moveH + ( double )element.GetValue(Canvas.TopProperty);
double newLeft = moveW + ( double )element.GetValue(Canvas.LeftProperty);
element.SetValue(Canvas.TopProperty, newTop);
element.SetValue(Canvas.LeftProperty, newLeft);
mousePosition = e.GetPosition( null );
}
}
private void button2_MouseEnter( object sender, MouseEventArgs e)
{
button2.Content = " leave me " ;
}
private void button2_MouseLeave( object sender, MouseEventArgs e)
{
button2.Content = " enter me " ;
}
}
}
编译出现如下效果:
左边的按钮实现MouseLeftButtonDown、MouseLeftButtonUp、MouseMove
右边的按钮实现MouseEnter、MouseLeave
鼠标放到左边按钮按住拖动,效果如下:
鼠标放到右边按钮,按钮文本改变,效果如下:
这里面值得注意的地方是后台代码初始化中的
button1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Button_MouseLeftButtonDown), true);
button1.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Button_MouseLeftButtonUp), true);
这里面要加上这两个为指定的路由事件添加路由事件处理程序,因为MouseLeftButtonDown的EventHandler被Button本身的Click事件占掉了,所以需要手动添加
至此,鼠标事件处理的基础知识讲解完毕!