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

鼠标滚轮事件 在当前版本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. { 
  7.  
  8. } 

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

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

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


  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. } 
  11. mouseDelta = Math.Sign(mouseDelta); 
  12. if (mouseDelta > 0)  
  13. { 
  14.     txt.Text = “向上滚动”; 
  15. } 
  16. else if (mouseDelta<0)  
  17. { 
  18.     txt.Text = “向下滚动”; 
  19. } 

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

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


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

具体如何方法

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


  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