【二次开发】将CATIA嵌入到Winform窗体中

来源:互联网 发布:微信网络投票公司 编辑:程序博客网 时间:2024/04/27 19:09

由于项目需要,我们需要将CATIA嵌入到我们的软件之中,要求在软件启动后,同时调用并启动CATIA软件,并能够屏蔽掉软件自身的菜单和按钮。通过在网上查阅资料,实现了这一功能。

调用并启动CATIA

public string GetCatiaInstallPath(){    // 通过读取注册表,获取CATIA安装路径    string keyPath = "CATIA.Analysis\\protocol\\StdFileEditing\\server";    RegistryKey key = Registry.ClassesRoot.OpenSubKey(keyPath);    return key.GetValue("(默认)");}[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);[DllImport("user32.dll")]public static extern int SetParent(IntPtr hWndChild,IntPtr hWndParent);public const int GWL_STYLE = -16;public const int WS_VISIBLE = 0x10000000;public static void LoadExtApplication(string installPath, IntPtr parentHandle){    // 启动CATIA    Process process = Process.Start(installPath);    process.WaitForInputIdle();    // 嵌入到parentHandle指定的句柄中    IntPtr appWin = process.MainWindowHandle;    SetParent(appWin, parentHandle);    SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);}

移动CATIA窗体并指定其窗体大小

[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);public static void Move(IntPtr appWin, int x, int y, int nWidth, int nHeight){    if (appWin != null)    {        // 其中x,y为CATIA窗体相对于其父窗体左上角的位置        // nWidth为CATIA窗体的宽度,nHeight为CATIA窗体的高度        MoveWindow(appWin, x, y, nWidth, nHeight, true);    }}

为了隐藏CATIA顶部的菜单栏,我们只需将y值设为-20即可隐藏掉菜单栏。同时,为了使CATIA窗体能够填满其父窗体,需要指定CATIA窗体的宽度和高度与父窗体保持一致。并且在父窗体大小改变时能够同时调整CATIA窗体的大小。


利用此原理,我们也能够将其他软件嵌入到Winform窗体中,只需按需求调整相应的x,y,nWidth,nHeight的值即可。

0 0
原创粉丝点击