控制台程序改变控制台的字体颜色(适用VB.Net)

 Imports System
' need this to make API calls
Imports System.Runtime.InteropServices

Namespace PFitzsimons.ConsoleColour
    ''' <summary>
    ''' Static class for console colour manipulation.
    ''' </summary>
    Public Class ConsoleColour
        ' constants for console streams
        Const STD_INPUT_HANDLE As Integer = -10
        Const STD_OUTPUT_HANDLE As Integer = -11
        Const STD_ERROR_HANDLE As Integer = -12

        <DllImportAttribute("Kernel32.dll")> _
        Private Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
            ' input, output, or error device
        End Function

        ' handle to screen buffer
        <DllImportAttribute("Kernel32.dll")> _
        Private Shared Function SetConsoleTextAttribute(ByVal hConsoleOutput As IntPtr, ByVal wAttributes As Integer) As Boolean
            ' text and background colors
        End Function

        ' colours that can be set
        <Flags()> _
        Public Enum ForeGroundColour
            Black = 0
            Blue = 1
            Green = 2
            Cyan = 3
            Red = 4
            Magenta = 5
            Yellow = 6
            Grey = 7
            White = 8
        End Enum

        ' class can not be created, so we can set colours
        ' without a variable
        Private Sub New()
        End Sub

        Public Shared Function SetForeGroundColour() As Boolean
            ' default to a white-grey
            Return SetForeGroundColour(ForeGroundColour.Grey)
        End Function

        Public Shared Function SetForeGroundColour(ByVal foreGroundColour As ForeGroundColour) As Boolean
            ' default to a bright white-grey
            Return SetForeGroundColour(foreGroundColour, True)
        End Function

        Public Shared Function SetForeGroundColour(ByVal foreGroundColour As ForeGroundColour, ByVal brightColours As Boolean) As Boolean
            ' get the current console handle
            Dim nConsole As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
            Dim colourMap As Integer

            ' if we want bright colours OR it with white
            If brightColours Then
                colourMap = CInt(foreGroundColour) Or CInt(ForeGroundColour.White)
            Else
                colourMap = CInt(foreGroundColour)
            End If

            ' call the api and return the result
            Return SetConsoleTextAttribute(nConsole, colourMap)
        End Function
    End Class
End Namespace

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值