Silverlight 解谜游戏 之十一 鼠标的新衣

Silverlight 解谜游戏 之十一 鼠标的新衣,第1张

概述       本篇我们将对鼠标指针进行美化,也给它穿上好看点的马甲,对于其样式来源可以选择Image 或Path。可以通过微软的 Expression Design 设计出鼠标指针样式。     先看看鼠标指针换上新装的效果:   1. 将鼠标指针图片加入Images 文件夹: 2. 在Interactivity 中创建MouseCursor 文件夹,并在其中加入MouseCursorBehavi

       本篇我们将对鼠标指针进行美化,也给它穿上好看点的马甲,对于其样式来源可以选择Image 或Path。可以通过微软的 Expression Design 设计出鼠标指针样式

   

先看看鼠标指针换上新装的效果:

 

1. 将鼠标指针图片加入Images 文件夹:

2. 在Interactivity 中创建MouseCursor 文件夹,并在其中加入MouseCursorBehavior、nameResolvedEventArgs、nameResolver 类:

3. 在nameResolver 类中,最关键的就是UpdateObjectFromname 方法,它将Cursorname 属性与鼠标指针对象结合起来:

private voID UpdateObjectFromname(DependencyObject oldobject)  {     DependencyObject resolvedobject = null;     this.Resolvedobject = null;       if (this.nameScopeReferenceElement != null)     {        if (!IsElementLoaded(this.nameScopeReferenceElement))        {            this.nameScopeReferenceElement.Loaded += 
new RoutedEventHandler(this.OnnameScopeReferenceLoaded); this.PendingReferenceElementLoad = true; return; } if (!string.IsNullOrEmpty(this.name)) { FrameworkElement actualnameScopeReferenceElement =
this.ActualnameScopeReferenceElement; if (actualnameScopeReferenceElement != null) { resolvedobject = actualnameScopeReferenceElement.Findname(this.name)
as DependencyObject; } } } this.HasAttempedResolve = true; this.Resolvedobject = resolvedobject; if (oldobject != this.Object) { this.OnObjectChanged(oldobject,this.Object); } }

4. 在MouseCursorBehavior 类中,存有Cursorname、OffsetX、OffsetY 属性,它们将用于在Blend 中对鼠标指针进行设置:

public static Readonly DependencyProperty CursornameProperty =     DependencyProperty.Register("Cursorname",typeof(string),typeof(MouseCursorBehavior),new PropertyMetadata(new PropertyChangedCallback(OnCursornameChanged)));    public static Readonly DependencyProperty OffsetXProperty =    DependencyProperty.Register("OffsetX",typeof(double),null);    public static Readonly DependencyProperty OffsetYProperty =    DependencyProperty.Register("OffsetY",null);

以及MouseEnter、MouseLeave、MouseMove 事件:

private voID Associatedobject_MouseEnter(object sender,MouseEventArgs e)  {     if (!this.IsCursornameSet)        return;       FrameworkElement cursor = Cursor as FrameworkElement;     cursor.IsHitTestVisible = false;     cursor.Visibility = Visibility.Visible;       if (CursorStack.Count > 0 && CursorStack.Peek() != cursor)     {        CursorStack.Peek().Visibility = Visibility.Collapsed;     }       if (!CursorStack.Contains(cursor))        CursorStack.Push(cursor);       Associatedobject.Cursor = Cursors.None;       this.Associatedobject.MouseMove += new MouseEventHandler(Associatedobject_MouseMove);  }    private voID Associatedobject_MouseLeave(object sender,MouseEventArgs e)  {     if (!this.IsCursornameSet)        return;       FrameworkElement cursor = Cursor as FrameworkElement;     cursor.Visibility = Visibility.Collapsed;       CursorStack.Pop();       if (CursorStack.Count > 0)     {        CursorStack.Peek().Visibility = Visibility.Visible;     }       this.Associatedobject.MouseMove -= new MouseEventHandler(Associatedobject_MouseMove);     Associatedobject.Cursor = null;  }    private voID Associatedobject_MouseMove(object sender,MouseEventArgs e)  {     if (!this.IsCursornameSet)        return;       FrameworkElement cursor = Cursor as FrameworkElement;       Point mouseposition = e.Getposition(null);     cursor.margin = new Thickness(mouseposition.X + OffsetX,mouseposition.Y + OffsetY,0);    }

5. 添加好上面三个类并重新编译后,我们回到Blend 中,为UserControl 添加新的Behavior->MouseCursorBehavior:

6. 在LayoutRoot 中添加鼠标指针图片,命名为cursorArrow:

将其放在GameScreen 上方,left 设为0,top 设为-50:

7. 选择刚刚添加的MouseCursorBehavior 将Cursorname 设为鼠标指针名称cursorArrow,OffsetY 设为50(因为之前它与LayoutRoot有-50的偏差):

运行程序便可看到新的鼠标指针效果,源代码下载:

总结

以上是内存溢出为你收集整理的Silverlight 解谜游戏 之十一 鼠标的新衣全部内容,希望文章能够帮你解决Silverlight 解谜游戏 之十一 鼠标的新衣所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存