VB 写控制台程序(转)

Karl E. Peterson's Classic VB Code: Console

Console

Description

The whole enchilada. If you want to write a real console application, using Classic VB, this is the ticket. Create a new project, set it to start with Sub Main, drop the MConsole.bas file into your application, and you're almost ready to rock. This sample provides complete support for writing and testing console applications within the IDE.

MConsole is designed as a lightweight COM object, using techniques developed by Matt Curland, and published in his book Advanced Visual Basic 6. This means you only need to initialize the object, and from there it handles teardown itself at application completion. The lightweight console object creates its own console window for output to while running within the IDE, which eliminates the need to compile before each test, and makes interactive debugging a possibility for the first time.

All the standard output functionalities you would expect are available. In this little example, you can see how easy it is to initialize the console, and start writing to it. This snippet even changes the background and foreground colors, as well as the command window caption:

Public Sub Main()
   Dim sName As String
   Dim fColor As Long, bColor As Long
   Dim sCaption As String

   ' Required in all MConsole.bas supported apps!
   Con.Initialize

   ' Stash value(s) we'll later reset.
   bColor = Con.BackColor
   fColor = Con.ForeColor
   sCaption = Con.Title

   ' Read and write a simple response:
   If Con.Height < 50 Then Con.Height = 50
   Con.ForeColor = conGreenHi
   Con.WriteLine "What's your name? ", False
   Con.ForeColor = fColor
   sName = Con.ReadLine()
   Con.ForeColor = conGreenHi
   Con.WriteLine "Hello " & sName, False
   Con.Title = "Console Demo for " & sName

   ' Restore original console colors and caption.
   Con.BackColor = bColor
   Con.ForeColor = fColor
   Con.Title = sCaption
End Sub

Of course, redirection and pipes are fully supported as well. Here's a snippet showing how to read standard input, and send it directly to the clipboard. (This is the heart of a neat little utility I wrote that allows you to capture the output of any console application and place it on the clipboard.) This snippet also demonstrates output of debugging information, output to standard error, and the issuance of an exitcode upon completion:

Public Sub Main()
   Dim sData As String
   Dim sMessage As String

   ' Required in all MConsole.bas supported apps!
   Con.Initialize

   ' Check to see if we have any waiting input.
   If Con.Piped Then
      ' Slurp it all in a single stream.
      sData = Con.ReadStream()
      ' Just to prove we did it, place text on clipboard.
      Clipboard.Clear
      Clipboard.SetText sData
      ' Write some debugging information.
      Con.DebugOutput "Wrote " & CStr(Len(sData)) & _
                      " characters to clipboard."
   Else
      sMessage = "No redirection detected; nothing to read?"
      ' Send error condition to Standard Error!
      Con.WriteLine sMessage, True, conStandardError
      Con.DebugOutput sMessage
      ' Set an exit code appropriate to this error.
      ' Application MUST BE COMPILED TO NATIVE CODE to
      ' avoid a GPF in the runtime!!!
      Con.ExitCode = 1
   End If
End Sub

There sample is probably one of the most comprehensive available on this site. There are uncountable uses for it, as it covers just about every conceivable aspect of console application authoring. Please let me know if you feel I've overlooked anything!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值