根据Catch mouse events in PowerPoint designer through VSTO我们可以使用名为http://globalmousekeyhook.codeplex.com/的库来捕获鼠标事件。它的效果很好,但是只能提供相对于整个屏幕的坐标。我正在寻找相对于PowerPoint幻灯片的单击或鼠标移动,其中0,0坐标实际上是幻灯片的左上角,而不是整个屏幕。
这是否有可能?
有几种方法可以在基于VSTO的加载项中获取鼠标坐标:
using System.Runtime.InteropServices;
using System.Windows; // Or use whatever point class you like for the implicit cast operator
/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
// NOTE: If you need error handling
// bool success = GetCursorPos(out lpPoint);
// if (!success)
return lpPoint;
}
Point cp = this.PointToClient(Cursor.Position); // Getting a cursor's position according form's area
Console.WriteLine("Cursor position: X = " + cp.X + ", Y = " + cp.Y);