C#IsMDIParent容器LabVIEW

问题描述 投票:1回答:1

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

目前,当用户点击“打开对话框”按钮时,它会在另一个独立的LabVIEW风格窗口中打开VI。如果可能,我想要的是将该窗口作为父窗体内的子窗体打开。

其他所有工作都可以用于LabVIEW / C#接口。我只想把一切都自成一体,放在一个美观的窗户里。

这是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窗口。 (第二张照片是launch.vi之一)

c# program screenshot

launch.vi

c# dll labview mdi
1个回答
0
投票

这是一个很好的开始!

它可能看起来像这样:

    [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.");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.