码迷,mamicode.com
首页 > Windows程序 > 详细

C# winform 将其他程序嵌入Form窗体

时间:2018-10-13 02:44:04      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:.text   cut   log   ati   lse   user   dial   vat   eve   

嵌入类

public class ExeImpaction
    {
        public void FrmClosing()
        {
            try
            {
                if (!process.HasExited)
                    process.Kill();
            }
            catch
            {
            }
        }

        public void FrmResize(Form frm)
        {
            if (this.appWin != IntPtr.Zero)
                MoveWindow(appWin, 0, 0, frm.Width, frm.Height, true);
        }

        Process process = null;
        IntPtr appWin;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);


        /// <summary>
        /// 打开一个exe程序 并嵌入到窗体
        /// </summary>
        /// <param name="parentFrm">父类窗体</param>
        /// <param name="exePath">程序路径</param>
        /// <param name="frmCaption">窗体名称</param>
        /// <param name="args">参数们</param>
        /// <param name="waitSecond">等待时间(单点登录时如果窗体还没创建得到句柄是0无法显示窗体要等到他生成再显示)</param>
        /// <returns></returns>
        public string ExecExe(Form parentFrm, string exePath, string frmCaption, string args = "", int waitSecond = 1)
        {
            string errStr = "";
            try

            {
                ProcessStartInfo info = null;

                if ((args ?? "").ToString() != "")
                    info = new ProcessStartInfo(exePath, args);
                else
                    info = new ProcessStartInfo(exePath);

                info.UseShellExecute = true;
                info.WindowStyle = ProcessWindowStyle.Minimized;

                process = System.Diagnostics.Process.Start(info);

                Thread.Sleep(waitSecond * 1000);

                process.WaitForInputIdle();

                appWin = FindWindow(null, frmCaption);

                appWin = FindWindow(null, frmCaption);

            }
            catch (Exception ex)
            {
                errStr = ex.Message;
            }

            SetParent(appWin, parentFrm.Handle);
            MoveWindow(appWin, 0, 0, parentFrm.Width, parentFrm.Height, true);

            return errStr;
        }

    }

用法

public partial class NewTest : Form
    {
        public ExeImpaction exeIm { get; set; }
        public NewTest()
        {
            InitializeComponent();

            string path = System.Environment.CurrentDirectory+ @"\人事管理\ProRSGL.exe";
            textBox1.Text = path;
            textBox2.Text = "人事管理系统";

            exeIm = new ExeImpaction();
            this.Resize += NewTest_Resize;
            this.FormClosing += NewTest_FormClosing;
        }

        private void NewTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            exeIm.FrmClosing();
        }

        private void NewTest_Resize(object sender, EventArgs e)
        {
            exeIm.FrmResize(this);
        }
                

        private void button1_Click(object sender, EventArgs e)
        {
             exeIm.ExecExe(this, @textBox1.Text, @textBox2.Text, "000,891560,1808290021,123124121243123123", 3);//vb程序单点登录这里的参数自己定义的
            //其他程序
             //exeIm.ExecExe(this, @textBox1.Text, @textBox2.Text, @textBox3.Text, 1);
        }

        private void newFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Form();
            frm.MdiParent = this;
            frm.Show();
        }

        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // Process process = System.Diagnostics.Process.Start(@textBox1.Text);
            Process process = System.Diagnostics.Process.Start(@"D:\我的项目\门诊西药房管理系统\门诊西药房管理系统.exe", "000,891560,1808290021,123124121243123123");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                textBox1.Text = ofd.FileName;
        }
    }

 

C# winform 将其他程序嵌入Form窗体

标签:.text   cut   log   ati   lse   user   dial   vat   eve   

原文地址:https://www.cnblogs.com/SoftWareIe/p/9781071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!