嵌入類
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; } }