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文件,并使用其中的函数即可.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值