how to deduce the slide ppt position of a shape from the screen position of the mouse

杀马特。学长 韩版系。学妹 提交于 2019-12-13 21:27:20

问题


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


来源:https://stackoverflow.com/questions/20786012/how-to-deduce-the-slide-ppt-position-of-a-shape-from-the-screen-position-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!