在VB.NET或C#表单应用程序中运行或嵌入VB.NET控制台应用程序

在VB.NET或C#表单应用程序中运行或嵌入VB.NET控制台应用程序

问题描述:

我为管理/管理功能编写了许多相关的VB.NET控制台应用程序.

I have a bunch of related VB.NET console applications I've written for admin/management functions.

我现在想将它们放在一个可以用作启动器的Windows Forms应用程序下,但我不想将这些应用程序作为单独的程序启动,而是希望将这些应用程序嵌入Windows窗体应用程序中,例如在左侧下方有一个可滚动的图标列表-单击一个图标将在Windows Forms应用程序的右侧窗格中启动关联的.exe.

I now want to bring them together under a single Windows Forms application that works as a launcher, but instead of launching the apps as seperate programs, I want to embed the apps in the windows forms app, e.g. have a scrollable list of icons down the left hand side - clicking on one will launch the associated .exe in the right hand pane of the windows forms app.

目前,我有一个带有一个面板的简单的单一表单应用程序,因此我可以测试我想做的事是否可行.

For now I have a simple single form application with one panel, so I can test out if what I want to do is possible.

在阅读了有关MSDN和SO的文章之后,我发现了与此相关的其他文章,但似乎都没有满足我的要求.

Having read around MSDN and SO, I've found other posts about this but none that seem to do what I want.

我不想拦截stin/stdout/stderr流并尝试模拟控制台应用程序,我想实际运行已编译的exe(可以实现),但可以在Windows窗体中运行它(可以无法实现,它是一个单独的过程.

I don't want to intercept stin/stdout/stderr streams and attempt to emulate the console application, I want to actually run the compiled exe (which I can achieve) but have it run inside the windows form (which I can't achieve, it launches as a seperate process).

我现在拥有的Form1的主要代码是:

The main code for Form1 that I have right now is:

Public Class Form1
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Dim proc As Process

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    proc = Process.Start("\\fileserver\datashare\path\WIN_MGR.exe")
    proc.WaitForInputIdle()
    SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
End Sub

End Class

任何人都可以让我朝正确的方向实际运行Windows窗体中窗口化的.exe应用程序吗?

Anyone able to put me in the right direction to actually run the .exe application windowed within the windows form?

基于注释,如下更改您的 Form_Load :

Based on the comments, change your Form_Load as follows:

proc = Process.Start("\\fileserver\datashare\path\WIN_MGR.exe")
Thread.Sleep(1000)  'Possibly replace this with polling MainWindowHandle
SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

WaitForInputIdle 失败,因为控制台应用程序没有图形界面.为了获得更准确的延迟, Idle_Mind 建议在while循环中轮询 MainWindowHandle 直到它返回除 IntPtr.Zero 之外的其他值.

WaitForInputIdle fails because the console application does not have a graphical interface. For a more accurate delay, Idle_Mind suggests polling MainWindowHandle in a while-loop until it returns a value other than IntPtr.Zero.