如何打印到DOS屏幕?

soybean208 2002-09-26 03:27:46
我想做一个命令行方式的程序,如何让提示信息在DOS命令行方式下显示?
...全文
24 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
laolin 2002-09-26
  • 打赏
  • 举报
回复
1. 创建一个EXE程序,加入一个Module1。然后在Module1中加入以下代码:
-----------------------------------------------------------
Option Explicit

Declare Function AllocConsole Lib "kernel32" () As Long
Declare Function FreeConsole Lib "kernel32" () As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) _
As Long
Declare Function GetStdHandle Lib "kernel32" (ByVal _
nStdHandle As Long) As Long
Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" _
(ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal _
nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _
lpReserved As Any) As Long
Public Const STD_OUTPUT_HANDLE = -11&
-----------------------------------------------------------

2.在Form1中加入一个Button1,然后加入以下代码:
-----------------------------------------------------------
Dim hConsole As Long

Private Sub Command1_Click()
Dim Result As Long, sOut As String, cWritten As Long

sOut = "Hi There" & vbCrLf
Result = WriteConsole(hConsole, ByVal sOut, Len(sOut), cWritten, _
ByVal 0&)
End Sub

Private Sub Form_Load()
If AllocConsole() Then
hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsole = 0 Then MsgBox "Couldn't allocate STDOUT"
Else
MsgBox "Couldn't allocate console"
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
CloseHandle hConsole
FreeConsole
End Sub
-----------------------------------------------------------

3.编译成EXE程序,并执行EXE程序后点击Button,即可看到DOS命令窗口中出现"Hi There"。

至于如何实现没有Form标准输入输出,你可在上述的程序基础上做些扩展。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧