home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / text / stxtchx.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  6KB  |  182 lines

  1. /*
  2.  * SetTextCharacterExtra
  3.  * gtxtchx.c
  4.  *
  5.  * This program registers a window and creates it on the screen.  The
  6.  *  program then creates the window, shows the window, and then updates
  7.  *  the window.  If the user couble clicks the right mouse button, the
  8.  *  SetTextCharacterExtra function is executed.  The intercharacter
  9.  *  spacing is returned and displayed in a message box.
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. /* Global Variables */
  16. static HANDLE hInst;
  17. static HWND hWnd;
  18.  
  19. /* FORWARD REFERENCES */
  20. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  21.  
  22. /* WINMAIN */
  23. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  24. HANDLE hInstance, hPrevInstance;
  25. LPSTR lpszCmdLine;
  26. int cmdShow;
  27. {
  28.   MSG msg;
  29.  
  30.   if (!hPrevInstance)  {
  31.  
  32.      WNDCLASS rClass;
  33.  
  34.      rClass.lpszClassName = (LPSTR)"stxtchx";
  35.      rClass.hInstance      = hInstance;
  36.      rClass.lpfnWndProc   = WindowProc;
  37.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  38.      rClass.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
  39.      rClass.lpszMenuName  = (LPSTR)NULL;
  40.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  41.      rClass.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  42.      rClass.cbClsExtra      = 0;       /* Add double click capabilities. */
  43.      rClass.cbWndExtra      = 0;
  44.  
  45.      RegisterClass((LPWNDCLASS)&rClass);
  46.  
  47.      }
  48.    else
  49.       ;
  50.  
  51.    hInst = hInstance;
  52.  
  53.    hWnd = CreateWindow((LPSTR) "stxtchx",
  54.                (LPSTR) "SetTextCharacterExtra", /* Create a window.         */
  55.                WS_OVERLAPPEDWINDOW,             /* Make it overlapped.      */
  56.                CW_USEDEFAULT,                   /* Use default coordinates. */
  57.                CW_USEDEFAULT,                   /* Use default coordinates. */
  58.                CW_USEDEFAULT,                   /* Use default coordinates. */
  59.                CW_USEDEFAULT,                   /* Use default coordinates. */
  60.                (HWND)NULL,
  61.                (HMENU)NULL,
  62.                (HANDLE)hInstance,
  63.                (LPSTR)NULL
  64.              );
  65.  
  66.    ShowWindow(hWnd, cmdShow);
  67.  
  68.    UpdateWindow(hWnd);
  69.  
  70.    while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  71.        TranslateMessage(&msg);
  72.        DispatchMessage(&msg);
  73.    }
  74.  
  75.    exit(msg.wParam);
  76.  
  77. } /* WinMain */
  78.  
  79. /* WINDOWPROC */
  80.  
  81. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  82. HWND    hWnd;
  83. unsigned identifier;
  84. WORD     wParam;
  85. LONG     lParam;
  86.  
  87. {
  88.    
  89.    switch (identifier) {
  90.  
  91.      case WM_PAINT: {
  92.  
  93.            PAINTSTRUCT ps;
  94.           RECT        rRect;
  95.          HDC        hDC;
  96.  
  97.          hDC=BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  98.          SetMapMode(hDC, MM_ANISOTROPIC);
  99.          SetWindowOrg(hDC, 0, 0);
  100.          SetWindowExt(hDC, 110, 110);
  101.          GetClientRect(hWnd, (LPRECT)&rRect);
  102.          SetViewportOrg(hDC, 0, 0);
  103.          SetViewportExt(hDC, rRect.right, rRect.bottom);
  104.          DrawText(hDC,
  105.                 (LPSTR)"Double Click Right Mouse Button To Set Intercharacter Spacing.",
  106.                 62, (LPRECT)&rRect, DT_SINGLELINE);  /* Place text to    */
  107.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);          /* prompt for test. */
  108.  
  109.       }
  110.         break;
  111.     
  112.      case WM_RBUTTONDBLCLK:                    /* If the user double    */
  113.        {                                       /* clicks on the right   */
  114.        char szbuf[80];   /* Output buffer       | mouse button, then    */
  115.        short sIntSpacing; /* Interchar spacing. | variables and call    */
  116.        short sOldIntSpacing; /* Old spacing.  */
  117.        HDC hDC; /* Handle to display Context. */
  118.  
  119.        hDC = GetDC(hWnd);                      /* (after getting DC)    */
  120.        sIntSpacing = GetTextCharacterExtra(hDC); /* GetTextCharacterExtra */
  121.                                                /* (with the display     */
  122.                                                /* context of the top    */
  123.                                                /* level window.         */
  124.         sprintf(szbuf,                          /* Capture the interchar */
  125.           "%s%i%s\0",                          /* spacing information   */
  126.           "Current Intercharacter Spacing = ", /* in a zero terminated  */
  127.            (int)sIntSpacing, ".");              /* buffer.               */   
  128.         MessageBox(hWnd,                        /* Output the buffer in  */
  129.                   (LPSTR)szbuf,                /* a message box format  */
  130.                   (LPSTR)"SetTextCharacterExtra", /* so that the user   */
  131.                    MB_OK);                      /* can have a readable   */
  132.                                                /* and useful format.    */
  133.  
  134.        sOldIntSpacing = SetTextCharacterExtra(hDC, 10);
  135.                                                /* Set the new spacing   */
  136.                                                /* to 10.                */
  137.  
  138.        if (sIntSpacing != sOldIntSpacing)
  139.           {                                    /* Issue error if fails. */
  140.           MessageBox(hWnd, (LPSTR)"SetTextCharacterExtra Failed!!!",
  141.                      (LPSTR)"ERROR!!!",
  142.                      MB_OK | MB_ICONEXCLAMATION);
  143.           break;
  144.           };
  145.                                                /* Output confirmation.  */
  146.        MessageBox(hWnd, (LPSTR)"Set the Intercharacter Spacing to 10.",
  147.                   (LPSTR)"Setting = 10",
  148.                   MB_OK);
  149.                                                /* Get new spacing.      */
  150.        sIntSpacing = GetTextCharacterExtra(hDC);
  151.  
  152.        sprintf(szbuf,                          
  153.           "%s%i%s\0",                          
  154.           "New Intercharacter Spacing = ", 
  155.            (int)sIntSpacing, ".");               
  156.         MessageBox(hWnd,                        /* Show that Set worked. */
  157.                   (LPSTR)szbuf,                
  158.                   (LPSTR)"SetTextCharacterExtra",
  159.                    MB_OK);                      
  160.        }                                       
  161.        break;
  162.  
  163.      case WM_CLOSE: {
  164.  
  165.           DestroyWindow(hWnd);
  166.        }
  167.          break;
  168.     
  169.     case WM_DESTROY:
  170.         PostQuitMessage(0);
  171.         break;
  172.  
  173.     default:
  174.         return(DefWindowProc(hWnd, identifier, wParam, lParam));
  175.         break;
  176.  
  177.    }
  178.  
  179.    return(0L);
  180.  
  181. }
  182.