【问题标题】:How work with Pwp objects? (unable to get shapes' width and height)如何使用 Pwp 对象? (无法获取形状的宽度和高度)
【发布时间】:2016-11-12 21:02:32
【问题描述】:

我正在为自定义功能区添加插件,并且在编写要由功能区按钮(回调方法)执行的代码时,在引用属于 Pwp API 的代码时出现错误(我在项目的 API参考文献,虽然它的“Copy Local”属性为 False,我无法将其设为 True(不知道这是否有问题)。

所以我什么都做不了……

代码:

public void SwapPositions(Office.IRibbonControl control, bool isPressed)
        {

            _Application myPPT = Globals.ThisAddIn.Application;
            Slide curSlide = myPPT.ActiveWindow.View.Slide;

            curSlide.Shapes.Range(0).Height;

        }

在这种情况下,错误出现在最后一条语句中。 VS 说:“只有赋值、调用、递增、递减和新建对象表达式可以作为语句使用”。

如您所见,代码才刚刚开始,因为我无法找到解决方案。 最后,我想在 C# 中复制我在 VBA 中已经拥有的用于切换形状位置的代码:

Sub swap_positions()

Static t1, l1, h1, w1 As Double
Dim t2, l2, h2, w2 As Double

t1 = ActiveWindow.Selection.ShapeRange(1).Top
l1 = ActiveWindow.Selection.ShapeRange(1).Left
h1 = ActiveWindow.Selection.ShapeRange(1).Height
w1 = ActiveWindow.Selection.ShapeRange(1).Width

t2 = ActiveWindow.Selection.ShapeRange(2).Top
l2 = ActiveWindow.Selection.ShapeRange(2).Left
h2 = ActiveWindow.Selection.ShapeRange(2).Height
w2 = ActiveWindow.Selection.ShapeRange(2).Width

'1 Vertical alignment
ActiveWindow.Selection.ShapeRange(1).Top = ActiveWindow.Selection.ShapeRange(2).Top + _
(ActiveWindow.Selection.ShapeRange(2).Height - ActiveWindow.Selection.ShapeRange(1).Height) / 2

'1 Horizontal alignment
ActiveWindow.Selection.ShapeRange(1).Left = ActiveWindow.Selection.ShapeRange(2).Left + _
(ActiveWindow.Selection.ShapeRange(2).Width - ActiveWindow.Selection.ShapeRange(1).Width) / 2

'2 Vertical alignment
ActiveWindow.Selection.ShapeRange(2).Top = t1 + _
(h1 - h2) / 2

'2 Horizontal alignment
ActiveWindow.Selection.ShapeRange(2).Left = l1 + _
(w1 - w2) / 2

End Sub

问题:在这种情况下,我如何在 c# 中处理形状????

【问题讨论】:

    标签: c# reference powerpoint add-in shape


    【解决方案1】:

    已解决:

    public void SwapPositions(Office.IRibbonControl control)
        {
            //Defining shape variables: Height, Width, Top and Left
    
            float[] heigthArray;
            heigthArray = new float[30];
            float[] widthArray;
            widthArray = new float[30];
            float[] topArray;
            topArray = new float[30];
            float[] leftArray;
            leftArray = new float[30];
    
            //Finding Height, Width, Top and Left of shapes
            int i = 1;
            foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange)
            {
                heigthArray[i] = sh.Height;
                widthArray[i] = sh.Width;
                topArray[i] = sh.Top;
                leftArray[i] = sh.Left;
    
                i = i + 1;
            }
    
            //Vetical alignment
    
            int x = 1;
            float shapeTop = 1;
            float shapeHeight = 1;
            foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange)
            {
    
                if (x == 1)
                {
                    shapeTop = sh.Top;
                    shapeHeight = sh.Height;
    
                    sh.Top = topArray[x + 1] + (heigthArray[x + 1] - heigthArray[x]) / 2;
                }
                else if (x == i - 1)
                {
                    sh.Top = shapeTop + (shapeHeight - heigthArray[x]) / 2;
    
                }
                else
                {
                    sh.Top = topArray[x + 1] + (heigthArray[x + 1] - heigthArray[x]) / 2;
                }
    
                x = x + 1;
    
            }
    
            //Horizontal alignment
            x = 1;
            float shapeLeft = 1;
            float shapeWidth = 1;
            foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange)
            {
                if (x == 1)
                {
                    shapeLeft = sh.Left;
                    shapeWidth = sh.Width;
    
                    sh.Left = leftArray[x + 1] + (widthArray[x + 1] - widthArray[x]) / 2;
                }
                else if (x == i - 1)
                {
                    sh.Left = shapeLeft + (shapeWidth - widthArray[x]) / 2;
    
                }
                else
                {
                    sh.Left = leftArray[x + 1] + (widthArray[x + 1] - widthArray[x]) / 2;
                }
    
    
                x = x + 1;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 2012-10-10
      • 2013-07-12
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2020-10-12
      • 2022-01-15
      相关资源
      最近更新 更多