是否可以使用批处理编程在命令提示符下输出当前的颜色代码? [英] Is it possible to output the current color code on the command prompt using batch programming?
问题描述
我正在使用批处理编程为我的一个朋友制作一个教程程序.我想知道是否可以在显示当前颜色代码的文件中写一些代码.
I am making a tutorial program for a friend of mine using batching programming. I would like to know if it is possible if there is code I can write in the file that will display the current color code.
示例是当前将颜色设置为0A,我想在行上显示:
Example being the color is currently set to 0A and I want be displayed on the line saying:
echo颜色当前设置为0A
.
我希望我的文件读取设置为的代码并显示出来,以帮助他们记住所做的更改,因为这是命令提示符/批处理中颜色代码的示例程序.
I want my file to read the code that is set to and display it to help them remember what changes they have made as this is an example program for color codes in the command prompt/batch.
谢谢您的帮助!
推荐答案
使用自己的命令轻松执行此操作.将下面的两个文本文件都复制到同一文件夹中的 GetConsoleColour.bat
和 GetConsoleColour.vb
中.双击批处理文件,它将创建 GetConsoleColour.exe
.
It is easy to make your own command to do this. Copy both below text files into GetConsoleColour.bat
and GetConsoleColour.vb
in the same folder. Double click the batch file and it will create GetConsoleColour.exe
.
PS 颜色的拼写符合我的文化.在撰写本文时,我认为在编程中通常不需要使用美国拼写.
PS Colour is spelt right for my culture. As I'm writing it I don't see any need to use American spelling which in programming you usually have to do.
请参见 https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo
GetConsoleColour.exe 以十六进制打印当前的控制台颜色,并且返回带有值的错误级别
GetConsoleColour.exe prints the current console colour in hex and returns an errorlevel with the value
要使用
C:\PathToFile\GetConsoleColour
我这里有一个程序,可以逐行设置文本颜色.这是唯一适用于所有Windows计算机的技术.
I have a program here that sets text color line by line. It is the only technique that will work on all Windows computers.
还有一个类似的程序,该控制台窗口中有多少个进程-ListConsole.exe列出当前控制台中的进程,并返回错误级别,指出有多少个进程. https://winsourcecode.blogspot.com/2019/05/listconsoleexe-list-processes-in.html
Also a similar program saying how many processes are in this console window - ListConsole.exe list the processes in the current console and returns an errorlevel saying how many . https://winsourcecode.blogspot.com/2019/05/listconsoleexe-list-processes-in.html
REM GetConsoleColour.bat
REM This file compiles GetConsoleColour.vb to GetConsoleColour.exe
REM GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetConsoleColour.exe" "%~dp0\GetConsoleColour.vb"
pause
注意:这里只有4行代码.剩下的只是程序执行这4行所需要的信息.在大型程序中,它们将被隐藏在单独的文件中.
Note: There is only 4 lines of code here. The rest is just information the program needs to do those 4 lines. In a big program they would be hidden away in a separate file.
'GetConsoleColour.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module MyApplication
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&
<StructLayout(LayoutKind.Sequential)> _
Public Structure COORD
Public x As Short
Public y As Short
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure CONSOLE_SCREEN_BUFFER_INFO
Public dwSize As COORD
Public dwCursorPosition As COORD
Public wAttributes As Integer
Public srWindow As SMALL_RECT
Public dwMaximumWindowSize As COORD
End Structure
Sub Main()
Dim hOut as IntPtr
Dim Ret as Integer
Dim CSBI as Console_Screen_Buffer_Info
hOut = GetStdHandle(STD_OUTPUT_HANDLE)
Ret = GetConsoleScreenBufferInfo(hOut, CSBI)
Console.Writeline(Hex(CSBI.wAttributes))
Environment.ExitCode = CSBI.wAttributes
End Sub
End Module
这篇关于是否可以使用批处理编程在命令提示符下输出当前的颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!