home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / console / codepage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  6.0 KB  |  144 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "console.h"
  16.  
  17. /* Note: demos for Get/SetConsoleCP not implemented */
  18.  
  19. /*********************************************************************
  20. * FUNCTION: showCP(HANDLE hConOut, UINT uiCP)                        *
  21. *                                                                    *
  22. * PURPOSE: dump the 256 character set in the given codepage          *
  23. *                                                                    *
  24. * INPUT: the console output handle to use and the desired code page  *
  25. *        to display the characters in                                *
  26. *********************************************************************/
  27.  
  28. void showCP(HANDLE hConOut, UINT uiCP)
  29. {
  30.   BOOL bSuccess;
  31.   DWORD dwSaveMode; /* store the current console mode to restore later */
  32.   UINT uiSaveCP; /* store the current code page to restore later */
  33.   int i;
  34.   DWORD dwWritten;
  35.   
  36.   /* save the current output mode and code page */
  37.   uiSaveCP = GetConsoleOutputCP();
  38.   bSuccess = GetConsoleMode(hConOut, &dwSaveMode);
  39.   PERR(bSuccess, "GetconsoleMode");
  40.   /* turn off processed output so that all chars will display */
  41.   bSuccess = SetConsoleMode(hConOut, dwSaveMode & ~ENABLE_PROCESSED_OUTPUT);
  42.   PERR(bSuccess, "SetConsoleMode");
  43.   bSuccess = SetConsoleOutputCP(uiCP);
  44.   PERR(bSuccess, "SetConsoleOutputCP");
  45.   for (i = 0; i < 256; i++)
  46.     WriteConsole(hConOut, &i, 1, &dwWritten, NULL);
  47.   /* now reset to original code page and output mode */
  48.   SetConsoleOutputCP(uiSaveCP);
  49.   bSuccess = SetConsoleMode(hConOut, dwSaveMode);
  50.   PERR(bSuccess, "SetConsoleMode");
  51.   return;
  52. }
  53.  
  54. /*********************************************************************
  55. * FUNCTION: demoOutputCodePage(HANDLE hConOut)                       *
  56. *                                                                    *
  57. * PURPOSE: demonstrate SetConsoleOutputCP and GetConsoleOutputCP.    *
  58. *          Dump the 256 character set for each code paged installed  *
  59. *          in the system.                                            *
  60. *                                                                    *
  61. * INPUT: the console output handle to manipulate the codepage for    *
  62. *********************************************************************/
  63.  
  64. void demoOutputCodePage(HANDLE hConOut)
  65. {
  66.   DWORD iValue = 0; /* index value for RegEnumValue */
  67.   LONG lSuccess; /* registry API return code */
  68.   HKEY hKey; /* handle to registry key containing codepage info */
  69.   char szValue[16], szData[16]; /* value and data info from registry */
  70.   DWORD dwType, cchValue, cbData;
  71.   UINT uiCP; /* the code page number that we extract from the registry */
  72.   char szTemp[128];
  73.   BOOL bSuccess;
  74.   OSVERSIONINFO osVer; /* for GetVersionEx() */
  75.  
  76.   /* check if Win95, if so, display notice and return - the console codepage */
  77.   /* APIs are not supported under Win95. */
  78.   osVer.dwOSVersionInfoSize = sizeof(osVer);
  79.   bSuccess = GetVersionEx(&osVer);
  80.   PERR(bSuccess, "GetVersionEx");
  81.   if (osVer.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  82.     {
  83.     MessageBox(NULL, 
  84.         "The console codepage APIs are not supported on Windows 95",
  85.         "Console API Test Application", MB_OK );
  86.     return;
  87.     }
  88.   setConTitle(__FILE__);
  89.   /* set the color for future text output */
  90.   bSuccess = SetConsoleTextAttribute(hConOut, FOREGROUND_YELLOW |
  91.       FOREGROUND_INTENSITY | BACKGROUND_BLUE);
  92.   PERR(bSuccess, "SetConsoleTextAttribute");
  93.   cls(hConOut); /* clear screen to new attributes */
  94.   myPuts(hConOut, "Warning: code page info will not display properly unless you are\n"
  95.          "using a TrueType font in the console. From the system menu, select\n"
  96.          "Fonts, then select a TrueType font from the list available.");
  97.   /* open the key containing all the code page info */
  98.   lSuccess = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  99.     "SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage", 0, KEY_QUERY_VALUE,
  100.     &hKey);
  101.   PERR(lSuccess == ERROR_SUCCESS, "RegOpenKeyEx");
  102.   while(TRUE)
  103.   {
  104.     cchValue = sizeof(szValue);
  105.     cbData = sizeof(szData);
  106.     /* get the next code page entry */
  107.     lSuccess = RegEnumValue(hKey, iValue++, szValue, &cchValue, NULL,
  108.       &dwType, szData, &cbData);
  109.     if (lSuccess == ERROR_NO_MORE_ITEMS)
  110.       break;
  111.     if (!szData[0]) /* if value data is NULL, it's an invalid code page; skip */
  112.       continue;
  113.     if (!(uiCP = atoi(szValue))) /* if value name is not a number, skip */
  114.       continue;
  115.     sprintf(szTemp, "\n\nCodepage: %s", szValue);
  116.     myPuts(hConOut, szTemp);
  117.     /* set code page and display all 256 chars on the console */
  118.     showCP(hConOut, uiCP);
  119.   }
  120.   myPuts(hConOut, "\n\nHit enter to continue...");
  121.   myGetchar();
  122.   return;
  123. }
  124.  
  125.  
  126. /*********************************************************************
  127. * FUNCTION: demoInputCodePage(HANDLE hConOut)                        *
  128. *                                                                    *
  129. * PURPOSE: demonstrate SetConsoleInputCP and GetConsoleInputCP.      *
  130. *          Read and display keyboard input, including special chars. *
  131. *                                                                    *
  132. * INPUT: the console output handle to manipulate the codepage for    *
  133. *********************************************************************/
  134.  
  135. void demoInputCodePage(HANDLE hConOut)
  136. {
  137.   myPuts(hConOut, "SetConsoleCP and GetConsoleCP demos");
  138.   myPuts(hConOut, "not implemented yet.");
  139.   myPuts(hConOut, "\n\nHit enter to continue...");
  140.   myGetchar();
  141.   return;
  142. }
  143.  
  144.