将CHAR_INFO.Attributes转换为ConsoleColor

时间:2019-02-20 00:27:38

标签: c# .net vb.net console

在C#或VB.NET中,我想将man man结构的 Attributes 成员转换为CHAR_INFO,以获取前景色和背景色。

这是我使用VB.NET声明的枚举:

<Flags>
Enum CharInfoAttributes As Short
    None = &H0S
    ForeColorBlue = &H1S
    ForeColorGreen = &H2S
    ForeColorRed = &H4S
    ForeColorIntensity = &H8S
    BackColorBlue = &H10S
    BackColorGreen = &H20S
    BackColorRed = &H40S
    BackColorIntensity = &H80S
    LeadingByte = &H100S
    TrailingByte = &H200S
    GridHorizontal = &H400S
    GridVerticalLeft = &H800S
    GridVerticalRight = &H1000S
    ReverseVideo = &H4000S
    Underscore = &H8000S
End Enum

要获得原色,我不确定我的解决方案是否正确(我尚未测试所有控制台颜色),但它似乎可以像我尝试过的颜色一样正常工作...

Public Shared Function GetForeColor(attributes As CharInfoAttributes) As ConsoleColor

    If (attributes = CharInfoAttributes.None) Then
        Throw New ArgumentException(
                           paramName:=NameOf(attributes),
                           message:="Value does not contain forecolor information.")
    End If

    Dim value As CharInfoAttributes = attributes And
        Not CharInfoAttributes.BackColorBlue And
        Not CharInfoAttributes.BackColorGreen And
        Not CharInfoAttributes.BackColorRed And
        Not CharInfoAttributes.BackColorIntensity And
        Not CharInfoAttributes.GridHorizontal And
        Not CharInfoAttributes.GridVerticalLeft And
        Not CharInfoAttributes.GridVerticalRight And
        Not CharInfoAttributes.LeadingByte And
        Not CharInfoAttributes.TrailingByte And
        Not CharInfoAttributes.ReverseVideo And
        Not CharInfoAttributes.Underscore

    Return CType(value, ConsoleColor)

End Function

但是我不知道如何获取背景色。

Public Shared Function GetBackColor(attributes As CharInfoAttributes) As ConsoleColor

    If (attributes = CharInfoAttributes.None) Then
        Throw New ArgumentException(
                           paramName:=NameOf(attributes),
                           message:="Value does not contain backcolor information.")
    End If

    ' ToDo...
    Return CType(value, ConsoleColor)

End Function

1 个答案:

答案 0 :(得分:0)

由于@TnTinMn的评论,我做了这个解决方案。

枚举定义:

<Flags>
Public Enum CharInfoAttributes As Short
    None = &H0S
    ForeColorBlue = &H1S
    ForeColorGreen = &H2S
    ForeColorRed = &H4S
    ForeColorIntensity = &H8S
    BackColorBlue = &H10S
    BackColorGreen = &H20S
    BackColorRed = &H40S
    BackColorIntensity = &H80S
    LeadingByte = &H100S
    TrailingByte = &H200S
    GridHorizontal = &H400S
    GridVerticalLeft = &H800S
    GridVerticalRight = &H1000S
    ReverseVideo = &H4000S
    Underscore = &H8000S
    ForeColorMask = &HFS
    BackColorMask = &HF0S
    ColorMask = &HFFS
End Enum

功能:

Public Shared Function GetCharInfoAttributesForeColor(ByVal attributes As CharInfoAttributes) As ConsoleColor
    If ((attributes And CharInfoAttributes.ForeColorMask) <> 0) Then
        attributes = CType((CInt(attributes)) And Not CharInfoAttributes.BackColorMask, CharInfoAttributes)
    End If
    Return CType(attributes, ConsoleColor)
End Function

Public Shared Function GetCharInfoAttributesBackColor(ByVal attributes As CharInfoAttributes) As ConsoleColor
    ' https://referencesource.microsoft.com/#mscorlib/system/console.cs,7a88edaade340cdb
    If ((attributes And CharInfoAttributes.BackColorMask) <> 0) Then
        ' Turn background colors into foreground colors.
        attributes = CType((CInt(attributes)) >> 4, CharInfoAttributes)
    End If
    Return CType(attributes, ConsoleColor)
End Function