怎样在一个父窗体中把其他程序(如计算器)作为子窗体运行呢?

解决方案 »

  1.   

    设置这个属性form1.MdiParent=this;//this 为主窗体
      

  2.   


                Form1 form1 = new Form1();
                form1.MdiParent = this;
                form1.Show();
      

  3.   

    当然不是计算器了。用vb我能实现,c#不行么?
    form1.MdiParent=this;这个对自己写的mdi可以,别的程序不行吧。
      

  4.   

    “别的程序不行”,楼主是指另外的exe,当然不行;如果是dll,是可以的
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Threading;/// <summary>
            /// 
            /// </summary>
            /// <param name="hChild"></param>
            /// <param name="hParent"></param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);        /// <summary>
            /// 
            /// </summary>
            /// <param name="lpClassName"></param>
            /// <param name="lpWindowName"></param>
            /// <returns></returns>
            [DllImport("user32.dll", EntryPoint = "FindWindow")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    /// <summary>
            /// 将计算器和记事本加入到当前的MDI中
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                Process ps = new Process();
                ps.StartInfo.FileName = "Notepad.exe";            
                ps.Start();
                for (int i = 0; i < 3; i++)
                {
                    Thread.Sleep(1000);
                    Application.DoEvents();
                }
                IntPtr Hwnd = FindWindow(null, "无标题 - 记事本");
                SetParent(Hwnd, this.Handle);
                Process ps1 = new Process();
                ps1.StartInfo.FileName = "Calc.exe";
                ps1.Start();
                for (int i = 0; i < 3; i++)
                {
                    Thread.Sleep(1000);
                    Application.DoEvents();
                }
                IntPtr Hwnd1 = FindWindow(null, "计算器");
                SetParent(Hwnd1, this.Handle);
            }
      

  6.   

    上面的代码我刚写的测试了,没有问题延迟你自己看着设置,我设置了三秒,也许不需要那么长。。太短的话程序还没有启动,加入MDI会失败