将 Magic Square 集成到 COM 应用程序中
概述
此示例使用一个简单的 MATLAB® 文件,该文件接受单个输入并创建该大小的幻方。然后,它使用此 MATLAB 文件作为类方法构建 COM 组件。最后,该示例展示了如何将该组件集成到独立的 Microsoft® Visual Basic® 应用程序中。该应用程序接受幻方大小作为输入,并在 ListView 控制框中显示矩阵。
注意
ListView 是一个 Windows® 窗体控件,用于显示带有图标的项列表。您可以使用列表视图来创建类似于 Windows 资源管理器右侧窗格的用户界面。有关 Windows 窗体控件的更多信息,请参阅 MSDN 库。
创建 MATLAB 文件
首先,创建包含以下代码的 MATLAB 文件 mymagic.m:
function y = mymagic(x); y = magic(x);
使用 COM 组件编译器创建和构建工程
在 MATLAB 中,打开 COM 组件编译器。
将
mymagic.m添加到导出函数列表中。mymagic.m位于MagicDemoComp文件夹中。点击打包按钮。
创建 Microsoft Visual Basic 工程
注意
此过程假定您使用的是 Microsoft Visual Basic 6.0。
启动 Visual Basic。
在 New Project 对话框中,选择 Installed > Templates > Other Languages > Visual Basic > Windows Form Application 作为工程类型,然后点击 Open。这将创建一个带有空白表单的新 Visual Basic 工程。
从主菜单中,选择 Project > References 以打开 Project References 对话框。
从可用组件列表中选择 magicdemo 1.0 Type Library 并点击 OK。
返回 Visual Basic 主菜单,选择 Project > Add Component... 以打开 Add New Item 对话框。
创建用户界面
创建工程后,向空白表单中添加一系列控件,创建具有以下设置的表单。
| 控制类型 | 控件名称 | 属性 | 目的 |
|---|---|---|---|
|
| Caption = | 对控件分组 |
|
| Caption = | 标记幻方编辑框。 |
|
| 接受幻方大小的输入。 | |
|
| Caption = | 按下时,创建一个具有当前大小的新幻方。 |
|
| GridLines = LabelEdit = View = | 显示幻方。 |
当表单和控件完成后,将以下代码添加到表单中。此代码引用了上面列出的控件和变量名称。如果您为任何控件或任何变量指定了不同的名称,请更改此代码以反映这些差异。
Public Class magicvb
Private sizeMatrix As Double 'Holds current matrix size
Private theMagic As magicdemo.magicdemoclass 'magic object instance
Private Sub magicvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This function is called when the form is loaded.
'Creates a new magic class instance.
On Error GoTo Handle_Error
theMagic = New magicdemo.magicdemoclass
sizeMatrix = 0
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
Private Sub ShowMatrix(matrixMagic As Object)
'This function populates the ListView with the contents of
'y. y is assumed to contain a 2D array.
Dim szSquare As Long
Dim indxRow As Long
Dim indxCol As Long
Dim nLen As Long
On Error GoTo Handle_Error
'Get array size
If IsArray(matrixMagic) Then
szSquare = UBound(matrixMagic, 1)
Else
szSquare = 1
End If
lstMagic.Clear()
lstMagic.Columns.Add("")
For cIndx = 1 To szSquare
lstMagic.Columns.Add(CStr(cIndx))
Next
lstMagic.View = View.Details
For indxRow = 1 To szSquare
Dim item As New ListViewItem(CStr(indxRow))
For indxCol = 1 To szSquare
item.SubItems.Add(Format(matrixMagic(indxRow, indxCol)))
Next
lstMagic.Items.Add(item)
Next
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
'This function is called when the Create button is pressed.
'Calls the mymagic method, and displays the magic square.
Dim matrixMagic As Object
If sizeMatrix <= 0 Or theMagic Is Nothing Then Exit Sub
On Error GoTo Handle_Error
Call theMagic.mymagic(1, matrixMagic, sizeMatrix)
Call ShowMatrix(matrixMagic)
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
Private Sub edtSize_TextChanged(sender As Object, e As EventArgs) Handles edtSize.TextChanged
'This function is called when ever the contents of the
'Text box change. Sets the current value of Size.
On Error Resume Next
sizeMatrix = CDbl(edtSize.Text)
If Err.Number > 0 Then
sizeMatrix = 0
End If
End Sub
End Class在 Microsoft Visual Basic 中创建可执行文件
代码完成后,创建独立可执行文件 magic.exe:
从主菜单中选择 File > Save Project 重新打开工程。接受主窗体的默认名称并输入
magic.vbp作为工程名称。返回 File 菜单。选择 File > Make magic.exe 来创建成品。
测试应用程序
您可以像运行任何其他程序一样运行 magic.exe 可执行文件。当主对话框打开时,在输入框中输入一个正数,然后点击 Create。出现输入大小的幻方。
如果幻方大于 4×4,则 ListView 控件会自动实现滚动。