如何在不显示控制台窗口的情况下运行控制台应用程序

时间:2014-02-18 21:45:32

标签: vb.net visual-studio

我已经用以下子主题编写了一个应用程序:

 Public Sub Main()
  Dim Value As String() = Environment.GetCommandLineArgs
  Dim F As Form
  Select Case Value.Last.ToLower
    Case "-character"
      F = New frmCharacterSheet
    Case "-viewer"
      F = New frmClient
    Case Else
      F = New frmCombat
  End Select
  Application.Run(F)
End Sub

这是因为我希望能够根据命令行使用三种不同的启动模式安装我的应用程序。我确实有一个表单可以做到这一点,但这使得错误捕获非常困难,因为主表单只报告错误。

此控制台似乎运行良好,但我不希望用户在启动时看到黑色控制台屏幕。

我已经搜索了答案,但大多数解决方案是“切换回Windows窗体应用程序”。出于上述原因,我不想这样做。 (我在winforms启动情况下无法使用application.run(f),因为我遇到了线程错误。

我需要知道如何隐藏控制台窗口,或者如何编写将启动其他三种形式之一的主菜单(但使它们成为启动形式)。

任何帮助都将不胜感激....

4 个答案:

答案 0 :(得分:3)

尝试:

Private Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
Private Declare Auto Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
Private Const SW_HIDE As Integer = 0

Sub Main()
    Dim hWndConsole As IntPtr
    hWndConsole = GetConsoleWindow()
    ShowWindow(hWndConsole, SW_HIDE)
    'continue your code
End Sub

它有副作用,窗口将显示,然后立即隐藏

瓦尔特

答案 1 :(得分:3)

"或者如何编写将启动其他三种形式之一的主菜单(但将它们作为启动形式)。"

从标准 WinForms 项目开始,然后使用Application.Startup()事件。从那里,您可以检查启动参数,然后通过将所需的实例分配给" My.Application.MainForm"来动态更改启动表单。这将导致该表单加载,就好像它是最初分配给"启动表单"条目。

点击项目 - >属性 - >应用程序选项卡 - > "查看应用程序事件"按钮(右下角;向下滚动)。 从"(常规)"更改左下拉列表to"(MyApplication Events)"。 更改"声明"的正确下拉列表到" Startup"。

简化代码:

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
            If True Then
                My.Application.MainForm = New Form1 ' <-- pass your desired instance to MainForm
            End If
        End Sub

    End Class

End Namespace

答案 2 :(得分:1)

转到Project Properties> Application> Application Type>并选择Windows Forms Application

此时,ConsoleApplication完全不可见,没有用户界面。

答案 3 :(得分:0)

我只想添加另一个解决方案,尽管Idle_Mind已经提供了一个很好的解决方案。这表明您可以在WinForms应用程序中使用Application.Run(Form)

Public Class Form1

    Private Shared applicationThread As New Threading.Thread(AddressOf Main)

    Private Shared Sub Main()
        Dim myForm As Form
        Dim config = 2 ' if 3, will run Form3
        Select Case config
            Case 2
                myForm = New Form2
            Case 3
                myForm = New Form3
            Case Else
                MessageBox.Show("Bad config!")
                Exit Sub
        End Select
        Application.Run(myForm)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        applicationThread.Start()
        ' immediately dispose Form1 so it's not even shown
        Dispose()
    End Sub

End Class