这次给大家聊聊如何获取鼠标滚轮事件.
鼠标滚轮事件 在当前版本beta2中并没有 但是我们可以借助 Htmlpage 对象 HtmlPage(System.Windows.Browser;)(之前也多次提到过他如何捕捉Silverlight右键点击事件,如何在Silverlight中使用Cookie ) 实现此功能
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
+=
"
+ 鼠标点击
"
;
以上是个人总结的一点小技巧而已~ 希望这点技巧对你有所帮助^^
Source code: Mouse_Wheel_keys_Event Deom