how to deduce the slide ppt position of a shape fr

2019-09-08 16:45发布

问题:

I'm using VB to implement a custom task pane for Powerpoint. I would like to display an image shape on the active powerpoint slide at the position of the mouse. I know screen position of the mouse by using " System.windows.froms.control.MousePosition.Y " .. So now it would be great to transform it into slide ppt position and fill the property "shape.top".

I tried the function "screentoclient" but it doesn't work. What is the unit of shape.top on ppt slide? what is the unit of mouse's screen coordinates?

It is important that the solution works for any size of the screen ...

回答1:

Have a go with this (VBA ... you'll need to translate to .NET)

Sub Test()

    Dim oSh1 As Shape
    Dim oSh2 As Shape

    ' Assuming nothing on the slide but two rectangles
    ' The first with its left edge just touching the left of the slide
    ' The second with its RIGHT edge just touching the right of the slide:

    Set oSh1 = ActivePresentation.Slides(1).Shapes(1)
    Set oSh2 = ActivePresentation.Slides(1).Shapes(2)

    MsgBox "Upperleft = " & vbCrLf _
        & oSh1.Left & " / " & ActivePresentation.Windows(1).PointsToScreenPixelsX(oSh1.Left) & vbCrLf _
        & oSh2.Left + oSh2.Width & " / " & ActivePresentation.Windows(1).PointsToScreenPixelsX(oSh2.Left + oSh2.Width)

    ' Or just working directly with the slide dimensions:
    MsgBox "Upperleft = " & vbCrLf _
        & 0 & " / " & ActivePresentation.Windows(1).PointsToScreenPixelsX(0) & vbCrLf _
        & ActivePresentation.PageSetup.SlideWidth & " / " & ActivePresentation.Windows(1).PointsToScreenPixelsX(ActivePresentation.PageSetup.SlideWidth)

    ' Both give exactly the same results

End Sub