且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

把一个窗体嵌入到WinForm中进行显示,以CMD窗口为例

更新时间:2022-09-15 13:47:06

1.添加引用

using System.Runtime.InteropServices;

2. 加入以下代码段

把一个窗体嵌入到WinForm中进行显示,以CMD窗口为例
        [DllImport("User32.dll ", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        
        private void btn_ThreadPool_Click(object sender, EventArgs e)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe ";
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//加上这句效果更好
            p.Start();
            System.Threading.Thread.Sleep(100);//加上,100 如果效果没有就继续加大
            SetParent(p.MainWindowHandle, panel1.Handle);
       //panel1.Handle为要显示外部程序的容器
            ShowWindow(p.MainWindowHandle, 3);

        }    
把一个窗体嵌入到WinForm中进行显示,以CMD窗口为例

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的***、好奇、充满创造力的想法被现实的框架所束缚,让创造力***成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。


    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/6042938.html,如需转载请自行联系原作者