12

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.

skink
  • 5,133
  • 6
  • 37
  • 58
Vitaliy
  • 359
  • 1
  • 7
  • 16

8 Answers8

16

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.

skink
  • 5,133
  • 6
  • 37
  • 58
  • Do I copy this and just insert and press enter or do I need to save it to some powershell executable script text file and double-click? – user1306322 Dec 22 '12 at 17:01
  • @user1306322, you copy the script and save it as `.ps1` file. Then you can run it like `powershell "& 'C:\myscript.ps1' 1"` from the command line. It is possible to tweak the system registry so you can double-click the file, though. – skink Dec 22 '12 at 17:12
  • Okay, now I get `File C:\asd.ps1 cannot be loaded because the execution of scripts is disabled on this system.` error, which seems to be common. I guess I'll have to go with one of first google results to fix this. – user1306322 Dec 23 '12 at 00:08
  • 2
    @user1306322, `Set-ExecutionPolicy Unrestricted -Force` should allow running all scripts, but `Set-ExecutionPolicy RemoteSigned` is probably more secure. Check this: http://stackoverflow.com/questions/4037939/ – skink Dec 23 '12 at 00:20
  • Thanks, now it works, but the command line to disable all font smoothing is `powershell "& 'C:\myscript.ps1' 0"`. – user1306322 Dec 23 '12 at 00:26
  • @user1306322, yes, exactly. Like I wrote in the answer: `Both scripts accept one parameter, 0 means disable ClearType, 1 means enable.` – skink Dec 23 '12 at 00:32
  • I can't believe this hasn't gotten more upvotes. This rocks! I love the use of dynamically added types in PowerShell, too – Joshua Honig Sep 09 '13 at 13:58
7

Just to add more options, I have the C# version, adding GetFontSmoothing to it.

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);

    const uint SPI_GETFONTSMOOTHING = 74;
    const uint SPI_SETFONTSMOOTHING = 75;
    const uint SPI_UPDATEINI = 0x1;
    const UInt32 SPIF_UPDATEINIFILE = 0x1;

    private Boolean GetFontSmoothing()
    {
        bool iResult;
        int pv = 0;
        /* Call to systemparametersinfo to get the font smoothing value. */
        iResult = SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref pv, 0);
        if (pv > 0)
        {
            //pv > 0 means font smoothing is on.
            return true;
        }
        else
        {
            //pv == 0 means font smoothing is off.
            return false;
        }
    }

    private void DisableFontSmoothing()
    {
        bool iResult;
        int pv = 0;
        /* Call to systemparametersinfo to set the font smoothing value. */
        iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 0, ref pv, SPIF_UPDATEINIFILE);
        Console.WriteLine("Disabled: {0}", iResult);
    }

    private void EnableFontSmoothing()
    {
        bool iResult;
        int pv = 0;
        /* Call to systemparametersinfo to set the font smoothing value. */
        iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ref pv, SPIF_UPDATEINIFILE);
        Console.WriteLine("Enabled: {0}", iResult);
    }
Carol
  • 1,852
  • 26
  • 29
  • 1
    Thank you Carol. We've implemented a client application which works more than 3k machines and I will be using your solution which works completely perfect. – Mumin Ka Feb 27 '19 at 10:23
3

Python version:

# make sure you install pywin32 
# from http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/
import win32con
import win32gui

win32gui.SystemParametersInfo(win32con.SPI_SETFONTSMOOTHING, True, 0) # enable only
win32gui.SystemParametersInfo(win32con.SPI_SETFONTSMOOTHINGTYPE,
        win32con.FE_FONTSMOOTHINGCLEARTYPE,
        win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE)
ccpizza
  • 28,968
  • 18
  • 162
  • 169
2

make file with extention .reg this is registry for files

Disable_Smooth_edges_of_screen_fonts

[HKEY_CURRENT_USER\Control Panel\Desktop]
"FontSmoothing"="0"

Enable_Smooth_edges_of_screen_fonts

[HKEY_CURRENT_USER\Control Panel\Desktop]
"FontSmoothing"="2"

you can also do this vis cmd here is syntax for command

REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [/d Data] [/f]

you must logoff to have effect that you changed

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
2

I'm not sure how to do it without Rebooting...

But i found that changing the FontSmoothing keys was simply not enough...

For a full procedure on how to completely remove ClearType and FontSmoothing, check this out:

Completley Disable Font Smoothing and ClearType in Windows 7

Hellspawn
  • 21
  • 1
2

The following works for me: Control Panel > System > Advanced system settings > Advanced > (Performance) Settings > Visual Effects > Select 'Custom' and uncheck 'Smooth edges of screen fonts'

Deepak Azad
  • 7,903
  • 2
  • 34
  • 49
1

Look at the stuff described in the following link:

http://www.vbforums.com/showthread.php?t=491091

Calling the API might trigger the system-wide update so you do not have to logoff/logon to see the change.

Of course, this is not limited to vb.net.

TheBlastOne
  • 4,291
  • 3
  • 38
  • 72
1

Here's a PowerShell way to do it:

Set-ItemProperty 'HKCU:\Control Panel\Desktop\' -Name FontSmoothing -Value "2"

You'll need to log off and back on for it to take effect.

NOTE: strangely the setting doesn't show up as enabled in Performance Options, even though it's clearly turned on:

enter image description here

KERR
  • 1,312
  • 18
  • 13