コマンドを実行し、MATLAB ウィンドウを開いてグラフをプロットするアプリを開発しています。この新しいウィンドウをキャッチして、それを開始する親ウィンドウ フォーム内に表示する必要があります。この新しいウィンドウを開くのは実際には MATLAB ですが、それをキャッチしてウィンドウに配置する方法はありますか? ありがとう
3 に答える
新しいコンテンツをそのコンテナーにロードする Iframe オブジェクトのようなものが必要だと思いますが、それは同じアプリケーションの一部であると感じます。残念ながら、MathLab オブジェクトがアプリケーション自体から使用できる ActiveX オブジェクトを提供しない限り、すぐに使用することはできません。あなたが考えられることは、Win32 API を使用して、外部の exe がアプリケーションの一部であることをシミュレートすることです。これが役立つかどうかを確認してください:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CarPC
{
public partial class MainForm
{
#region Methods/Consts for Embedding a Window
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
private static extern bool PostMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);
private const int SWP_NOOWNERZORDER = 0x200;
private const int SWP_NOREDRAW = 0x8;
private const int SWP_NOZORDER = 0x4;
private const int SWP_SHOWWINDOW = 0x0040;
private const int WS_EX_MDICHILD = 0x40;
private const int SWP_FRAMECHANGED = 0x20;
private const int SWP_NOACTIVATE = 0x10;
private const int SWP_ASYNCWINDOWPOS = 0x4000;
private const int SWP_NOMOVE = 0x2;
private const int SWP_NOSIZE = 0x1;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000;
private const int WS_MAXIMIZE = 0x01000000;
#endregion
#region Variables
private Panel gpsPanel;
private IntPtr gpsHandle;
private Process gpsProcess = null;
private ProcessStartInfo gpsPSI = new ProcessStartInfo();
#endregion
private void SetupGPSPanel()
{
//Panel to Contain Controls
this.gpsPanel = new Panel();
this.gpsPanel.Location = new Point(this.LeftBarRight, this.TopBarBottom);
this.gpsPanel.Size = new Size(this.Size.Width - this.LeftBarRight, this.Size.Height - this.TopBarBottom);
this.gpsPanel.Visible = false;
gpsPSI.FileName = "notepad.exe";
gpsPSI.Arguments = "";
gpsPSI.WindowStyle = ProcessWindowStyle.Maximized;
gpsProcess = System.Diagnostics.Process.Start(gpsPSI);
gpsProcess.WaitForInputIdle();
gpsHandle = gpsProcess.MainWindowHandle;
SetParent(gpsHandle, this.gpsPanel.Handle);
SetWindowLong(gpsHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
MoveWindow(gpsHandle, 0,0, this.gpsPanel.Width, this.gpsPanel.Height, true);
this.Controls.Add(gpsPanel);
}
}
}
Matlab プロジェクトを.NetまたはCOM Object-Componentとして簡単にビルドできます。したがって、次のようにコンポーネントをビルドしてパッケージ化します。
- ビルドするコンポーネントのタイプを選択します (.Net または COM)
- コンポーネントのパブリック メソッドとしてアクセスする Matlab コード ファイルと MEX ファイルを追加します。
- 生成されたタイプ セーフ API を定義する .Net インターフェイスを指定します。
- ビルドとパッケージ化のプロパティを設定します。
- コンポーネントをビルドしてテストし、機能することを確認します。
- プログラマーまたはエンド ユーザー向けに、コンポーネントと MCR をパッケージ化します。
上記の手順を完了すると、Visual Studio から他のオブジェクト コンポーネントと同様にMatlab コンポーネントにアクセスできるようになります。その後、 Matlab および .Netから問題なく オブジェクトをネイティブに渡すことができます。
データが変換されたため。そのため、他のコンポーネントと同様に相互作用します。
次に、コンポーネントをWindows フォームにドラッグするだけで対話またはインターフェイスし、他の と同じように扱うことができMDI
ます。
MatlabComponentFrm m = new MatlabComponentFrm();
m.MdiParent = this;
m.Show();
非常に単純な例。私の意見では、それが最も簡単でしょう。それ以外の場合は、実行可能ファイルをインポートまたはホストできます。これを実現する方法はいくつかあります。うまくいけば、それは役に立ちます。