?? form1.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 + -