这次给大家聊聊如何获取鼠标滚轮事件.

鼠标滚轮事件 在当前版本beta2中并没有 但是我们可以借助 Htmlpage 对象 HtmlPage(System.Windows.Browser;)(之前也多次提到过他如何捕捉Silverlight右键点击事件,如何在Silverlight中使用Cookie ) 实现此功能

 
  1.  HtmlPage.Window.AttachEvent(“DOMMouseScroll”, OnMouseWheel); 
  2.  HtmlPage.Window.AttachEvent(“onmousewheel”, OnMouseWheel); 
  3.  HtmlPage.Document.AttachEvent(“onmousewheel”, OnMouseWheel);         
  4.  
  5.  private void OnMouseWheel(object sender, HtmlEventArgs args) 
  6.  

之后我们要在 方法中获取一个旋转角度属性

但是在不同浏览器中 这个属性的名称有些不同

根据这个角度我们可以得知鼠标正在向上或是向下滚动


  1. double mouseDelta = 0; 
  2. ScriptObject e = args.EventObject; 
  3. if (e.GetProperty(“detail”) != null
  4. {// 火狐和苹果 
  5.     mouseDelta = ((double)e.GetProperty(“detail”)); 
  6. }     
  7. else if (e.GetProperty(“wheelDelta”) != null
  8. {// IE 和 Opera     
  9.     mouseDelta = ((double)e.GetProperty(“wheelDelta”)); 
  10. mouseDelta = Math.Sign(mouseDelta); 
  11. if (mouseDelta > 0)  
  12.     txt.Text = “向上滚动”
  13. else if (mouseDelta<0)  
  14.     txt.Text = “向下滚动”

接下来 再给大家聊聊 如何获取键盘的组合键(比如我们经常按住ctrl+鼠标点击 或者 ctrl+enter)

其实 我们只要用到一个枚举值


  1. namespace System.Windows.Input 
  2.     // Summary: 
  3.     //     Specifies the set of modifier keys. 
  4.     [Flags] 
  5.     public enum ModifierKeys 
  6.     { 
  7.         // Summary: 
  8.         //     No modifiers are pressed. 
  9.         None = 0, 
  10.         // 
  11.         // Summary: 
  12.         //     The ALT key is pressed. 
  13.         Alt = 1, 
  14.         // 
  15.         // Summary: 
  16.         //     The CTRL key is pressed. 
  17.         Control = 2, 
  18.         // 
  19.         // Summary: 
  20.         //     The SHIFT key is pressed. 
  21.         Shift = 4, 
  22.         // 
  23.         // Summary: 
  24.         //     The Windows logo key is pressed. 
  25.         Windows = 8, 
  26.         // 
  27.         // Summary: 
  28.         //     The Apple key (also known as the “Open Apple key”is pressed. 
  29.         Apple = 8, 
  30.     } 

具体如何方法

好比我们现在页面注册一个点击事件


  1. this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown); 
  2. void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  3.  {} 

我们需要在里面做一点儿小操作就可以判断用户是否还在按住了键盘上的某个按键


  1. ModifierKeys keys = Keyboard.Modifiers; 
  2. txt.Text = “”
  3. if ((keys & ModifierKeys.Shift) != 0) 
  4.     txt.Text += “shift”
  5. if ((keys & ModifierKeys.Alt) != 0) 
  6.     txt.Text += “alt”
  7. if ((keys & ModifierKeys.Apple) != 0) 
  8.     txt.Text += “apple”
  9. if ((keys & ModifierKeys.Control) != 0) 
  10.     txt.Text += “ctrl”
  11. if ((keys & ModifierKeys.Windows) != 0) 
  12.     txt.Text += “windows”
  13. txt.Text += ” + 鼠标点击”

以上是个人总结的一点小技巧而已~ 希望这点技巧对你有所帮助^^

Source code: Mouse_Wheel_keys_Event Deom