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:
view source
print
?
1 |
private Point GetPointRelativeToElement(UIElement element, Point point) |
3 |
return element.TranslatePoint(point, element); |
To get the absolute screen position of a UI element,
view source
print
?
1 |
private Point GetAbsolutePositionOfElement(UIElement element) |
3 |
return element.PointToScreen(
new Point(0, 0)); |
To get the absolute midpoint of a UI element,
view source
print
?
1 |
private Point GetAbsoluteMidPointOfElement(FrameworkElement element) |
3 |
Point midPoint = element.TranslatePoint(
new Point(element.Width / 2, element.Height / 2), element); |
4 |
Point absolute = PointToScreen(midPoint); |
To get the relative and absolute mouse position,
view source
print
?
1 |
private Point GetAbsoluteMousePosition() |
3 |
return PointToScreen(Mouse.GetPosition(
this
)); |
6 |
private Point GetMousePositionRelativeToElement(UIElement element) |
8 |
return Mouse.GetPosition(element); |
To programmatically adjust mouse position,
view source
print
?
1 |
using System.Runtime.InteropServices; |
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