Enable/disable ClearType without logging out

Vitaliy picture Vitaliy · Apr 15, 2011 · Viewed 30k times · Source

I need to enable/disable ClearType (or "Adjust the appearance and performance of Windows > Smooth edges of screen fonts") via cmd (or any script like VBS/JS) or from the registry without logging out or restarting Windows.

Maybe it's possible to enable ClearType only for one application.

Answer

Joulukuusi picture Joulukuusi · Dec 22, 2012

The modern way of scripting under Windows is using PowerShell. The following script requires version 2.0, which is available from Windows XP SP3:

#requires -version 2.0
param([bool]$enable)

$signature = @'
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(
    uint uiAction,
    uint uiParam,
    uint pvParam,
    uint fWinIni);
'@

$SPI_SETFONTSMOOTHING      = 0x004B
$SPI_SETFONTSMOOTHINGTYPE  = 0x200B
$SPIF_UPDATEINIFILE        = 0x1
$SPIF_SENDCHANGE           = 0x2
$FE_FONTSMOOTHINGCLEARTYPE = 0x2

$winapi = Add-Type -MemberDefinition $signature -Name WinAPI -PassThru

if ($enable)
{
    [void]$winapi::SystemParametersInfo($SPI_SETFONTSMOOTHING, 1, 0, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)
    [void]$winapi::SystemParametersInfo($SPI_SETFONTSMOOTHINGTYPE, 0, $FE_FONTSMOOTHINGCLEARTYPE, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)
}
else
{
    [void]$winapi::SystemParametersInfo($SPI_SETFONTSMOOTHING, 0, 0, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)
}

If, for some reason, you can't use PowerShell, you'll need DynamicWrapperX in order to execute WinAPI functions in WSH. You will first need to register it on the target machine, then you could use this VBScript:

Set WinAPI = CreateObject("DynamicWrapperX")
WinAPI.Register "user32.dll", "SystemParametersInfo", "i=uuuu"

Const SPI_SETFONTSMOOTHING      = &H004B
Const SPI_SETFONTSMOOTHINGTYPE  = &H200B
Const SPIF_UPDATEINIFILE        = &H1
Const SPIF_SENDCHANGE           = &H2
Const FE_FONTSMOOTHINGCLEARTYPE = &H2

If WScript.Arguments(0) Then
    WinAPI.SystemParametersInfo SPI_SETFONTSMOOTHING, 1, 0, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE
    WinAPI.SystemParametersInfo SPI_SETFONTSMOOTHINGTYPE, 0, FE_FONTSMOOTHINGCLEARTYPE, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE
Else
    WinAPI.SystemParametersInfo SPI_SETFONTSMOOTHING, 0, 0, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE
End If

Both scripts accept one parameter, 0 means disable ClearType, 1 means enable. No reboot is needed.