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 SubOf 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 SubThere 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!
这篇博客介绍了如何使用Karl E. Peterson的Classic VB Code创建真正的控制台应用程序。MConsole是一个轻量级COM对象,它在IDE中提供完整的控制台应用支持,包括输出、输入、调试和错误处理功能。示例代码展示了初始化控制台、改变颜色、读写输入输出以及处理标准输入和错误输出的方法。
6006

被折叠的 条评论
为什么被折叠?



