home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap14 / pickfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  9.7 KB  |  274 lines

  1. /*-----------------------------------------
  2.    PICKFONT.C -- Font Picker Program
  3.                  (c) Charles Petzold, 1990
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "pickfont.h"
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10. BOOL FAR PASCAL DlgProc (HWND, WORD, WORD, LONG) ;
  11.  
  12. char    szAppName [] = "PickFont" ;
  13. DWORD   dwAspectMatch = 0L ;
  14. HWND    hDlg ;
  15. LOGFONT lf ;
  16. short   nMapMode = IDD_TEXT ;
  17.  
  18. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  19.                     LPSTR lpszCmdLine, int nCmdShow)
  20.      {
  21.      HWND     hwnd ;
  22.      MSG      msg ;
  23.      WNDCLASS wndclass ;
  24.  
  25.      if (!hPrevInstance) 
  26.           {
  27.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  28.           wndclass.lpfnWndProc   = WndProc ;
  29.           wndclass.cbClsExtra    = 0 ;
  30.           wndclass.cbWndExtra    = 0 ;
  31.           wndclass.hInstance     = hInstance ;
  32.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  33.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  34.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  35.           wndclass.lpszMenuName  = NULL ;
  36.           wndclass.lpszClassName = szAppName ;
  37.  
  38.           RegisterClass (&wndclass) ;
  39.           }
  40.  
  41.      hwnd = CreateWindow (szAppName, "Font Picker",
  42.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           NULL, NULL, hInstance, NULL) ;
  46.  
  47.      ShowWindow (hwnd, nCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.  
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.           {
  52.           if (hDlg == 0 || !IsDialogMessage (hDlg, &msg))
  53.                {
  54.                TranslateMessage (&msg) ;
  55.                DispatchMessage  (&msg) ;
  56.                }
  57.           }
  58.      return msg.wParam ;
  59.      }
  60.  
  61. void MySetMapMode (HDC hdc)
  62.      {
  63.      if (nMapMode == IDD_LTWPS)
  64.           {
  65.           SetMapMode (hdc, MM_ANISOTROPIC) ;
  66.           SetWindowExt (hdc, 1440, 1440) ;
  67.           SetViewportExt (hdc, GetDeviceCaps (hdc, LOGPIXELSX),
  68.                                GetDeviceCaps (hdc, LOGPIXELSY)) ;
  69.           }
  70.      else
  71.           SetMapMode (hdc, MM_TEXT + nMapMode - IDD_TEXT) ;
  72.      }
  73.  
  74. void ShowMetrics (HWND hDlg)
  75.      {
  76.      static TEXTMETRIC tm ;
  77.      static struct 
  78.           {
  79.           short nDlgID ;
  80.           short *pData ;
  81.           }
  82.           shorts [] = 
  83.           {
  84.           TM_HEIGHT,     &tm.tmHeight,
  85.           TM_ASCENT,     &tm.tmAscent,
  86.           TM_DESCENT,    &tm.tmDescent,
  87.           TM_INTLEAD,    &tm.tmInternalLeading,
  88.           TM_EXTLEAD,    &tm.tmExternalLeading,
  89.           TM_AVEWIDTH,   &tm.tmAveCharWidth,
  90.           TM_MAXWIDTH,   &tm.tmMaxCharWidth,
  91.           TM_WEIGHT,     &tm.tmWeight,
  92.           TM_OVER,       &tm.tmOverhang,
  93.           TM_DIGX,       &tm.tmDigitizedAspectX,
  94.           TM_DIGY,       &tm.tmDigitizedAspectY
  95.           } ;
  96.      static char    *szFamily [] = { "Don't Care", "Roman",  "Swiss",
  97.                                      "Modern",     "Script", "Decorative" } ;
  98.      BOOL           bTrans ;
  99.      char           szFaceName [LF_FACESIZE] ;
  100.      HDC            hdc ;
  101.      HFONT          hFont ;
  102.      short          i ;
  103.  
  104.      lf.lfHeight    = GetDlgItemInt (hDlg, IDD_HEIGHT, &bTrans, TRUE) ;
  105.      lf.lfWidth     = GetDlgItemInt (hDlg, IDD_WIDTH,  &bTrans, FALSE) ;
  106.      lf.lfWeight    = GetDlgItemInt (hDlg, IDD_WEIGHT, &bTrans, FALSE) ;
  107.  
  108.      lf.lfItalic    = (BYTE) (IsDlgButtonChecked (hDlg, IDD_ITALIC) ? 1 : 0) ;
  109.      lf.lfUnderline = (BYTE) (IsDlgButtonChecked (hDlg, IDD_UNDER)  ? 1 : 0) ;
  110.      lf.lfStrikeOut = (BYTE) (IsDlgButtonChecked (hDlg, IDD_STRIKE) ? 1 : 0) ;
  111.  
  112.      GetDlgItemText (hDlg, IDD_FACE, lf.lfFaceName, LF_FACESIZE) ;
  113.  
  114.      dwAspectMatch = IsDlgButtonChecked (hDlg, IDD_ASPECT) ? 1L : 0L ;
  115.  
  116.      hdc = GetDC (hDlg) ;
  117.      MySetMapMode (hdc) ;
  118.      SetMapperFlags (hdc, dwAspectMatch) ;
  119.  
  120.      hFont = SelectObject (hdc, CreateFontIndirect (&lf)) ;
  121.      GetTextMetrics (hdc, &tm) ;
  122.      GetTextFace (hdc, sizeof szFaceName, szFaceName) ;
  123.  
  124.      DeleteObject (SelectObject (hdc, hFont)) ;
  125.      ReleaseDC (hDlg, hdc) ;
  126.  
  127.      for (i = 0 ; i < sizeof shorts / sizeof shorts [0] ; i++)
  128.           SetDlgItemInt (hDlg, shorts[i].nDlgID, *shorts[i].pData, TRUE) ;
  129.  
  130.      SetDlgItemText (hDlg, TM_PITCH, tm.tmPitchAndFamily & 1 ?
  131.                                                       "VARIABLE":"FIXED") ;
  132.  
  133.      SetDlgItemText (hDlg, TM_FAMILY, szFamily [tm.tmPitchAndFamily >> 4]) ;
  134.      SetDlgItemText (hDlg, TM_CHARSET, tm.tmCharSet ? "OEM" : "ANSI") ;
  135.      SetDlgItemText (hDlg, TF_NAME, szFaceName) ;
  136.      }
  137.  
  138. BOOL FAR PASCAL DlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  139.      {
  140.      switch (message)
  141.           {
  142.           case WM_INITDIALOG:
  143.                CheckRadioButton (hDlg, IDD_TEXT,   IDD_LTWPS,  IDD_TEXT) ; 
  144.                CheckRadioButton (hDlg, IDD_ANSI,   IDD_OEM,    IDD_ANSI) ;
  145.                CheckRadioButton (hDlg, IDD_QDRAFT, IDD_QPROOF, IDD_QDRAFT) ;
  146.                CheckRadioButton (hDlg, IDD_PDEF,   IDD_PVAR,   IDD_PDEF) ;
  147.                CheckRadioButton (hDlg, IDD_DONT,   IDD_DEC,    IDD_DONT) ;
  148.  
  149.                lf.lfEscapement    = 0 ;
  150.                lf.lfOrientation   = 0 ;
  151.                lf.lfOutPrecision  = OUT_DEFAULT_PRECIS ;
  152.                lf.lfClipPrecision = CLIP_DEFAULT_PRECIS ;
  153.  
  154.                ShowMetrics (hDlg) ;
  155.                                         /* fall through */
  156.           case WM_SETFOCUS:
  157.                SetFocus (GetDlgItem (hDlg, IDD_HEIGHT)) ;
  158.                return FALSE ;
  159.  
  160.           case WM_COMMAND:
  161.                switch (wParam)
  162.                     {
  163.                     case IDD_TEXT:
  164.                     case IDD_LOMET:
  165.                     case IDD_HIMET:
  166.                     case IDD_LOENG:
  167.                     case IDD_HIENG:
  168.                     case IDD_TWIPS:
  169.                     case IDD_LTWPS:
  170.                          CheckRadioButton (hDlg, IDD_TEXT, IDD_LTWPS, wParam) ;
  171.                          nMapMode = wParam ;
  172.                          break ;
  173.  
  174.                     case IDD_ASPECT:
  175.                     case IDD_ITALIC:
  176.                     case IDD_UNDER:
  177.                     case IDD_STRIKE:
  178.                          CheckDlgButton (hDlg, wParam, 
  179.                               IsDlgButtonChecked (hDlg, wParam) ? 0 : 1) ;
  180.                          break ;
  181.  
  182.                     case IDD_ANSI:
  183.                     case IDD_OEM:
  184.                          CheckRadioButton (hDlg, IDD_ANSI, IDD_OEM, wParam) ;
  185.                          lf.lfCharSet = (BYTE) (wParam == IDD_ANSI ? 0 : 255) ;
  186.                          break ;
  187.  
  188.                     case IDD_QDRAFT:
  189.                     case IDD_QDEF:
  190.                     case IDD_QPROOF:
  191.                          CheckRadioButton (hDlg, IDD_QDRAFT, IDD_QPROOF,
  192.                                                                       wParam) ;
  193.                          lf.lfQuality = (BYTE) (wParam - IDD_QDRAFT) ;
  194.                          break ;
  195.  
  196.                     case IDD_PDEF:
  197.                     case IDD_PFIXED:
  198.                     case IDD_PVAR:
  199.                          CheckRadioButton (hDlg, IDD_PDEF, IDD_PVAR, wParam) ;
  200.                          lf.lfPitchAndFamily &= 0xF0 ;
  201.                          lf.lfPitchAndFamily |= (BYTE) (wParam - IDD_PDEF) ;
  202.                          break ;
  203.  
  204.                     case IDD_DONT:
  205.                     case IDD_ROMAN:
  206.                     case IDD_SWISS:
  207.                     case IDD_MODERN:
  208.                     case IDD_SCRIPT:
  209.                     case IDD_DEC:
  210.                          CheckRadioButton (hDlg, IDD_DONT, IDD_DEC, wParam) ;
  211.                          lf.lfPitchAndFamily &= 0x0F ;
  212.                          lf.lfPitchAndFamily |= (BYTE) (wParam-IDD_DONT << 4) ;
  213.                          break ;
  214.  
  215.                     case IDD_OK:
  216.                          ShowMetrics (hDlg) ;
  217.                          InvalidateRect (GetParent (hDlg), NULL, TRUE) ;
  218.                          break ;
  219.                     }
  220.                break ;
  221.  
  222.           default:
  223.                return FALSE ;
  224.           }
  225.      return TRUE ;
  226.      }
  227.  
  228. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  229.      {
  230.      static char  szText [] =
  231.                       "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPqQqRrSsTtUuVvWwXxYyZz" ;
  232.      static short cxClient, cyClient ;
  233.      HANDLE       hInstance ;
  234.      HDC          hdc ;
  235.      HFONT        hFont ;
  236.      FARPROC      lpfnDlgProc ;
  237.      PAINTSTRUCT  ps ;
  238.      RECT         rect ;
  239.      
  240.      switch (message)
  241.           {
  242.           case WM_CREATE :
  243.                hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  244.                lpfnDlgProc = MakeProcInstance (DlgProc, hInstance) ;
  245.                hDlg = CreateDialog (hInstance, szAppName, hwnd, lpfnDlgProc) ;
  246.                return 0 ;
  247.  
  248.           case WM_SETFOCUS:
  249.                SetFocus (hDlg) ;
  250.                return 0 ;
  251.  
  252.           case WM_PAINT:
  253.                hdc = BeginPaint (hwnd, &ps) ;
  254.                MySetMapMode (hdc) ;
  255.                SetMapperFlags (hdc, dwAspectMatch) ;
  256.                GetClientRect (hDlg, &rect) ;
  257.                rect.bottom += 1 ;
  258.                DPtoLP (hdc, (LPPOINT) &rect, 2) ;
  259.  
  260.                hFont = SelectObject (hdc, CreateFontIndirect (&lf)) ;
  261.  
  262.                TextOut (hdc, rect.left, rect.bottom, szText, 52) ;
  263.  
  264.                DeleteObject (SelectObject (hdc, hFont)) ;
  265.                EndPaint (hwnd, &ps) ;
  266.                return 0 ;
  267.  
  268.           case WM_DESTROY:
  269.                PostQuitMessage (0) ;
  270.                return 0 ;
  271.           }
  272.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  273.      }
  274.