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

嵌入类

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:\我的项目\application1.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;
        }
    }

 

转载于:https://www.cnblogs.com/SoftWareIe/p/9781071.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值