The Stars ...My Destination

adamxx

天下事,法无定法,然后知非法法之
世间人,尤了未了,何妨以不了了之

导航

Silverlight 自定义光标 Custom Cursor

Posted on 2008-01-10 17:36  adamxx  阅读(4337)  评论(3编辑  收藏  举报

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);
        }