我正在开发一些运行命令并打开 MATLAB 窗口来绘制图形的应用程序,我需要捕获这个新窗口并将其显示在启动它的父窗口窗体中。实际上是那个 MATLAB 打开了这个新窗口,但是有没有办法抓住它并将它放在我的窗口中?谢谢
问问题
355 次
3 回答
0
于 2013-03-29T16:11:45.687 回答
0
我相信您想要像 Iframe 对象这样的东西,您可以在其中将新内容加载到该容器中,但感觉它是同一个应用程序的一部分。不幸的是,您不能开箱即用,除非 MathLab 对象提供一些您可以从应用程序本身使用的 ActiveX 对象。您可以做的就是模拟外部 exe 是使用 Win32 API 的应用程序的一部分。看看这是否有帮助:
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);
}
}
}
于 2013-03-29T16:19:29.707 回答
0
您可以简单地将您的Matlab 项目构建为.Net或COM对象组件。因此,您将像这样构建和打包组件:
- 选择要构建的组件类型(.Net 或 COM)
- 添加要作为组件的公共方法访问的 Matlab 代码文件和 MEX 文件。
- 指定定义生成的类型安全 API 的 .Net 接口。
- 设置建筑和包装的属性。
- 构建,然后测试您的组件以确保其正常工作。
- 为程序员或最终用户打包组件和 MCR。
完成上述步骤后,您将能够像访问Visual Studio 中的任何其他对象组件一样访问Matlab 组件。然后,您可以从Matlab 和 .Net本地传递对象,而不会出现任何问题。
由于数据已被转换;所以它会像任何其他组件一样交互。
然后,您可以通过在Windows 窗体上拖动组件来简单地进行交互或界面,并将其视为任何其他MDI
.
MatlabComponentFrm m = new MatlabComponentFrm();
m.MdiParent = this;
m.Show();
非常简单的例子。在我看来,这将是最简单的。否则,您可以导入或托管可执行文件。你有很多方法可以做到这一点。希望这会有所帮助。
于 2013-03-29T16:25:23.880 回答