如何在 Microsoft Visual Basic Express 2010 中创建和删除(或移动)任意形状

How do I create and delete (or move) a freeform shape in Microsoft Visual Basic Express 2010

我已经通过一组 VBA 命令在 Microsoft Excel 中成功地创建和删除了多个自由形状,并且想在 Microsoft Visual Basic Express 2010 中做同样的事情,但真的一无所获! Excel 中的创建代码类似于:

With Sheet1.Shapes.BuildFreeform(msoEditingAuto, triXArray(1), triYArray(1))
    For cnt = 2 To 4
        .AddNodes msoSegmentLine, msoEditingAuto, triXArray(cnt), triYArray(cnt)
    Next
End With

triXArray() 和 triYArray() 是点的 X 和 Y 坐标数组,cnt 是遍历项目的计数器(它是一个三角形)。

这是在 Excel 中从 VB.Net

创建和删除自由形状的方法

您需要在 .ConvertToShape() 之后将自由形式分配给形状。这样,您就可以使用它(删除、移动)

Imports Excel = Microsoft.Office.Interop.Excel
Imports MsoEd = Microsoft.Office.Core.MsoEditingType
Imports MsoSg = Microsoft.Office.Core.MsoSegmentType

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim shpFF As Excel.FreeformBuilder
    Dim Shp As Excel.Shape

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '~~> Add a New Workbook
        xlWorkBook = xlApp.Workbooks.Add
        '~~> Work with first sheet
        xlWorkSheet = xlWorkBook.Sheets(1)

        '~~> Create the Freeform shape
        shpFF = xlWorkSheet.Shapes.BuildFreeform(MsoEd.msoEditingCorner, 360, 200)

        With shpFF
            '~~> Add the nodes. You can use your array method as well
            .AddNodes(MsoSg.msoSegmentCurve, MsoEd.msoEditingCorner, _
             380, 230, 400, 250, 450, 300)
            .AddNodes(MsoSg.msoSegmentCurve, MsoEd.msoEditingAuto, 480, 200)
            .AddNodes(MsoSg.msoSegmentLine, MsoEd.msoEditingAuto, 480, 400)
            .AddNodes(MsoSg.msoSegmentLine, MsoEd.msoEditingAuto, 360, 200)

            '~~> Convert it to shape and assign it
            Shp = .ConvertToShape()

            '~~> Display Excel
            xlApp.Visible = True
        End With

        MessageBox.Show ("Wait")

        '~~> Delete the shape
        Shp.Delete()
    End Sub
End Class