⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 将matlab图形嵌入到C#窗口
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using ShowPicture;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using System.Runtime.InteropServices;
using System.Threading;



//功能:
/*些程序用于实现将Matlab窗口嵌入到C#的窗口中,并实现随主窗口的
 * 大小改变而改变,同时Matlab原有的工具箱仍然保留
 */

//原理:
/*先在matlab里画一个特别小的窗口, 小到看不见, 因为如果要是用figure('Visible','off')
 * 的窗口后面用findwindow()找到matlab图形窗口的过程就会失败. 找到图形窗口之后,就可以设置其父窗口
 */

//实现技术要点:
/*
 * 1.利用Matlab抽作一个显示图形的Dll,以供在C#中调用,绘制图形
 * 2.在C#中导入该DLL(本例中为ShowPicture)
 * 3.导入Matlab For .Net引擎(本机路径:D:\Program Files\MATLAB\R2007a\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll)
 * 4.由于要用到一些Win32 API,事先声明一些API函数(在本例中为DotNetWin32所封装了一些必须的API函数)
 */

//存在问题
/*1.应该在Matlab 显示图形DLL中,将图形窗口初始化一个特别小的窗口, 小到看不见
 * 2.应该当两个窗口都显示出来再去FindWindow
 * 
 */

namespace MatlabWindowInForm
{
    public partial class FrmMatlab : Form
    {
        private IntPtr hChild;  //子窗口
        private IntPtr hParent; //父窗口

        public FrmMatlab()
        {
            InitializeComponent();
            this.Show() ; 
            DrawInit();
        }


        private void DrawInit()
        {
            ShowPictureclass showPicture = new ShowPictureclass();
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;
            string fileName = "Right.fig";

            byte[] unicodeBytes = unicode.GetBytes(fileName);
            byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

            char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string asciiString = new string(asciiChars);

            showPicture.ShowPicture(asciiString);
            Thread.Sleep(1500); //等待显示窗口

            hParent = DotNetWin32.FindWindow("WindowsForms10.Window.8.app.0.3b95145", this.Text);
            hChild = DotNetWin32.FindWindow("com.mathworks.hg.peer.FigureFrameProxy$FigureFrame", "Figure 1");

            if(hParent.ToInt32() == 0 || hChild.ToInt32() == 0)
            {
                MessageBox.Show("获取窗口句炳失败!");
                return;
            }


            DotNetWin32.SetParent(hChild, hParent);

            DotNetWin32.RECT myrect = new DotNetWin32.RECT();

            DotNetWin32.GetClientRect(hParent, ref myrect);
            DotNetWin32.MoveWindow(hChild, -10, -30, myrect.right + 20, myrect.bottom + 40, true); 
 
        }

        private void FrmMatlab_Resize(object sender, EventArgs e)
        {
            DotNetWin32.RECT myrect = new DotNetWin32.RECT();

            DotNetWin32.GetClientRect(hParent, ref myrect);
            DotNetWin32.MoveWindow(hChild, -10, -30, myrect.right + 20, myrect.bottom + 40, true); 

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -