zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    使用边缘计算来改变5G世界中的网络
    解开关于人工智能的六个迷思
    哪些数据将成为区块链系统的关键数据?
    如何通过7个步骤构建机器学习模型
    人工智能的发展体现了人类社会由实向虚的趋势
    5G技术与人工智能的智能结合
    量子计算总是混合的,这需要不断协调
    7.5省队集训 tree
    bzoj2989&4170: 数列
    bzoj1010: [HNOI2008]玩具装箱toy
  • 原文地址:https://www.cnblogs.com/SoftWareIe/p/9781071.html
Copyright © 2011-2022 走看看