Question

Is there anyway of having a button (btnHCon) clicked event in which enables high contrast mode in vb.net (and then obviously one in which turns it off again)?

I am looking to add the functionality/accessibility in adding this to my project (similar to toggling High contrast in Control Panel)?

Any suggestions greatly appreciated!

Was it helpful?

Solution

It seems that the only way to do this is to use the system default colors for your forms / controls when in high contrast mode (since only those will be changed by high contrast mode).1 To turn on high contrast mode, it seems your only option is to use unmanaged code--particularly, SystemParametersInfo() with uiAction SPI_SETHIGHCONTRAST with a HIGHCONTRAST structure for pvParam.

I'm not very good at making calls to unmanaged code but thankfully chris128 at VBForums has done the hard work. You're on your own for setting it back though! But I think if you look at the references above you can figure out the appropriate tweaks.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top