控件不支持透明背景颜色。 VB.NET

时间:2018-03-13 07:08:29

标签: vb.net visual-studio-2017 transparency

我已经编写了以下内容来加载ini文件中的背景颜色:

Form1.elect1Label.BackColor = decipherColor(ReadIni.ReadIni("save.ini", "Default", "bg"))
End Sub

Function decipherColor(ByVal undecipheredColor As String) As Color
    Dim deciphered As String = undecipheredColor.Split(New Char() {"[", "]"})(1)
    decipherColor = System.Drawing.Color.FromName(deciphered)
    Console.WriteLine(System.Drawing.Color.FromName(deciphered))
End Function

请注意ReadIni.ReadIni("save.ini', "Default", "bg") = Color [A=255 R=255 G=255 B=254]。 decipherColor函数用于将Color [RED]形式的颜色转换为控件将识别的内容,因此我可以执行control.backcolor = Color.red而不是control.backcolor = decipherColor("Color [RED]")

此函数工作正常,Form1.elect1Label.BackColor变为255,255,255,254(这是form1的transparencyKey)但它只会在Windows 7下执行此操作。在Windows 10下运行代码时,我收到

  

控件不支持透明背景颜色。

我发现这是一个解决方案:this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);,但.SetStyle不是Form1.elect1Label的可用选项。这使我认为.SetStylec++代码,而不是vb.net

任何人都可以帮我解决Windows 10下的这个错误吗?

1 个答案:

答案 0 :(得分:0)

我最终通过以下方式修复了它:

...
Try 'botch to fix issue #2
    Form1.elect1Label.BackColor = decipherColor(ReadIni.ReadIni("save.ini", "Default", "bg"))
    Form1.elect2Label.BackColor = decipherColor(ReadIni.ReadIni("save.ini", "Default", "bg"))
    Form1.BackColor = decipherColor(ReadIni.ReadIni("save.ini", "Default", "bg"))
Catch ex As Exception
    Form1.elect1Label.BackColor = fixARGB(ReadIni.ReadIni("save.ini", "Default", "bg"))
    Form1.elect2Label.BackColor = fixARGB(ReadIni.ReadIni("save.ini", "Default", "bg"))
    Form1.BackColor = fixARGB(ReadIni.ReadIni("save.ini", "Default", "bg"))
End Try

End Function

Function fixARGB(ByVal brokenARGB As String) As Color
    Dim halfwayThere As String = brokenARGB.Split(New Char() {"[", "]"})(1)
    Dim noA As String = Replace(halfwayThere, "A=", "")
    Dim noR As String = Replace(noA, "R=", "")
    Dim noG As String = Replace(noR, "G=", "")
    Dim clean As String = Replace(noG, "B=", "")
    Dim sept As String() = clean.Split(New Char() {","c})
    fixARGB = Color.FromArgb(sept(0), sept(1), sept(2), sept(3))
End Function