C Sharp和Matlab混合编程调用以捕获图形弹出窗口图像在多次拖动和缩放后消失

问题描述

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        #region //Windows API

        #endregion


        public Form1()
        {
            InitializeComponent();
        }

        public delegate void UpdateUI1();
        public delegate void UpdateUI2();
        public delegate void UpdateUI3();
        Thread startload1;
        Thread startload2;
        Thread startload3;
        IntPtr figure1;
        IntPtr figure2;
        IntPtr figure3;

        private void button1_Click(object sender,EventArgs e)
        {
            FileStream fs = new FileStream("ant_orbit_data_test_trans12.m",FileMode.OpenorCreate,FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Properties.Resources.ant_orbit_data_test_trans12);
            sw.Close(); //关闭文件
            fs = null;
            figure1 = IntPtr.Zero;
            figure2 = IntPtr.Zero;
            figure3 = IntPtr.Zero;
            startload1 = null;
            startload2 = null;
            startload3 = null;

            MLApp.MLApp matlab = null;
            Type matlabAppType = System.Type.GetTypeFromProgID("Matlab.Application");
            matlab = System.Activator.CreateInstance(matlabAppType) as MLApp.MLApp;
            string path_project = Directory.GetCurrentDirectory();
            string path_matlab = "cd('" + path_project + "\\SatSimV1.1" + "')";
            matlab.Execute(path_matlab);
            matlab.Execute("clear all");
            matlab.Execute("close all");
            //<span style="color:#ff6666;">//这条语句也很重要,先注释掉,下面讲解</span>
            string command;

            command = @"[s]=A(1,2,3)";

            matlab.Execute(command);
            string result = matlab.Execute(command);//result作为是否执行成功的判决
            startload1 = new Thread(new ThreadStart(startload_run1));
            startload2 = new Thread(new ThreadStart(startload_run2));
            startload3 = new Thread(new ThreadStart(startload_run3));
            //运行线程方法
            startload1.Start();
            startload2.Start();
            startload3.Start();

        }

        void startload_run1()
        {
            int count50ms = 0;
            while (figure1 == IntPtr.Zero)
            {
                figure1 = FindWindow("SunAwtFrame","figure 10");
                Thread.Sleep(50);
                count50ms++;
                if (count50ms >= 4000)
                {
                    string label1 = "matlab资源加载时间过长!";
                    return;
                }
            }
            UpdateUI1 update1 = delegate
            {
                SetParent(figure1,panel1.Handle);
                var style = getwindowlong(figure1,GWL_STYLE);
                SetwindowLong(figure1,GWL_STYLE,style & ~WS_CAPTION & ~WS_THICKFRAME);
                MoveWindow(figure1,400 + 0,300 + 0,true);
            };
            panel1.Invoke(update1);
            Thread.Sleep(100);
            MoveWindow(figure1,true);
        }

        void startload_run2()
        {
            int count50ms = 0;
            while (figure2 == IntPtr.Zero)
            {
                figure2 = FindWindow("SunAwtFrame","figure 20");
                Thread.Sleep(50);
                count50ms++;
                if (count50ms >= 4000)
                {
                    string label2 = "matlab资源加载时间过长!";
                    return;
                }
            }
            UpdateUI2 update2 = delegate
            {
                SetParent(figure2,panel2.Handle);
                var style = getwindowlong(figure2,GWL_STYLE);
                SetwindowLong(figure2,style & ~WS_CAPTION & ~WS_THICKFRAME);
                MoveWindow(figure2,true);
            };
            panel2.Invoke(update2);
            Thread.Sleep(100);
            MoveWindow(figure2,true);
        }

        void startload_run3()
        {
            int count50ms = 0;
            while (figure3 == IntPtr.Zero)
            {
                figure3 = FindWindow("SunAwtFrame","figure 30");
                Thread.Sleep(50);
                count50ms++;
                if (count50ms >= 4000)
                {
                    string label3 = "matlab资源加载时间过长!";
                    return;
                }
            }
            UpdateUI3 update3 = delegate
            {
                SetParent(figure3,panel3.Handle);
                var style = getwindowlong(figure3,GWL_STYLE);
                SetwindowLong(figure3,style & ~WS_CAPTION & ~WS_THICKFRAME);
                MoveWindow(figure3,true);
            };

            Thread.Sleep(100);
            MoveWindow(figure3,true);
        }

        private void Form1_SizeChanged(object sender,EventArgs e)
        {
            MoveWindow(figure1,panel1.Width + 0,panel1.Height + 0,true);
            MoveWindow(figure2,panel2.Width + 0,panel2.Height + 0,true);
            MoveWindow(figure3,panel3.Width + 0,panel3.Height + 0,true);
        }
    }
}   

尝试过绘画事件,这不是绘画的原因,请帮助查看是否是捕获的图像消失的原因。在最大化和最小化的过程中,图像有时消失而有时不消失。手动拖动和缩放时,图像将消失。是什么原因难以解释。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)