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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21866105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:25:59  来源:igfitidea点击:

How to run a console application without showing the console window

vb.netvisual-studio

提问by Tim

I have written an application with the following sub main:

我已经编写了一个具有以下子主要内容的应用程序:

 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

This is because I want to be able to install my app with three different startup modes based on the command line. I did have a form that did this, but this has made error trapping very hard because the main form just reports the error.

这是因为我希望能够基于命令行以三种不同的启动模式安装我的应用程序。我确实有一个执行此操作的表单,但这使得错误捕获变得非常困难,因为主表单仅报告错误。

This console seems to work well but I don't want the user to see the black console screen at startup.

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

I have searched for the answer but most solutions are 'switch back to a windows forms application'. I don't want to do this though for the above reason. (I cannot use application.run(f) in a winforms start situation because I get a threading error.

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

I need to know either how to hide the console window, or alternatively how to code a main menu that will launch one of the other three forms (but making them the startup form).

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

Any help would be appreciated....

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

回答by γηρ?σκω δ' αε? πολλ? διδασκ?με

Try:

尝试:

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

It has a side effect that the window will be shown and then immediately hidden

它有一个副作用是窗口将被显示然后立即隐藏

valter

瓦尔特

回答by Idle_Mind

"or alternatively how to code a main menu that will launch one of the other three forms (but making them the startup form)."

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

Start with a standard WinFormsProject and use the Application.Startup()event. From there you can check your startup parameters and then dynamically changethe Startup form by assigning your desired instance to "My.Application.MainForm". This will cause that form to load as if it was the one originally assigned to the "Startup Form" entry.

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

Click on Project --> Properties --> Application Tab --> "View Application Events" Button (bottom right; scroll down). Change the Left dropdown from "(General)" to "(MyApplication Events)". Change the Right dropdown from "Declarations" to "Startup".

单击项目 --> 属性 --> 应用程序选项卡 --> “查看应用程序事件”按钮(右下角;向下滚动)。将左侧下拉列表从“(General)”更改为“(MyApplication Events)”。将右侧下拉列表从“声明”更改为“启动”。

Simplified code:

简化代码:

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

回答by ElektroStudios

Just go to Project Properties> Application> Application Type>and select Windows Forms Application

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

At this point your ConsoleApplicationturns totally invisible, with no User-Interface.

在这一点上,您ConsoleApplication完全不可见,没有用户界面。

回答by djv

I just want to add another solution although Idle_Mind has already provided an excellent one. This demonstrates that you can use Application.Run(Form)inside a WinForms app.

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

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