在VB中创建一个控制台(如我的世界控制台)

时间:2014-08-21 13:33:25

标签: vb.net console

我需要在我的应用程序中创建一个控制台..... 我尝试过:

Public Class Form1
<DllImport("user32.dll")> Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim p As Process = Process.Start("java -jar C:\Server\Minecraft\Server.jar")
    Threading.Thread.Sleep(500)
    SetParent(p.MainWindowHandle, Panel1.Handle)

End Sub

但我只有不好的结果

请帮帮我

1 个答案:

答案 0 :(得分:2)

您可以使用Process类启动命令提示符(cmd.exe)。然后,您可以使用标准输入和标准输出与其进行通信。

首先在表单级别声明类型为Process的变量:

Private WithEvents MyProcess As Process

并初始化它。启动cmd.exe。

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    MyProcess = New Process
    With MyProcess.StartInfo
        .FileName = "CMD.EXE"
        .UseShellExecute = False
        .CreateNoWindow = True
        .RedirectStandardInput = True
        .RedirectStandardOutput = True
        .RedirectStandardError = True
    End With
    MyProcess.Start()
    MyProcess.BeginErrorReadLine()
    MyProcess.BeginOutputReadLine()
End Sub

然后,您可以使用Process类的StandardInput对象向其发送输入,并使用OutputDataReceived事件发送输入并从中获取输出。

Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    MessageBox.Show(e.Data)
End Sub

Private Sub ExecuteCommand()
    MyProcess.StandardInput.WriteLine("whatever command you want to send goes here...")
    MyProcess.StandardInput.Flush()
End Sub

此博客文章介绍了如何在表单中构建类似DOS的应用程序。

http://pradeep1210.wordpress.com/2010/02/04/launching-and-controlling-external-applications-from-vb-net-application/

我希望这正是您所寻找的。