vb.net与matlab的混合编程
首先,使用deploytool工具或者命令行将matlab的m文件编译成类,结果产生动态链接库.dll文件和一些c#代码的类.
第二步,将这些dll文件导入进去,并使用一些win32api函数,因为此m文件会产生figure窗口,这些api函数将此figure窗口嵌入到vb程序窗体里面.
代码:
Imports System
Imports System.Runtime.InteropServices
Imports MathWorks.MATLAB.NET.Arrays
Imports MathWorks.MATLAB.NET.Utility
Imports SinImage.SinImage
Public Class SinForm
Dim img As New SinImage.SinImage
Dim FigureHwnd As IntPtr = IntPtr.Zero
Public Declare Function SetParent Lib "user32" Alias "SetParent" _
(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function GetWindowLong Lib "User32.Dll" Alias "GetWindowLongA" _
(ByVal hWnd As System.IntPtr, ByVal nIndex As Integer) As Integer
Public Declare Function SetWindowLong Lib "User32.Dll" Alias "SetWindowLongA" _
(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Public Declare Function MoveWindow Lib "user32" Alias "MoveWindow" _
(ByVal hwnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal Flags As Boolean) As Boolean
Private Sub number_NUD_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles number_NUD.Click
img.sx(number_NUD.Value)
Move_Window()
End Sub
Private Sub SinForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
img.sx(number_NUD.Value)
Threading.Thread.Sleep(300)
Do While FigureHwnd = IntPtr.Zero
FigureHwnd = FindWindow(Nothing, "Figure 1")
Threading.Thread.Sleep(100)
Loop
SetWindowLong(FigureHwnd, -16, GetWindowLong(FigureHwnd, -16) And (Not &HC00000))
SetParent(FigureHwnd, Me.image_GBX.Handle)
Move_Window()
End Sub
Private Sub Move_Window() Handles Me.Resize
MoveWindow(FigureHwnd, -3, -20, Me.image_GBX.Width + 8, Me.image_GBX.Height + 54, True)
Me.Refresh()
End Sub
End Class
其实可以将m文件的c#代码,直接使用csc.exe编译成.netmodule文件,然后将应用此m文件包含的函数的.vb文件也使用vbc.exe编译成exe文件,这里面要将csc.exe编译的.netmodule文件引用到vbc.exe编译中.
若m文件不产生figure窗口,程序就更简单,直接导入.dll文件,并使用其中的函数即可.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。