如何:为 Windows 窗体应用程序启用批处理模式 (Visual Basic)
此示例使用 My.Application.Startup 事件检查应用程序是否通过作为参数的 /batch 字符串启动。
为 Window 窗体应用程序启用批处理模式
在**“解决方案资源管理器”中选择一个项目。 在“项目”菜单上,单击“属性”**。
在**“应用程序”选项卡上,单击“查看应用程序事件”**以打开代码编辑器。
创建处理 Startup 事件的方法。 有关更多信息,请参见如何:处理应用程序事件 (Visual Basic)。
Private Sub MyApplication_Startup( ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs ) Handles Me.Startup
End Sub
循环访问应用程序的命令行参数,如果其中的一个参数为 /batch,则将 e 对象的 Cancel 属性设置为 True。
当将 Cancel 属性设置为 True 后,启动窗体将不会启动。
For Each s As String In My.Application.CommandLineArgs If s.ToLower = "/batch" Then ' Stop the start form from loading. e.Cancel = True End If Next
如果将 e 对象的 Cancel 属性设置为 True,则调用主例程以执行无窗口操作。
If e.Cancel Then ' Call the main routine for windowless operation. Dim c As New BatchApplication c.Main() End If
示例
Private Sub MyApplication_Startup(
ByVal sender As Object,
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
) Handles Me.Startup
For Each s As String In My.Application.CommandLineArgs
If s.ToLower = "/batch" Then
' Stop the start form from loading.
e.Cancel = True
End If
Next
If e.Cancel Then
' Call the main routine for windowless operation.
Dim c As New BatchApplication
c.Main()
End If
End Sub
Class BatchApplication
Sub Main()
' Insert code to run without a graphical user interface.
End Sub
End Class