【问题标题】:C# open other program in form panelC#在表单面板中打开其他程序
【发布时间】:2018-04-17 14:51:28
【问题描述】:

您好,我是韩国的初学者。

我正在制作。程序。程序在一个窗体上运行三个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);


        }

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

【问题讨论】:

  • 您的catch 块中有什么?你有什么错误吗?您是否尝试过以管理员身份运行?

标签: c# process exe panel


【解决方案1】:

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

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

【讨论】:

  • 哦,真的吗?我现在就试试!非常感谢。你想调用的exe程序没有代码就可以了吗?
  • 是的,如果程序集没有被混淆,您可以在没有代码的情况下执行此操作。如果是这样,您可能会遇到一些问题。使用ilspy.net 来确定 Main 函数正在做什么以及您需要从他们的应用程序中加载什么。
  • 好的,我试试!!谢谢
【解决方案2】:
Thread.Sleep(100);
SetParent(p.MainWindowHandle, this.Handle); <-- panel1.Handle
SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);

【讨论】:

  • 您好,欢迎来到 Stackoverflow。您可以添加解释来补充您的代码吗?没有备注的代码转储在这里通常不受欢迎。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
相关资源
最近更新 更多