private void button2_Click(object sender, RoutedEventArgs e)//獲取位置
{
POINT p = new POINT();
Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
Point ppp = (e.Source as FrameworkElement).PointToScreen(pp);//WPF方法
if (GetCursorPos(out p))//API方法
{
MessageBox.Show(string.Format("GetCursorPos {0},{1} GetPosition {2},{3}\r\n {4},{5}", p.X, p.Y, pp.X, pp.Y, ppp.X, ppp.Y));
}
}
/// <summary>
/// 設置鼠標的坐標
/// </summary>
/// <param name="x">橫坐標</param>
/// <param name="y">縱坐標</param>
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
/// <summary>
/// 獲取鼠標的坐標
/// </summary>
/// <param name="lpPoint">傳址參數,坐標point類型</param>
/// <returns>獲取成功返回真</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);