C# IsMDIParent 容器 LabVIEW

C# IsMDIParent Container LabVIEW

我有一些 C# 代码,允许用户从 C# Windows Forms 应用程序控制 LabVIEW VI。

截至目前,当用户单击 "open dialog" 按钮时,它会在另一个单独的 LabVIEW 样式 window 中打开 VI。如果可能的话,我想要的是 window 作为父表单中的子表单打开。

就 LabVIEW/C# 界面而言,其他一切都有效。我只想让一切都独立于一个美观的 window.

这是Form1.cs:

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LabVIEW_DLL_Call
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("SharedLib.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern long Launch();

        [DllImport("SharedLib.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern long SetParams(ushort signalType, double frequency, double amplitude);

        [DllImport("SharedLib.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern long GetData(double[] Array, long len);

        [DllImport("user32.dll")]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            Launch();
            var hWnd = FindWindow("dialog.vi", null);
            SetParent(hWnd, panel1.Handle);
        }

        private void btnSetParams_Click(object sender, EventArgs e)
        {
            SetParams((ushort)this.dropSignalType.SelectedIndex, (double)this.numFreq.Value, (double)this.numAmplitude.Value);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            int dataCount = 1000;
            double[] results = new double[dataCount];
            GetData(results, dataCount);
            string txt = String.Join("\r\n", results);
            this.textBox1.Text = txt;
        }
    }
}

基本上,Form2 在 Form1 中加载,但它也会生成 LabVIEW window。 (第二张照片是launch.vi中的一张)

这是一个很好的开始!

它可能看起来像这样:

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    private const int SWP_NOSIZE = 0x0001;

    private async void btnLaunch_Click(object sender, EventArgs e)
    {
        bool foundIt = false;
        DateTime timeOut = DateTime.Now.AddSeconds(10);
        Launch();
        do
        {
            await Task.Delay(250);
            var hWnd = FindWindow(null, "dialog.vi");
            if (!hWnd.Equals(IntPtr.Zero))
            {
                SetParent(hWnd, panel1.Handle);
                SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE);
                foundIt = true;
            }
        }
        while (!foundIt && (DateTime.Now <= timeOut));
        if (!foundIt)
        {
            MessageBox.Show("Failed to find the LabView window.");
        }
    }