Run or Embed VB.NET console application within a VB.NET or C# forms application

血红的双手。 提交于 2021-02-11 06:40:59

问题


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

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.

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

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).

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

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


回答1:


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 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.



来源:https://stackoverflow.com/questions/27707433/run-or-embed-vb-net-console-application-within-a-vb-net-or-c-sharp-forms-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!