Question

TomScherger on Thu, 15 Jun 2017 22:57:27


Problem:  Shapes in PowerPoint do not respond to mouse events and ActiveX objects yield cursor coordinates relative to the object, not the PowerPoint slide. I am trying to develop a way to track cursor coordinates even when it is over a Shape in PowerPoint presentation mode.  I’d like the slide respond to a mouse click while at the same time having access to the cursor coordinates relative to the window.  My attempts at using an OnMouseMove event tied to an ActiveX object will not work because the event is only fired when the mouse is over the object.  I’ve tried creating an ActiveX object that covers the slide and making it transparent but the underlying Shapes alter the mouse event behavior when the cursor is over the shape.  I.e. the mouse events do not occur until one moves off the shape.  In summary, is there a way to have guaranteed access to cursor coordinates and guaranteed response to mouse events anywhere on a Slide in presentation mode? 

Sponsored



Replies

Deepak Saradkumar Panchal on Fri, 16 Jun 2017 06:34:39


Hi TomScherger,

Powerpoint does not provide any event for that.

but you can try to use Mouse hooks.

to get more detailed information and example code please visit the link below.

Catch mouse events in PowerPoint designer through VSTO

part of the code:

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);
}

so you can try to refer the answer in that thread and try to implement the code to get coordinates.

Regards

Deepak