The Stars ...My Destination

adamxx

天下事,法无定法,然后知非法法之
世间人,尤了未了,何妨以不了了之
posts - 50, comments - 36, trackbacks - 10, articles - 0

Silverlight 自定义光标 Custom Cursor

Posted on 2008-01-10 17:36 adamxx 阅读(1842) 评论(3)  编辑 收藏 所属分类: Silverlight

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

Feedback

#1楼    回复  引用  查看    

2008-01-10 19:28 by Ariel Y.      
滚轮滚动的时候有些顿,不知道又没有性能更好的方法。

也许以后会内建支持,也许永远也不会。

#2楼    回复  引用    

2008-01-10 23:47 by 2jno [未注册用户]
强呀,那是相当的不错

#3楼    回复  引用    

2008-09-01 22:32 by ooxxxxxxxxx [未注册用户]
好土的办法。

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: