C # IsMDIParent Container LabVIEW - PullRequest
       36

C # IsMDIParent Container LabVIEW

0 голосов
/ 23 февраля 2019

У меня есть некоторый код C #, который позволяет пользователю управлять VI LabVIEW из приложения C # Windows Forms.

На данный момент, когда пользователь нажимает кнопку «Открыть диалог», он открываетVI в другом отдельном окне в стиле LabVIEW.Что бы я хотел, если это возможно, это открыть это окно как дочернюю форму внутри родительского элемента.

Все остальное работает до интерфейса 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.(А вторая фотография - одна из версий .vi)

c# program screenshot

launch.vi

1 Ответ

0 голосов
/ 24 февраля 2019

Отличное начало!

Это может выглядеть примерно так:

    [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.");
        }
    }
...