• VBA 输出到CMD控制台显示暨更新当前行显示


    参考资料: 在VB控制台中更新当前行 |

    应用于:用 chrome + excel + VBA + XMLHTTP 爬视频网站 video 标签中的 blob:http m3u8 视频资源,ffmpeg 拼接资源_jessezappy的博客-CSDN博客

     之前的文章:两 API 三步最简实现 VB6 输出到CMD控制台显示 (含获取输入),真输出至 CMD 窗口,非 AllocConsole 模式_jessezappy的博客-CSDN博客_vbs输出内容到cmd窗口

    中,已实现输出控制台显示,但想在 VBA 中使用时就不行,查了下,发现需要加上:AllocConsole 用伪控制台方式就行,但注意 VB6 不能加这个

    下面放上 VBA 可用的模块定义:

    1. '--------所用API定义
    2. Public Declare Function AllocConsole Lib "kernel32" () As Long
    3. Public Declare Function FreeConsole Lib "kernel32" () As Long
    4. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    5. Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
    6. Public Declare Function LstrLen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
    7. 'Public Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
    8. 'Public Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal dwMode As Long) As Long
    9. Public Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
    10. Public Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
    11. Public Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
    12. '-------------常量定义
    13. '控制台输入输出句柄获取用常量
    14. Public Const STD_INPUT_HANDLE = -10&
    15. Public Const STD_OUTPUT_HANDLE = -11&
    16. Public Const STD_ERROR_HANDLE = -12&
    17. '部分前背景颜色代码,详见: 控制台色卡.png
    18. Public Const FOREGROUND_BLUE = 9
    19. Public Const FOREGROUND_GREEN = 10
    20. Public Const FOREGROUND_RED = 12
    21. Public Const FOREGROUND_INTENSITY = &H8
    22. Public Const BACKGROUND_BLUE = &H10
    23. Public Const BACKGROUND_GREEN = &H20
    24. Public Const BACKGROUND_RED = &H40
    25. Public Const BACKGROUND_INTENSITY = &H80
    26. '设置输入模式常量 SetConsoleMode (input)
    27. Public Const ENABLE_LINE_INPUT = &H2
    28. Public Const ENABLE_ECHO_INPUT = &H4
    29. Public Const ENABLE_MOUSE_INPUT = &H10
    30. Public Const ENABLE_PROCESSED_INPUT = &H1
    31. Public Const ENABLE_WINDOW_INPUT = &H8
    32. '设置输出模式常量 SetConsoleMode (output)
    33. Public Const ENABLE_PROCESSED_OUTPUT = &H1
    34. Public Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2
    35. '-----------所需全局变量
    36. Public hConsoleIn As Long ' 控制台输入句柄
    37. Public hConsoleOut As Long ' 控制台输出句柄
    38. 'Public hConsoleErr As Long ' 控制台错误句柄
    39. '==========================使用例子=========
    40. ' Call Initialize
    41. ''----------此处程序代码开始-------------
    42. ' Call setTitle(App.Title)
    43. ' Call setCONColor(FOREGROUND_GREEN, 0)
    44. '' Call COut("VB6 控制台:Hello World !" & vbCrLf)
    45. ' Call COut(App.Title & ",启动数据接收工作:" & vbCrLf)
    46. ' 'szUserInput = LIn
    47. ' 'Call COut(szUserInput)
    48. '
    49. '
    50. '
    51. ' If 1 = 1 Then '--调整此句决定有无窗口显示
    52. ' Load Form1
    53. ' Form1.Show
    54. ' Exit Sub
    55. ' End If
    56. ''----------程序代码到此结束-------------
    57. ' Call Terminate
    58. '==================================================
    59. '---------定义函数
    60. Public Sub Initialize() '---初始化获取句柄
    61. Call AllocConsole 'VBA 中使用。
    62. '获得控制窗口的句柄
    63. 'hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
    64. hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
    65. 'hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
    66. End Sub
    67. Public Sub Terminate() '-----销毁句柄
    68. Call FreeConsole ' Destroy the console
    69. 'Call CloseHandle(hConsoleIn)
    70. Call CloseHandle(hConsoleOut)
    71. End Sub
    72. Public Sub COut(szOut As String) '------文字输出到控制台函数
    73. WriteConsole hConsoleOut, szOut, LstrLen(szOut), vbNull, vbNull
    74. End Sub
    75. Public Sub setTitle(s As String)
    76. SetConsoleTitle s '设置窗口标题 '获得控制窗口的句柄
    77. End Sub
    78. Public Sub setCONColor(ByVal f As Long, ByVal b As Long) '---设置文字和背景显示颜色
    79. If b >= 0 And b <= 15 Then
    80. If f >= 0 And f <= 15 Then
    81. b = b * &H10
    82. SetConsoleTextAttribute hConsoleOut, f Or b
    83. Else
    84. 'MsgBox "输入的文字颜色代码错误!颜色代码详见:控制台色卡.png", vbCritical, "错误"
    85. End If
    86. Else
    87. 'MsgBox "输入的背景颜色代码错误!颜色代码详见:控制台色卡.png", vbCritical, "错误"
    88. End If
    89. End Sub
    90. 'Public Function LIn() As String
    91. ' Dim sUserInput As String * 256
    92. ' Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
    93. ' '--去除空字符和回车字符
    94. ' LIn = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
    95. 'End Function

    注意这个是 VBA 环境使用的。注意,若中途调用 shell 执行 bat 文件的话,执行结果会跑到上面创建的控制台窗口去显示。

    今天用这个显示下载 m3u8 资源文件进程时,总觉得刷屏稀里哗啦的不美观,想跟 ffmpeg 那样,执行进度只显示在一行,查了下,没找到直接说明资料,但是有个类似的:在VB控制台中更新当前行 | ,说是用  vbCr 结尾就行,用上面的代码试了下,果然可以:

    将原来的换行  vbCrLf   :

    Call COut("第 " & x & " 组:第" & i & "/" & d & "行:......下载完成,用时:" & Format((Timer - timerX) * 1000, "#0.00") & " 毫秒。        " & vbCrLf)

    换成 vbCr :

    Call COut("第 " & x & " 组:第" & i & "/" & d & "行:......下载完成,用时:" & Format((Timer - timerX) * 1000, "#0.00") & " 毫秒。        " & vbCr)

    即可,如下图:

     要正常换行时,使用 vbCrLf 结尾即可。

    此记!

  • 相关阅读:
    springBoot 源码五:springboot启动源码补充和配置优先级
    Android和JS互相调用
    CC导入UE5 人物眼球变白,睫毛粗解决办法思路提供
    MySQL基础——数据库和表的相关操作
    两个pdf文件合并为一个怎么操作?分享pdf合并操作步骤
    【前端开发基础知识&快速入门】
    【计算机组成原理 | 第二篇】计算机硬件架构的发展
    Java项目打包的可执行jar 文件部署到云服务器,并请求接口
    性能优化———事件代理
    PMP每日一练 | 考试不迷路-10.21(包含敏捷+多选)
  • 原文地址:https://blog.csdn.net/jessezappy/article/details/126251842