winform窗体里面怎么打开exe程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform窗体里面怎么打开exe程序相关的知识,希望对你有一定的参考价值。

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "要调用的exe名称";
process.StartInfo.WorkingDirectory = path//要掉用得exe路径例如:"C:\windows";
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();//无限制的等待,看自己情况要不要加 追问还是在窗体外面显示呀?我把窗体设为了MDI窗体。。。

参考技术A private void toolStripButton1_Click(object sender, EventArgs e)

System.Diagnostics.Process p = System.Diagnostics.Process.Start("calc");//notepad");

p.WaitForInputIdle();

SetParent(p.MainWindowHandle, this.Handle);

ShowWindowAsync(p.MainWindowHandle, 3);

[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
或者
[DllImport("user32.dll")]
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);

写在方法里,
System.Diagnostics.Process.Start("calc.exe");
System.Diagnostics.Process.Start("winword.exe.exe");
System.Diagnostics.Process.Start("excel.exe");
System.Diagnostics.Process.Start("notepad.exe");
SetParent(FindWindow(null, "计算器"), this.Handle.ToInt32());//FindWindow(null, "计算器")第一个参数是类名,第二个是标题名
只能是这几个可以,其他的程序就不行
System.Diagnostics.Process.Start("calc.exe");
System.Diagnostics.Process.Start("winword.exe.exe");
System.Diagnostics.Process.Start("excel.exe");
System.Diagnostics.Process.Start("notepad.exe");来自:求助得到的回答
参考技术A 判断con的状态是否为open(),con.State==ConnectionState.Open,如果是则提示成功,否则失败;
参考技术B 汗~~~~~不是一行代码就OK么写这么多不环保哟
Process.Start("calc");
参考技术C System.Diagnostics.Process.Start("绝对路径"); 参考技术D System.Diagnostics.Process.Start("路径");

C#将exe运行程序嵌入到自己的winform窗体中

以下例子是将Word打开,然后将它嵌入到winform窗体中,效果如下图:
C将exe运行程序嵌入到自己的winform窗体中 - kingmax_res - iSport
注意:该方法只适用于com的exe(如word,Excel之类),.net的编的exe就不能用这用方法嵌入到窗体中。

技术分享

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsTest
{
    public partial class Form2 : Form
    {
        Process process = null;
        IntPtr appWin;
        private string exeName = "";
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
            CharSet = CharSet.Unicode, ExactSpelling = true,
            CallingConvention = CallingConvention.StdCall)]
        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
        private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
        //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [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);
        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);
        private const int SWP_NOOWNERZORDER = 0x200;
        private const int SWP_NOREDRAW = 0x8;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int WS_EX_MDICHILD = 0x40;
        private const int SWP_FRAMECHANGED = 0x20;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_ASYNCWINDOWPOS = 0x4000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;
        public string ExeName
        {
            get
            {
                return exeName;
            }
            set
            {
                exeName = value;
            }
        }

        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.exeName = @"D:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe";
            try
            {
                // Start the process 
                process = System.Diagnostics.Process.Start(this.exeName);
                // Wait for process to be created and enter idle condition 
                process.WaitForInputIdle();
                // Get the main handle
                appWin = process.MainWindowHandle;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error");
            }
            // Put it into this form
            SetParent(appWin, this.Handle);
            // Remove border and whatnot
            // SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
            // Move the window to overlay it on this window
            MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                process.Kill();
            }
            catch { }
        }
        private void Form2_Resize(object sender, EventArgs e)
        {
            if (this.appWin != IntPtr.Zero)
            {
                MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
            }
            //base.OnResize(e);
        }
    }
}

  

以上是关于winform窗体里面怎么打开exe程序的主要内容,如果未能解决你的问题,请参考以下文章

C#开发的winform程序,窗体打开的速度很慢,请高手指点。。谢了先。。。

C#开发的winform程序,窗体打开的速度很慢,请高手指点。。谢了先。。。

C# winform程序,怎么实现菜单的功能?

浏览器怎么给winform传值

C#WINFORM窗体怎么执行CMD命令?

winform 怎么让子窗体显示在主窗体上?