专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »博文摘选 » silverlight鼠标事件:跟老外学Silverlight游戏 的十一 鼠标的新衣 »正文

silverlight鼠标事件:跟老外学Silverlight游戏 的十一 鼠标的新衣

来源: 发布时间:星期六, 2010年1月9日 浏览:0次 评论:0
       本篇我们将对鼠标指针进行美化也给它穿上好看点马甲对于其样式来源可以选择Image 或Path可以通过微软 Expression Design 设计出鼠标指针样式arrow

102709_1407_HiddenObjec1   

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

 

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

addimage

 

2. 在Interactivity 中创建MouseCursor 文件夹并在其中加入MouseCursorBehaviorNameResolvedEventArgsNameResolver 类:

add<img src='/icons/89713class.gif' /> 

 

3. 在NameResolver 类中最关键就是UpdateObjectFromName 思路方法它将CursorName 属性和鼠标指针对象结合起来:

private void UpdateObjectFromName(DependencyObject oldObject) { DependencyObject resolvedObject = null; this.ResolvedObject = null; (this.NameScopeReferenceElement != null) { (!IsElementLoaded(this.NameScopeReferenceElement)) { this.NameScopeReferenceElement.Loaded
RoutedEventHandler(this.OnNameScopeReferenceLoaded); this.PendingReferenceElementLoad = true; ; } (!.IsNullOrEmpty(this.Name)) { FrameworkElement actualNameScopeReferenceElement =
this.ActualNameScopeReferenceElement; (actualNameScopeReferenceElement != null) { resolvedObject = actualNameScopeReferenceElement.FindName(this.Name)
as DependencyObject; } } } this.HasAttempedResolve = true; this.ResolvedObject = resolvedObject; (oldObject != this.Object) { this.OnObjectChanged(oldObject, this.Object); } }

 

4. 在MouseCursorBehavior 类中存有CursorName、OffX、OffY 属性它们将用于在Blend 中对鼠标指针进行设置:

public readonly DependencyProperty CursorNameProperty = DependencyProperty.Register("CursorName", typeof(), typeof(MouseCursorBehavior), PropertyMetadata( PropertyChangedCallback(OnCursorNameChanged))); public readonly DependencyProperty OffXProperty = DependencyProperty.Register("OffX", typeof(double), typeof(MouseCursorBehavior), null); public readonly DependencyProperty OffYProperty = DependencyProperty.Register("OffY", typeof(double), typeof(MouseCursorBehavior), null);

 

以及MouseEnter、MouseLeave、MouseMove 事件:

private void AssociatedObject_MouseEnter(object sender, MouseEventArgs e) { (!this.IsCursorNameSet) ; FrameworkElement cursor = Cursor as FrameworkElement; cursor.IsHitTestVisible = false; cursor.Visibility = Visibility.Visible; (CursorStack.Count > 0 && CursorStack.Peek != cursor) { CursorStack.Peek.Visibility = Visibility.Collapsed; } (!CursorStack.Contains(cursor)) CursorStack.Push(cursor); AssociatedObject.Cursor = Cursors.None; this.AssociatedObject.MouseMove MouseEventHandler(AssociatedObject_MouseMove); } private void AssociatedObject_MouseLeave(object sender, MouseEventArgs e) { (!this.IsCursorNameSet) ; FrameworkElement cursor = Cursor as FrameworkElement; cursor.Visibility = Visibility.Collapsed; CursorStack.Pop; (CursorStack.Count > 0) { CursorStack.Peek.Visibility = Visibility.Visible; } this.AssociatedObject.MouseMove -= MouseEventHandler(AssociatedObject_MouseMove); AssociatedObject.Cursor = null; } private void AssociatedObject_MouseMove(object sender, MouseEventArgs e) { (!this.IsCursorNameSet) ; FrameworkElement cursor = Cursor as FrameworkElement; Po mousePosition = e.GetPosition(null); cursor.Margin = Thickness(mousePosition.X + OffX, mousePosition.Y + OffY, 0, 0); }

 

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

addbehavior

 

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

image

将其放在GameScreen 上方Left 设为0Top 设为-50:

position

lay

 

7. 选择刚刚添加MouseCursorBehavior 将CursorName 设为鼠标指针名称cursorArrowOffY 设为50(的前它和LayoutRoot有-50偏差):

 

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


 作者: Gnie  出处: {GnieTech} (http://www.cnblogs.com/gnielee/)  版权声明: 本文版权归作者和博客园共有转载时须注明本文详细链接否则作者将保留追究其法律责任


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: