Changing Windows NT Font's Size

Environment: VC6 and NT4

How to change Windows NT Font's size

Sometimes you need to do something and you know it's possible because Windows does it all the time. However, you want to do it within the code of your program. Searching the net and MSDN often gives Nada.

That's the time to document the undocumented.

Changing from small fonts to large fonts and vice versa on Windows NT is done by SetupChangeFontSize in SysSetup.dll:

DWORD WINAPI SetupChangeFontSize( HANDLE HWindow, 
                                  LPCWSTR lpszFontSize)

the return value in a win32 Error code for example:

ERROR_SUCCESS   ( Value == 0    )  The operation completed successfully.
ERROR_CANCELLED ( Value == 1223 )  The operation was canceled by the user.

#include "windows.h"
///////////////////////////////////////////////////////////////
/// Arnon Mathias 13-Mar-2002
///////////////////////////////////////////////////////////////

DWORD  _MyExitWindows(UINT ExitFlags)
{
/// From the ExitWindowsEx Documentation in the MSDN
  
  HANDLE            hToken; 
  TOKEN_PRIVILEGES  tkp;
  DWORD             dwRetVal;
 
/// Get a token for this process. 
  if (!OpenProcessToken(GetCurrentProcess(), 
      TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  {
     ::MessageBox (NULL,"Open Process Token","Exit Windows",MB_OK);
     return (GetLastError());
  }

/// Get the LUID for the shutdown privilege. 
  LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
                       &tkp.Privileges[0].Luid); 

  tkp.PrivilegeCount = 1;  // one privilege to set
  tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

/// Get the shutdown privilege for this process. 
  	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
                          (PTOKEN_PRIVILEGES)NULL, 0);

/// Cannot test the return value of AdjustTokenPrivileges.
  if ( (dwRetVal = GetLastError()) != ERROR_SUCCESS) 
  {
     ::MessageBox (NULL,"AdjustTokenPrivileges","Exit Windows",MB_OK); 
     return dwRetVal;
  }
/// Shut down or Restart according to ExitFlags
  ::MessageBox ( NULL,
                 "Restarting Windows",
                 "System settings has changed",
                 MB_OK); 
  if (!ExitWindowsEx(ExitFlags, 0)) 
  {
     ::MessageBox (NULL,
                   "Error While trying to exit",
                   "Exit Windows",
                   MB_OK); 
     return (GetLastError());
  }

  return ERROR_SUCCESS;
}


DWORD  MyRestartWindows()
{
  return _MyExitWindows(EWX_LOGOFF|EWX_REBOOT);
}


DWORD   _MyChangeFontSize(unsigned short* lpszFontSize)
{ 
  // the font size string must be in unicode format
  HMODULE hModule;
  hModule = LoadLibrary("syssetup.dll");
  if (hModule == NULL) 
  {
     return (GetLastError());
  }

  typedef DWORD  (WINAPI* MYPROC) (HANDLE,LPWSTR);
    MYPROC ProcAdd; 

    ProcAdd = (MYPROC) GetProcAddress(hModule,
                                      "SetupChangeFontSize"); 
    if (ProcAdd == NULL) 
  {
     return (GetLastError());
  }

//////////////////////////////////////////////////////////////////
/// Changing the font size:
/// SetupChangeFontSize Gets two parameters:
///     Handle of the calling window (i used NULL)
///     font size string in unicode format:
///     L"120" for Large fonts
///     L"96"  for Small fonts
///  SetupChangeFontSize returns 0 if everything went fine
  DWORD dwRetVal;
  dwRetVal = (ProcAdd) (NULL,lpszFontSize); 
  if (dwRetVal != ERROR_SUCCESS)
  {
     return dwRetVal;
  }
////////////////////////////////////////////////////////////////

  if ( !(FreeLibrary(hModule)) )
  {
     return (GetLastError()); 
  }
  return ERROR_SUCCESS;
}


void   Change2LargeFonts()
{
  DWORD  dwRetVal = _MyChangeFontSize(L"120");
  if (dwRetVal == ERROR_SUCCESS)
  {
     MyRestartWindows();
  }
}


void   Change2SmallFonts()
{
  DWORD	dwRetVal = _MyChangeFontSize(L"96");
  if (dwRetVal == ERROR_SUCCESS)
  {
     MyRestartWindows();
  }
}


int main(int argc, char* argv[])
{
  //  Change2LargeFonts();
  Change2SmallFonts();
  return 0;
}


Comments

  • That's work for me. This is the most important!

    Posted by Legacy on 08/08/2002 12:00am

    Originally posted by: Toub

    Thank u for this code. It works very well!

    Reply
  • Nice but...

    Posted by Legacy on 03/17/2002 12:00am

    Originally posted by: Work colleague

    Its nice you publish in codeguru but what about your work?
    
    Its pressure time, remmember?

    Reply
  • Very Good!

    Posted by Legacy on 03/14/2002 12:00am

    Originally posted by: liangs

    thx.
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • Live Event Date: March 18, 2015 @ 2:00 p.m. ET / 11:00 a.m. PT Content is king, and businesses of all kinds want to leverage content to demonstrate their business benefits to customers, prospects, and partners alike. With new media options – 4K, 3D, HTML, and new devices – all becoming a part of your communications strategy, what tools are there that can speed time to value while reducing learning curve and duplication of efforts? Check out this upcoming eSeminar to learn how working with Adobe …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date