- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 C#/VSTO 为 PowerPoint (2013) 开发插件。当用户处于设计模式而非演示模式时,加载项将起作用。
如何捕获与幻灯片上的形状/对象相关的鼠标事件,例如mouseOver、mouseDown 等? 我想监听这些事件以创建位于对象/形状附近的自定义 UI。是否有任何我可以监听的事件,或者是否有必要使用更高级的方法,例如创建一个全局鼠标监听器,将坐标转换为 PowerPoint 形状,并循环遍历幻灯片上的形状以查看鼠标是否在其中任何形状的边界?我还将欣赏针对该问题的其他创造性解决方案。
我曾尝试寻找答案,但运气不佳。但是,我知道这是有可能的,因为其他加载项正在做我想做的事情。一个例子是 Think-Cell ( https://www.youtube.com/watch?v=qsnciEZi5X0 ),其中您操作的对象是“普通”PowerPoint 对象,例如 TextFrames 和 Shapes。
我在 Windows 8.1 Pro 上使用 .Net 4.5。
最佳答案
PowerPoint 不直接公开这些事件,但可以通过将全局鼠标 Hook 与 PowerPoint 公开的形状参数相结合来实现您自己的事件。
这个答案涵盖了使用 MouseEnter 和 MouseLeave 的更困难的情况。要处理其他事件,例如 MouseDown 和 MouseUp,您可以使用提供的 PowerPoint 事件 Application_WindowSelectionChange
,正如 Steve Rindsberg 所建议的那样。
要获得全局鼠标 Hook ,您可以使用出色的 Application and Global Mouse and Keyboard Hooks .Net Libary in C#,位于 http://globalmousekeyhook.codeplex.com/。
您需要下载并引用该库,然后使用此代码设置鼠标 Hook
// Initialize the global mouse hook
_mouseHookManager = new MouseHookListener(new AppHooker());
_mouseHookManager.Enabled = true;
// Listen to the mouse move event
_mouseHookManager.MouseMove += MouseHookManager_MouseMove;
可以设置 MouseMove 事件来处理这样的鼠标事件(示例只有 MouseEnter 和 MouseLeave)
private List<PPShape> _shapesEntered = new List<PPShape>();
private List<PPShape> _shapesOnSlide = new List<PPShape>();
void MouseHookManager_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Temporary list holding active shapes (shapes with the mouse cursor within the shape)
List<PPShape> activeShapes = new List<PPShape>();
// Loop through all shapes on the slide, and add active shapes to the list
foreach (PPShapes in _shapesOnSlide)
{
if (MouseWithinShape(s, e))
{
activeShapes.Add(s);
}
}
// Handle shape MouseEnter events
// Select elements that are active but not currently in the shapesEntered list
foreach (PPShape s in activeShapes)
{
if (!_shapesEntered.Contains(s))
{
// Raise your custom MouseEntered event
s.OnMouseEntered();
// Add the shape to the shapesEntered list
_shapesEntered.Add(s);
}
}
// Handle shape MouseLeave events
// Remove elements that are in the shapes entered list, but no longer active
HashSet<long> activeIds = new HashSet<long>(activeShapes.Select(s => s.Id));
_shapesEntered.RemoveAll(s => {
if (!activeIds.Contains(s.Id)) {
// The mouse is no longer over the shape
// Raise your custom MouseLeave event
s.OnMouseLeave();
// Remove the shape
return true;
}
else
{
return false;
}
});
}
_shapesOnSlide
是包含当前幻灯片上所有形状的列表,_shapesEntered
是包含已输入但尚未离开的形状的列表,PPShape
是 PowerPoint 形状的包装对象(如下所示)
MouseWithinShape 方法测试鼠标当前是否位于幻灯片上的某个形状内。它通过将形状的 X 和 Y 坐标(PowerPoint 以点为单位公开)转换为屏幕坐标,并测试鼠标是否在该边界框内来实现这一点
/// <summary>
/// Test whether the mouse is within a shape
/// </summary>
/// <param name="shape">The shape to test</param>
/// <param name="e">MouseEventArgs</param>
/// <returns>TRUE if the mouse is within the bounding box of the shape; FALSE otherwise</returns>
private bool MouseWithinShape(PPShape shape, System.Windows.Forms.MouseEventArgs e)
{
// Fetch the bounding box of the shape
RectangleF shapeRect = shape.Rectangle;
// Transform to screen pixels
Rectangle shapeRectInScreenPixels = PointsToScreenPixels(shapeRect);
// Test whether the mouse is within the bounding box
return shapeRectInScreenPixels.Contains(e.Location);
}
/// <summary>
/// Transforms a RectangleF with PowerPoint points to a Rectangle with screen pixels
/// </summary>
/// <param name="shapeRectangle">The Rectangle in PowerPoint point-units</param>
/// <returns>A Rectangle in screen pixel units</returns>
private Rectangle PointsToScreenPixels(RectangleF shapeRectangle)
{
// Transform the points to screen pixels
int x1 = pointsToScreenPixelsX(shapeRectangle.X);
int y1 = pointsToScreenPixelsY(shapeRectangle.Y);
int x2 = pointsToScreenPixelsX(shapeRectangle.X + shapeRectangle.Width);
int y2 = pointsToScreenPixelsY(shapeRectangle.Y + shapeRectangle.Height);
// Expand the bounding box with a standard padding
// NOTE: PowerPoint transforms the mouse cursor when entering shapes before it actually
// enters the shape. To account for that, add this extra 'padding'
// Testing reveals that the current value (in PowerPoint 2013) is 6px
x1 -= 6;
x2 += 6;
y1 -= 6;
y2 += 6;
// Return the rectangle in screen pixel units
return new Rectangle(x1, y1, x2-x1, y2-y1);
}
/// <summary>
/// Transforms a PowerPoint point to screen pixels (in X-direction)
/// </summary>
/// <param name="point">The value of point to transform in PowerPoint point-units</param>
/// <returns>The screen position in screen pixel units</returns>
private int pointsToScreenPixelsX(float point)
{
// TODO Handle multiple windows
// NOTE: PresStatic is a reference to the PowerPoint presentation object
return PresStatic.Windows[1].PointsToScreenPixelsX(point);
}
/// <summary>
/// Transforms a PowerPoint point to screen pixels (in Y-direction)
/// </summary>
/// <param name="point">The value of point to transform in PowerPoint point-units</param>
/// <returns>The screen position in screen pixel units</returns>
private int pointsToScreenPixelsY(float point)
{
// TODO Handle multiple windows
// NOTE: PresStatic is a reference to the PowerPoint presentation object
return PresStatic.Windows[1].PointsToScreenPixelsY(point);
}
最后我们实现了一个自定义的 PPShape
对象,它公开了我们想要监听的事件
using System.Drawing;
using Microsoft.Office.Interop.PowerPoint;
using System;
namespace PowerPointDynamicLink.PPObject
{
class PPShape
{
#region Fields
protected Shape shape;
/// <summary>
/// Get the PowerPoint Shape this object is based on
/// </summary>
public Shape Shape
{
get { return shape; }
}
protected long id;
/// <summary>
/// Get or set the Id of the Shape
/// </summary>
public long Id
{
get { return id; }
set { id = value; }
}
protected string name;
/// <summary>
/// Get or set the name of the Shape
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Gets the bounding box of the shape in PowerPoint Point-units
/// </summary>
public RectangleF Rectangle
{
get
{
RectangleF rect = new RectangleF
{
X = shape.Left,
Y = shape.Top,
Width = shape.Width,
Height = shape.Height
};
return rect;
}
}
#endregion
#region Constructor
/// <summary>
/// Creates a new PPShape object
/// </summary>
/// <param name="shape">The PowerPoint shape this object is based on</param>
public PPShape(Shape shape)
{
this.shape = shape;
this.name = shape.Name;
this.id = shape.Id;
}
#endregion
#region Event handling
#region MouseEntered
/// <summary>
/// An event that notifies listeners when the mouse has entered this shape
/// </summary>
public event EventHandler MouseEntered = delegate { };
/// <summary>
/// Raises an event telling listeners that the mouse has entered this shape
/// </summary>
internal void OnMouseEntered()
{
// Raise the event
MouseEntered(this, new EventArgs());
}
#endregion
#region MouseLeave
/// <summary>
/// An event that notifies listeners when the mouse has left this shape
/// </summary>
public event EventHandler MouseLeave = delegate { };
/// <summary>
/// Raises an event telling listeners that the mouse has left this shape
/// </summary>
internal void OnMouseLeave()
{
// Raise the event
MouseLeave(this, new EventArgs());
}
#endregion
#endregion
}
}
为了完全详尽,还有多个应该处理的额外元素未在此处介绍。这包括在 PowerPoint 窗口停用时暂停鼠标 Hook 、处理多个 PowerPoint 窗口和多个屏幕等。
关于c# - 通过 VSTO 在 PowerPoint 设计器中捕获鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22815084/
我正在尝试为我的网站创建一个功能,允许用户使用 mousemove 和 touchmove 事件水平滚动 div 内容(类似于 Apple AppStore any app Screenshots s
我有固定的侧边栏导航栏,它在悬停时工作,但我想通过单击折叠按钮打开第一个菜单。类似于悬停在菜单 1 上的工作方式。我已经尝试了以下方法。 jsfiddle Demo $(document).on('c
Mouse.Synchronize() 在 .Net 中有什么作用? MSDN 说它“强制鼠标重新同步” 最佳答案 只是我的假设: Stylus 中存在类似的方法类别:Stylus.Synchroni
有没有什么办法可以同时使用鼠标, pygame.mouse.set_visible(False) 已激活。当前鼠标仅在尝试使用时返回右下坐标。需要在隐藏鼠标时能够获得正确的坐标。 在他们的 docum
我有一个缺少数据的数据库。我需要估算数据(我使用的是鼠标),然后根据原始列创建新列(使用估算数据)。我需要使用这些新列进行统计分析。 具体来说,我的参与者使用李克特 7 分量表填写了几份问卷。有些人没
我正在编写一个与电脑交互的机器人。简而言之,我所做的是: -截取屏幕截图- 在此屏幕截图上识别对象(使用 cv2 matchTemplate) -使用找到的位置进行一些鼠标操作(例如:将鼠标指针移动到
我的程序是一个文本游戏,它使用 WindowsForm 上的文本框模拟控制台输出。我试图实现的一个功能是通过单击一个按钮,它将以一定的速度输出到 TextBox,这是通过这种方法实现的 atm: pu
我遇到了一个问题。如果有任何帮助,我将不胜感激。 我正在尝试从玩家位置射击到鼠标点击位置。代码没有给我任何错误,根据我的逻辑,它应该可以工作,但它没有 它创建了项目符号对象,仅此而已。 //Bulle
给定一个带蓝牙的 Windows Mobile 6.1 智能手机,我想将它注册为鼠标。 基本上我现在做的: 使用 Guid {00001124-0000-1000-8000-00805f9b34fb}
我有一个关于在 JavaFX 中实现鼠标拖动事件的正确方法的问题。 我的 playGame() 方法当前使用 onMouseClicked,但这只是一个占位符 理想情况下,我希望“飞盘”沿着鼠标拖动的
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我目前正在使用 Windows 的 RawInput API 来访问键盘和鼠标输入。我有点困惑的一件事是,当我将鼠标注册为 RawInputDevice 时,我无法移动我的 Win32 窗口或使用那里
我想在我的网站浏览器窗口中 move 鼠标,如下所示:www.lmsify.com。我怎样才能做到这一点?(javascript、flash、activex) 问候,丽莎M 最佳答案 他们并没有真正
我想要一个动画。我是后端开发人员,但我必须使用 jquery 创建动画。 动画、背景和元素位置随鼠标移动而变化。 类似于http://www.kennedyandoswald.com/#!/premi
如何将鼠标“锁定”到某个 OpenGL 窗口。有点像在 Minecraft 中是如何完成的。GameDev 是一个更好的询问地点吗? 最佳答案 正如 Robert 在评论中所说,OpenGL 实际上并
我正在尝试实现一个颜色选择器,它从屏幕上各处的像素中获取颜色。为此,我计划使用全局鼠标 Hook 来监听 WM_MOUSEMOVE,以便在鼠标四处移动时更新颜色,并监听鼠标点击以确认 (WM_LBUT
如何使用 Java 和 JNA(Java native 访问)与 Windows API 交互?。我试图通过在鼠标输入流上排队鼠标事件来让鼠标做某事,并且代码有效,因为 SendInput(...)
我想用 C++ 脚本 move 鼠标光标。我在 Parallels 中的 Windows 7 中使用 Visual C++ 2010 Express,并创建了一个控制台应用程序。 我知道 SetCur
我有一些关于 WH_MOUSE 的问题。根据我的阅读,通过将钩子(Hook)放入 DLL 中,它会注入(inject)进程。这是否意味着捕获鼠标也适用于我的桌面、菜单启动等?那么应用程序的标题栏呢?我
如何为多只鼠标显示另一个光标? 我有两个 TMemos,两个可以输入各自 TMemo 的键盘,2 个鼠标,我需要 2 个光标。 如果假设的话,我已经可以检测出哪只鼠标是哪只了。我怎样才能让我自己的光标
我是一名优秀的程序员,十分优秀!