2013-03-29 211 views
0

我正在开发一些运行命令并打开一个MATLAB窗口来绘制图形的应用程序,我需要捕获这个新窗口并在启动它的父窗口窗体中显示它。实际上,MATLAB会打开这个新窗口,但是无论如何要抓住它并将它放在窗口中?谢谢如何在父窗口窗体中显示一个窗口?

+0

您是否正在像'MDI儿童形式'那样对待窗口?我不完全确定你的意思是*抓住它并把它放在我的窗口*中。什么类型的窗口? – Greg

+0

我想你正在开发的应用程序是使用Windows窗体的权利? – Marco

回答

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); 
    } 
} 
} 
0

你可以简单地建立自己的Matlab的项目作为COM对象的组件。所以,你会构建和打包像这样的组件:

  • 选择组件的类型建(.NET或COM)
  • 加入Matlab代码文件和MEX-文件被作为公共方法来访问你的零件。
  • 指定一个定义生成的类型安全API的.Net接口。
  • 设置建筑和包装的属性。
  • 建立,然后测试你的组件,以确保它的功能。
  • 为程序员或最终用户打包组件和MCR。

一旦你完成了这些上面的步骤,你就可以访问Matlab的组件从Visual Studio中的任何其他对象的组成部分。然后,您可以从Matlab和.Net本机传递对象,没有任何问题。

由于数据已被转换;所以它会像任何其他组件一样进行交互。

然后,您可以通过将组件拖动到Windows窗体上进行简单的交互或界面,并像其他任何MDI一样对待它。

MatlabComponentFrm m = new MatlabComponentFrm(); 
m.MdiParent = this; 
m.Show(); 

很简单的例子。这在我看来是最简单的。否则,您可以导入或主机可执行文件。你有很多方法可以实现这一点。希望有帮助。

相关问题