使用VSTO在PowerPoint中检索鼠标位置

问题描述 投票:0回答:1

根据Catch mouse events in PowerPoint designer through VSTO我们可以使用名为http://globalmousekeyhook.codeplex.com/的库来捕获鼠标事件。它的效果很好,但是只能提供相对于整个屏幕的坐标。我正在寻找相对于PowerPoint幻灯片的单击或鼠标移动,其中0,0坐标实际上是幻灯片的左上角,而不是整个屏幕。

这是否有可能?

vsto powerpoint
1个回答
0
投票

有几种方法可以在基于VSTO的加载项中获取鼠标坐标:

  1. 您可以自由使用Windows API函数-GetCursorPos
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;
}
  1. 您可以使用Cursor.Position属性来获取或设置光标的位置。
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);
  1. Control.MousePosition属性获取鼠标光标在屏幕坐标中的位置。然后,您可以使用上面显示的代码来获取客户坐标。
© www.soinside.com 2019 - 2024. All rights reserved.