如何使用Pwp对象? (无法获得形状的宽度和高度)

时间:2016-11-12 21:02:33

标签: c# reference powerpoint add-in shape

我正在为自定义功能区执行添加操作,并且在编写要由功能区按钮执行的代码(回调方法)时,在引用属于Pwp API的代码时出现错误(我在项目中使用了API)引用,虽然它的“复制本地”属性为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#中的形状????

1 个答案:

答案 0 :(得分:0)

解决:

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

    }