C# 在窗体面板中打开其他程序

标签 c# process exe panel

您好,我是韩国的一名初学者开发人员。

我正在做。程序。程序在一个窗体上运行三个exe(c++、c#开发)并将程序放入面板中。

现在我把C++或者其他语言的程序放到面板里。然而,用C#构建的Windows程序不会保留在面板上。 这是我的代码的一部分:

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    static extern bool MoveWindow(IntPtr Handle, int x, int y, int w, int h, bool repaint);

    static readonly int GWL_STYLE = -16;
    static readonly int WS_VISIBLE = 0x10000000;


    private void Form1_Load(object sender, EventArgs e)
    {

        try
        {
            Process p = Process.Start("C:\\Users\\sonmi\\OneDrive\\Japan_Bunge_alpha\\Japan_Bunge_alpha\\bin\\Debug\\Japan_Bunge_alpha"); //C# program
            Process p2 = Process.Start("C:\\Users\\sonmi\\Desktop\\astana_test\\astana\\Server\\Server_CES"); //C++ program

            p.WaitForInputIdle();

            Thread.Sleep(100);
            SetParent(p.MainWindowHandle, this.Handle);
            SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
            MoveWindow(p.MainWindowHandle, 0, 0, panel1.Width, panel1.Height, true);


            p2.WaitForInputIdle();
            SetParent(p2.MainWindowHandle, panel2.Handle);
            SetWindowLong(p2.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
            MoveWindow(p2.MainWindowHandle, 0, 0, panel2.Width, panel2.Height, true);


        }

我现在已经使用了两个面板,但测试结束后我会添加更多面板。 不知道问题出在哪里。

最佳答案

对于 C# 应用程序,您可以将应用程序加载到当前 AppDomain 中并将其视为库。为此,将其添加到您的项目引用中。如何初始化并将其放入面板将取决于引用的应用程序的设计方式。

您可能必须自己初始化该应用程序的主窗体并以这种方式设置父级。

关于C# 在窗体面板中打开其他程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120055/

相关文章:

c# - .NET MVC 路由 - 路由开始时的包罗万象?

c# - Microsoft Access 数据库引擎找不到对象“Sheet1$”

python - 与 Python 3 中的进程 ID 混淆

c# - 评论存储在哪里以及如何提取它们?

c# - c#.net 中用于 winforms 的气球类

process - Keras "pickle_safe": What does it mean to be "pickle safe", 或者 Python 中的 "non picklable"?

powershell - 将参数传递给Powershell Start-Job

python 3.4 py2exe image.open 找不到图像

java - 如何将 Java 程序转换为 .exe 文件?

c++ - 如何从 dll 内部找到 dll 的父 exe?