home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / fonts / gtxtface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.2 KB  |  168 lines

  1. /*
  2.  *
  3.  * GetTextFace
  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.  *  GetTextFace function is executed with the HDC of the top level
  9.  *  window and default font.  The facename is returned in a message
  10.  *  box.  If an error occurs, a message box is also returned.
  11.  *
  12.  */
  13.  
  14. #include "windows.h"
  15.  
  16. #define BUF_SIZE 80
  17.  
  18. /* Global Variables */
  19. static HANDLE hInst;         /* Handle to the Instance.            */
  20. static HWND hWnd;            /* Handle to the Window.              */
  21.  
  22. /* FORWARD REFERENCES */
  23. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  24.  
  25. /* WINMAIN */
  26. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  27. HANDLE hInstance, hPrevInstance;
  28. LPSTR lpszCmdLine;
  29. int cmdShow;
  30. {
  31.   MSG msg;
  32.  
  33.   if (!hPrevInstance)  {
  34.  
  35.      WNDCLASS rClass;
  36.  
  37.      rClass.lpszClassName = (LPSTR)"gtxtface";
  38.      rClass.hInstance      = hInstance;
  39.      rClass.lpfnWndProc   = WindowProc;
  40.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  41.      rClass.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
  42.      rClass.lpszMenuName  = (LPSTR)NULL;
  43.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  44.      rClass.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  45.      rClass.cbClsExtra      = 0;       /* Add double click capabilities. */
  46.      rClass.cbWndExtra      = 0;
  47.  
  48.      RegisterClass((LPWNDCLASS)&rClass);
  49.  
  50.      }
  51.    else
  52.       ;
  53.  
  54.    hInst = hInstance;
  55.  
  56.    hWnd = CreateWindow((LPSTR) "gtxtface",
  57.                (LPSTR) "GetTextFace",          /* Create a window.         */
  58.                WS_OVERLAPPEDWINDOW,            /* Make it overlapped.      */
  59.                CW_USEDEFAULT,                  /* Use default coordinates. */
  60.                CW_USEDEFAULT,                  /* Use default coordinates. */
  61.                CW_USEDEFAULT,                  /* Use default coordinates. */
  62.                CW_USEDEFAULT,                  /* Use default coordinates. */
  63.                (HWND)NULL,
  64.                (HMENU)NULL,
  65.                (HANDLE)hInstance,
  66.                (LPSTR)NULL
  67.              );
  68.  
  69.    ShowWindow(hWnd, cmdShow);
  70.  
  71.    UpdateWindow(hWnd);
  72.  
  73.    while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  74.        TranslateMessage(&msg);
  75.        DispatchMessage(&msg);
  76.    }
  77.  
  78.    exit(msg.wParam);
  79.  
  80. } /* WinMain */
  81.  
  82. /* WINDOWPROC */
  83.  
  84. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  85. HWND    hWnd;
  86. unsigned identifier;
  87. WORD     wParam;
  88. LONG     lParam;
  89.  
  90. {
  91.    switch (identifier) {
  92.  
  93.      case WM_PAINT: {
  94.  
  95.            PAINTSTRUCT ps;
  96.           RECT        rRect;
  97.          HDC        hDC;
  98.  
  99.          hDC=BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  100.          SetMapMode(hDC, MM_ANISOTROPIC);
  101.          SetWindowOrg(hDC, 0, 0);
  102.          SetWindowExt(hDC, 110, 110);
  103.          GetClientRect(hWnd, (LPRECT)&rRect);
  104.          SetViewportOrg(hDC, 0, 0);
  105.          SetViewportExt(hDC, rRect.right, rRect.bottom);
  106.          DrawText(hDC,
  107.                 (LPSTR)"Double Click Right Mouse Button To Conduct Test.",
  108.                 48, (LPRECT)&rRect, DT_SINGLELINE);  /* Prompt text.    */
  109.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);          
  110.  
  111.       }
  112.         break;
  113.     
  114.      case WM_RBUTTONDBLCLK:                    /* If the user double   */
  115.        {                                       /* clicks on the right  */
  116.        char szbuf[BUF_SIZE]; /* Output buffer.  | mouse button, then   */
  117.                              /*                 | establish needed     */
  118.        char szFacename[BUF_SIZE]; /* Facename Buffer.                  */
  119.        short sBytesCopied; /* Bytes copied.     | variables and call   */
  120.        HDC hDC; /* Display Context Handle. */
  121.  
  122.        hDC = GetDC(hWnd);                      /* (after getting DC)   */
  123.        sBytesCopied = GetTextFace(hDC, BUF_SIZE,
  124.                            (LPSTR)szFacename); /* the GetTextFace fn.  */
  125.  
  126.        if (sBytesCopied == 0)                  /* If error, issue a    */
  127.           {                                    /* message box.         */
  128.              MessageBox(hWnd,
  129.                         (LPSTR)"No Bytes Copied!!!",
  130.                         (LPSTR)"GetTextFace",
  131.                         MB_OK | MB_ICONEXCLAMATION);
  132.              break;
  133.           }
  134.        else
  135.           {
  136.            sprintf(szbuf,                       /* Now its OK; capture  */
  137.              "%s%s%s\0",                       /* the Face information */
  138.              "The Facename is '",               /* in a zero terminated */
  139.               szFacename, "'.");                 /* buffer.              */
  140.  
  141.           MessageBox(hWnd,                     /* Output the buffer in */
  142.                      (LPSTR)szbuf,             /* a message box format */
  143.                      (LPSTR)"GetTextFace",     /* so that the user can */
  144.                       MB_OK);                   /* have a readable and  */
  145.            };                                  /* useful format.       */
  146.        }
  147.        break;
  148.  
  149.      case WM_CLOSE: {
  150.  
  151.           DestroyWindow(hWnd);
  152.        }
  153.          break;
  154.     
  155.     case WM_DESTROY:
  156.         PostQuitMessage(0);
  157.         break;
  158.  
  159.     default:
  160.         return(DefWindowProc(hWnd, identifier, wParam, lParam));
  161.         break;
  162.  
  163.    }
  164.  
  165.    return(0L);
  166.  
  167. }
  168.