Silverlight 自定义光标 Custom Cursor

Silverlight 自定义光标 Custom Cursor



其实很简单,将在Load的时候将光标设置为none值,在前台随便画一个东西,鼠标移动的时候跟随就行

< Rectangle  Width ="20"  Height ="20"  x:Name ="cursor" >
    
< Rectangle.Triggers >
      
< EventTrigger  RoutedEvent ="Canvas.Loaded" >
        
< BeginStoryboard >
          
< Storyboard >
            
< DoubleAnimation
              
Storyboard.TargetName ="rotate"  Storyboard.TargetProperty ="Angle"
              By
="360"  Duration ="00:00:03"  RepeatBehavior ="Forever" />
          
</ Storyboard >
        
</ BeginStoryboard >
      
</ EventTrigger >
    
</ Rectangle.Triggers >
    
< Rectangle.RenderTransform >
      
< RotateTransform  x:Name ="rotate"  CenterX ="10"  CenterY ="10" />
    
</ Rectangle.RenderTransform >
    
< Rectangle.Fill >
      
< LinearGradientBrush >
        
< GradientStop  Color ="White"  Offset ="0" />
        
< GradientStop  Color ="Red"  Offset =".5" />
        
< GradientStop  Color ="Black"  Offset ="1" />
      
</ LinearGradientBrush >
    
</ Rectangle.Fill >
  
</ Rectangle >

public   void  Page_Loaded( object  o, EventArgs e)  {
            
// Required to initialize variables
            InitializeComponent();
            
this.Cursor = Cursors.None;
        }


public   void  OnMouseMove( object  sender, MouseEventArgs e)  {
            
double x = e.GetPosition(this).X;
            
double y = e.GetPosition(this).Y;

            
if (x <= 20 || x >= this.Width - 20 || y <= 20 || y >= this.Height - 20)
                
this.cursor.Visibility = Visibility.Collapsed;
            
else
                
this.cursor.Visibility = Visibility.Visible;

            
this.cursor.SetValue<double>(Canvas.LeftProperty, x);
            
this.cursor.SetValue<double>(Canvas.TopProperty, y);
        }

你可能感兴趣的:(silverlight)