如何在VB.Net中为所有元素切换高对比度模式?

时间:2014-03-27 11:51:47

标签: vb.net high-contrast

是否有一个按钮(btnHCon)点击事件,在vb.net中启用高对比度模式(然后显然会再次将其关闭)?

我希望添加功能/辅助功能,将其添加到我的项目中(类似于在控制面板中切换高对比度)?

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:1)

似乎唯一的方法是在高对比度模式下使用表单/控件的系统默认颜色(因为只有那些会被高对比度模式更改)。1要打开高对比度模式,似乎您唯一的选择是使用非托管代码 - 尤其是SystemParametersInfo()uiAction SPI_SETHIGHCONTRASTHIGHCONTRAST structure pvParam

我不太擅长调用非托管代码,但谢天谢地chris128 at VBForums has done the hard work。你可以自己设置它!但我想如果你看看上面的参考文献,你可以找出适当的调整。

Imports System.Runtime.InteropServices

Public Class Form1
'
'API declarations
'

Public Const HCF_HIGHCONTRASTON As Integer = 1
Public Const SPI_SETHIGHCONTRAST As Integer = 67
Public Const SPIF_SENDWININICHANGE As Integer = 2

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure HIGHCONTRAST
    Public cbSize As UInteger
    Public dwFlags As UInteger
    <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
    Public lpszDefaultScheme As String
End Structure

<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SystemParametersInfoW")> _
Public Shared Function SystemParametersInfoW(ByVal uiAction As UInteger, ByVal uiParam As UInteger, ByVal pvParam As System.IntPtr, ByVal fWinIni As UInteger) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
End Function

'
'End of API declarations
'

'Some button click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim highcontraststruct As New HIGHCONTRAST
    highcontraststruct.dwFlags = HCF_HIGHCONTRASTON
    highcontraststruct.cbSize = CUInt(Marshal.SizeOf(highcontraststruct))
    Dim highcontrastptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(highcontraststruct))
    Runtime.InteropServices.Marshal.StructureToPtr(highcontraststruct, highcontrastptr, False)

    SystemParametersInfoW(SPI_SETHIGHCONTRAST, CUInt(Marshal.SizeOf(highcontraststruct)), highcontrastptr, SPIF_SENDWININICHANGE)

End Sub
End Class