最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#的winform如何嵌套另一個(gè)exe程序

 更新時(shí)間:2023年06月16日 10:59:42   作者:故里2130  
這篇文章主要介紹了C#的winform如何嵌套另一個(gè)exe程序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C#winform嵌套另一個(gè)exe程序

一共有二種方法,也不知道作者從哪里復(fù)制來(lái)的,先感謝原作者。

首先建立一個(gè)程序,加2個(gè)按鈕,為了區(qū)分,界面修改成紅色。

第一種

1.建立一個(gè)主程序,加一個(gè)panel1,為了區(qū)分背景是綠色

2.代碼調(diào)用

3.所有代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 嵌入外部exe
        /// </summary>
        public class EmbeddedExeTool
        {
            [DllImport("User32.dll", EntryPoint = "SetParent")]
            private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
            [DllImport("user32.dll", EntryPoint = "ShowWindow")]
            private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
            [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")]
            private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
            [DllImport("user32.dll")]
            private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            IntPtr WindowHandle = IntPtr.Zero;
            private const int WS_THICKFRAME = 262144;
            private const int WS_BORDER = 8388608;
            private const int GWL_STYLE = -16;
            private const int WS_CAPTION = 0xC00000;
            private Process proApp = null;
            private Control ContainerControl = null;
            private const int WS_VISIBLE = 0x10000000;
            [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
            private static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
            [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
            private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
            private IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
            {
                if (IntPtr.Size == 4)
                {
                    return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
                }
                return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
            }
            /// <summary>
            /// 加載外部exe程序到程序容器中
            /// </summary>
            /// <param name="control">要顯示exe的容器控件</param>
            /// <param name="exepath">exe的完整絕對(duì)路徑</param>
            public void LoadEXE(Control control, string exepath)
            {
                ContainerControl = control;
                control.SizeChanged += Control_SizeChanged;
                ProcessStartInfo info = new ProcessStartInfo(exepath);
                info.WindowStyle = ProcessWindowStyle.Minimized;
                info.UseShellExecute = false;
                info.CreateNoWindow = false;
                proApp = Process.Start(info);
                Application.Idle += Application_Idle;
                EmbedProcess(proApp, control);
            }
            /// <summary>
            /// 加載外部exe程序到程序容器中
            /// </summary>
            /// <param name="form">要顯示exe的窗體</param>
            /// <param name="exepath">exe的完整絕對(duì)路徑</param>
            public void LoadEXE(Form form, string exepath)
            {
                ContainerControl = form;
                form.SizeChanged += Control_SizeChanged;
                proApp = new Process();
                proApp.StartInfo.UseShellExecute = false;
                proApp.StartInfo.CreateNoWindow = false;
                proApp.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
                proApp.StartInfo.FileName = exepath;
                proApp.StartInfo.Arguments = Process.GetCurrentProcess().Id.ToString();
                proApp.Start();
                Application.Idle += Application_Idle;
                EmbedProcess(proApp, form);
            }
            /// <summary>
            /// 確保應(yīng)用程序嵌入此容器
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Application_Idle(object sender, EventArgs e)
            {
                if (this.proApp == null || this.proApp.HasExited)
                {
                    this.proApp = null;
                    Application.Idle -= Application_Idle;
                    return;
                }
                if (proApp.MainWindowHandle == IntPtr.Zero) return;
                Application.Idle -= Application_Idle;
                EmbedProcess(proApp, ContainerControl);
            }
            /// <summary>
            /// 將指定的程序嵌入指定的控件
            /// </summary>
            private void EmbedProcess(Process app, Control control)
            {
                // Get the main handle
                if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
                try
                {
                    // Put it into this form
                    SetParent(app.MainWindowHandle, control.Handle);
                    // Remove border and whatnot               
                    SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
                    ShowWindow(app.MainWindowHandle, (int)ProcessWindowStyle.Maximized);
                    MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
                }
                catch (Exception ex3)
                {
                    Console.WriteLine(ex3.Message);
                }
            }
            /// <summary>
            /// 嵌入容器大小改變事件
            /// </summary>
            private void Control_SizeChanged(object sender, EventArgs e)
            {
                if (proApp == null)
                {
                    return;
                }
                if (proApp.MainWindowHandle != IntPtr.Zero && ContainerControl != null)
                {
                    MoveWindow(proApp.MainWindowHandle, 0, 0, ContainerControl.Width, ContainerControl.Height, true);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            EmbeddedExeTool exetool = new EmbeddedExeTool();
            //WindowsFormsApp4.exe 為要嵌入外部exe的具體路徑
            exetool.LoadEXE(panel1, AppDomain.CurrentDomain.BaseDirectory+ "WindowsFormsApp4.exe");//debug下面的文件夾
        }
    }
}

4.效果很好。

紅配綠,絕配。 

第二種

和第一種方式有點(diǎn)不一樣。 

1.建立一個(gè)winform主程序,為了區(qū)分背景是綠色

2.把代碼直接復(fù)制進(jìn)去,修改命名空間,再加上一個(gè)load事件即可

3.代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 0x10000000;
        EventHandler appIdleEvent = null;
        Action<object, EventArgs> appIdleAction = null;
        Process m_AppProcess;
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        //SetParent(IntPtr hWndChild, IntPtr hWndNewParent);這個(gè)方法很重要,就是將hWndChild指向開(kāi)啟exe的窗體嵌入到hWndNewParent窗體的某個(gè)控件上,或者是窗體本 身的容器
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
        // MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);這個(gè)方法是windows的api,見(jiàn)名知意,是移動(dòng)hwnd所指的窗體到指定的位置,并且指定是否需要重繪
        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }
        public Form1()
        {
            InitializeComponent();
            appIdleAction = new Action<object, EventArgs>(Application_Idle);
            appIdleEvent = new EventHandler(appIdleAction);
        }
        /// <summary>
        /// 確保應(yīng)用程序嵌入此容器,再次確認(rèn)exe嵌入,如果不調(diào)用這個(gè)方法,程序不一定能嵌入外部exe
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Application_Idle(object sender, EventArgs e)
        {
            if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
            {
                this.m_AppProcess = null;
                Application.Idle -= appIdleEvent;//這一步一直不知道有什么用,但是不用這行代碼程序有時(shí)候能嵌入有時(shí)候又不行
                return;
            }
            if (m_AppProcess.MainWindowHandle == IntPtr.Zero) return;
            Application.Idle -= appIdleEvent;
            EmbedProcess(m_AppProcess, this);
        }
        /// <summary>
        /// 將指定的程序嵌入指定的控件
        /// </summary>
        private void EmbedProcess(Process app, Control control)
        {
            // Get the main handle
            if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
            try
            {
                // Put it into this form
                SetParent(app.MainWindowHandle, control.Handle);
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.Message);
            }
            try
            {
                // Remove border and whatnot               
                SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
            }
            catch (Exception ex2)
            {
                Console.WriteLine(ex2.Message);
            }
            try
            {
                MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
            }
            catch (Exception ex3)
            {
                Console.WriteLine(ex3.Message);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //以下這段代碼是通過(guò)命令行方式調(diào)起一個(gè)exe程序,獲取這個(gè)程序的句柄然后嵌入主的winform窗體中
            ProcessStartInfo info = new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + "WindowsFormsApp4.exe");//debug下面的文件夾
            info.WindowStyle = ProcessWindowStyle.Minimized;
            info.UseShellExecute = false;
            info.CreateNoWindow = false;
            m_AppProcess = System.Diagnostics.Process.Start(info);
            Application.Idle += appIdleEvent;
            try
            {
                EmbedProcess(m_AppProcess, this);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

4.效果

可見(jiàn),第一種和第二種的效果有所區(qū)別的。

拓展。

WPF簽入winform

1.依然用上面的winform程序,把輸出類型改成類庫(kù)

  

2.建立一個(gè)WPF程序,引用System.Windows.Forms和WindowsFormsIntegration,紅色。綠色是步驟1生產(chǎn)的dll

3.在wpf中增加WindowsFormsHost控件

4.cs后臺(tái)代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WindowsFormsApp4;
namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Form1 mainform = new Form1();
            mainform.TopLevel = false;
            winform.Child = mainform;
        }
    }
}

5.效果

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)高性能文件批量處理器的示例代碼

    C#實(shí)現(xiàn)高性能文件批量處理器的示例代碼

    文件批量處理器可以用于數(shù)字資產(chǎn)管理,數(shù)據(jù)遷移工程,日志文件處理和安全審計(jì)場(chǎng)景,本文將使用C#開(kāi)發(fā)一個(gè)高性能文件批量處理器,希望對(duì)大家有所幫助
    2025-03-03
  • Unity UGUI的Toggle復(fù)選框組件使用詳解

    Unity UGUI的Toggle復(fù)選框組件使用詳解

    這篇文章主要為大家介紹了Unity UGUI的Toggle復(fù)選框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 基于C#實(shí)現(xiàn)的多線程文件上傳下載工具

    基于C#實(shí)現(xiàn)的多線程文件上傳下載工具

    本文介紹了如何設(shè)計(jì)和實(shí)現(xiàn)一個(gè)基于C#的多線程文件上傳和下載工具,支持FTP、SMTP、MSMQ和ActiveMQ通信,工具包含詳細(xì)的用戶界面設(shè)計(jì)、部署說(shuō)明、擴(kuò)展功能建議、性能測(cè)試數(shù)據(jù)以及注意事項(xiàng),需要的朋友可以參考下
    2026-03-03
  • C#函數(shù)式編程中的惰性求值詳解

    C#函數(shù)式編程中的惰性求值詳解

    這篇文章主要介紹了C#函數(shù)式編程中的惰性求值詳解,本文講解了惰性求值的相關(guān)知識(shí)并給出代碼實(shí)例,需要的朋友可以參考下
    2015-01-01
  • 解決Unity urp級(jí)聯(lián)陰影接縫問(wèn)題

    解決Unity urp級(jí)聯(lián)陰影接縫問(wèn)題

    通過(guò)從unity內(nèi)部函數(shù)中抽幾個(gè)出來(lái)改造,強(qiáng)制取某個(gè)裁切球的級(jí)聯(lián)陰影映射,通過(guò)案例給大家詳細(xì)介紹,文中給出了完整的urp shader代碼,對(duì)Unity級(jí)聯(lián)陰影知識(shí)感興趣的朋友一起看看吧
    2021-06-06
  • C#通過(guò)屬性名字符串獲取、設(shè)置對(duì)象屬性值操作示例

    C#通過(guò)屬性名字符串獲取、設(shè)置對(duì)象屬性值操作示例

    這篇文章主要介紹了C#通過(guò)屬性名字符串獲取、設(shè)置對(duì)象屬性值操作,結(jié)合實(shí)例形式總結(jié)分析了C#通過(guò)反射獲取對(duì)象屬性值并設(shè)置屬性值,獲取對(duì)象的所有屬性名稱及類型等相關(guān)操作技巧,需要的朋友可以參考下
    2020-03-03
  • C#畫筆Pen畫虛線的方法

    C#畫筆Pen畫虛線的方法

    這篇文章主要介紹了C#畫筆Pen畫虛線的方法,涉及C#畫筆Pen屬性的相關(guān)設(shè)置技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用雙檢鎖的示例代碼

    C#使用雙檢鎖的示例代碼

    本文主要介紹了C#使用雙檢鎖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-12-12
  • C#?獲取文件夾里所有文件名的詳細(xì)代碼

    C#?獲取文件夾里所有文件名的詳細(xì)代碼

    這篇文章主要介紹了C#?獲取文件夾里所有文件名,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法

    C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法

    這篇文章主要介紹了C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11

最新評(píng)論

登封市| 金寨县| 怀来县| 龙口市| 德昌县| 通道| 千阳县| 宣恩县| 九江市| 万荣县| 张家港市| 松原市| 田东县| 佛坪县| 安国市| 梨树县| 天柱县| 墨竹工卡县| 五台县| 崇明县| 册亨县| 缙云县| 高青县| 长宁区| 汾阳市| 卓尼县| 自贡市| 新沂市| 宿州市| 贡觉县| 天水市| 札达县| 北流市| 大安市| 磴口县| 饶平县| 兴义市| 龙游县| 贵定县| 孙吴县| 太湖县|