WPF Mouse and Point Acrobatics

I was recently working on implementing mouse gestures in WPF, and was a bit confused with the Point API. To clarify this, I wanted to give a few quick examples that might help people currently working with it:

To get a point relative to a UI element:

1 private Point GetPointRelativeToElement(UIElement element, Point point)
2 {
3        return element.TranslatePoint(point, element);
4 }


To get the absolute screen position of a UI element,

1 private Point GetAbsolutePositionOfElement(UIElement element)
2 {
3       return element.PointToScreen( new Point(0, 0));
4 }

To get the absolute midpoint of a UI element,

1 private Point GetAbsoluteMidPointOfElement(FrameworkElement element)
2 {
3              Point midPoint = element.TranslatePoint( new Point(element.Width / 2, element.Height / 2), element);
4              Point absolute = PointToScreen(midPoint);
5              return absolute;
6 }<br>

To get the relative and absolute mouse position,

1 private Point GetAbsoluteMousePosition()
2 {
3         return PointToScreen(Mouse.GetPosition( this ));
4 }
5  
6 private Point GetMousePositionRelativeToElement(UIElement element)
7 {
8         return Mouse.GetPosition(element);
9 }

 


To programmatically adjust mouse position,

1 using System.Runtime.InteropServices;
2  
3 // … class declaration
4  
5 [DllImport( "user32.dll" )]
6 static extern bool SetCursorPos( int X, int Y);


Now, you can call SetCursorPos(x,y) with absolute coordinates to set the mouse position.
I hope this helps!

 

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值