您的位置:首页 > 移动开发

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

2011-01-10 15:59 609 查看
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 += " + 鼠标点击"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐