silverlight - 获取鼠标滚轮事件 及 判断获取组合键的方法

来源:互联网 发布:17年网络群体事件 编辑:程序博客网 时间:2024/06/03 13:36

http://www.cnblogs.com/xh831213/archive/2010/08/05/1792866.html

 

可以借助 Htmlpage 对象 HtmlPage(System.Windows.Browser;)

HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel);
            HtmlPage.Window.AttachEvent(
"onmousewheel", OnMouseWheel);
            HtmlPage.Document.AttachEvent(
"onmousewheel", OnMouseWheel);        

            
private void OnMouseWheel(object sender, HtmlEventArgs args)
           {

           }

 

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

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

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

 

            double mouseDelta = 0;
            ScriptObject e 
= args.EventObject;
            
if (e.GetProperty("detail"!= null)
            {
// 火狐和苹果
                mouseDelta = ((double)e.GetProperty("detail"));
            }    
            
else if (e.GetProperty("wheelDelta"!= null)
            {
// IE 和 Opera    
                mouseDelta = ((double)e.GetProperty("wheelDelta"));
            }
            mouseDelta 
= Math.Sign(mouseDelta);
            
if (mouseDelta > 0
            {
                txt.Text 
= "向上滚动";
            }
            
else if (mouseDelta<0
            {
                txt.Text 
= "向下滚动";
            }

 

在向上向下滚动的时候可以加入鼠标坐标的判断,以便确定鼠标在那个控件上,可以执行不同的操作.

 

应用程序的“允许在浏览器外运行应用程序” 不选

 

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

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

namespace System.Windows.Input
{
    
// Summary:
    
//     Specifies the set of modifier keys.
    [Flags]
    
public enum ModifierKeys
    {
        
// Summary:
        
//     No modifiers are pressed.
        None = 0,
        
//
        
// Summary:
        
//     The ALT key is pressed.
        Alt = 1,
        
//
        
// Summary:
        
//     The CTRL key is pressed.
        Control = 2,
        
//
        
// Summary:
        
//     The SHIFT key is pressed.
        Shift = 4,
        
//
        
// Summary:
        
//     The Windows logo key is pressed.
        Windows = 8,
        
//
        
// Summary:
        
//     The Apple key (also known as the "Open Apple key") is pressed.
        Apple = 8,
    }
}

具体如何方法

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

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

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

 

            ModifierKeys keys = Keyboard.Modifiers;
            txt.Text 
= "";
            
if ((keys & ModifierKeys.Shift) != 0)
                txt.Text 
+= "shift";
            
if ((keys & ModifierKeys.Alt) != 0)
                txt.Text 
+= "alt";
            
if ((keys & ModifierKeys.Apple) != 0)
                txt.Text 
+= "apple";
            
if ((keys & ModifierKeys.Control) != 0)
                txt.Text 
+= "ctrl";
            
if ((keys & ModifierKeys.Windows) != 0)
                txt.Text 
+= "windows";
            txt.Text 
+= " + 鼠标点击"
原创粉丝点击