home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / GDIDEMO.ZIP / FONT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  135 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. // Font demo window for GDIDemo program
  4.  
  5. #include <owl.h>
  6. #include <string.h>
  7. #include "demobase.h"
  8. #include "font.h"
  9.  
  10.  
  11. typedef struct {
  12.     HFONT Handle;   /* Handle to logical font */
  13.     short Height;   /* Height of logical font in pixels */
  14.     long  Width;    /* Width of name of the font in pixels */
  15.     char Name[ LF_FACESIZE ];  /* Name of this font */
  16. } FontInfoRec;
  17.  
  18.  
  19. /* local variables used by EnumerateFonts callback function */
  20.         int FontUsers = 0;
  21. FontInfoRec FontInfo[ MaxNumFonts ];
  22.         int NumFonts;            /* Number of system fonts available */
  23.         HDC TheDC;
  24.  
  25. /* EnumerateFont is a call back function.  It receives information
  26.   about system fonts.  It creates an example of each font by calling
  27.   CreateFont. When MaxNumFonts have been processed, 0 is returned
  28.   notifying windows to stop sending information, otherwise 1 is
  29.   returned telling windows to send more information if available */
  30.  
  31. int FAR PASCAL _export EnumerateFont( LPLOGFONT LogFont, LPTEXTMETRIC,
  32.                                       short, LPSTR)
  33. {
  34.   HFONT OldFont;
  35.  
  36.   /* Create the font described by LogFont */
  37.   FontInfo[NumFonts].Handle = CreateFontIndirect(LogFont);
  38.  
  39.   /* Save the height of the font for positioning when drawing in the window */
  40.   FontInfo[NumFonts].Height = LogFont->lfHeight;
  41.  
  42.   /* Save the name of the font for drawing in the window */
  43.   _fstrcpy(FontInfo[NumFonts].Name, (LPSTR) LogFont->lfFaceName);
  44.   OldFont = (HFONT)SelectObject(TheDC, FontInfo[NumFonts].Handle);
  45.   FontInfo[NumFonts].Width = GetTextExtent(TheDC,
  46.                                            (LPSTR) LogFont->lfFaceName,
  47.                                            _fstrlen((LPSTR) LogFont->lfFaceName));
  48.   SelectObject(TheDC, OldFont);
  49.   NumFonts++;
  50.   if (NumFonts > MaxNumFonts) {
  51.     return 0;  /* Don't send any more information */
  52.   } else {
  53.     return 1; /* Send more information if available */
  54.   }
  55. };
  56.  
  57. /* Collect all of the system fonts */
  58. void GetFontInfo()
  59. {
  60.   OLDFONTENUMPROC EnumProc;
  61.   if (FontUsers == 0) {
  62.     TheDC = GetDC(GetFocus());
  63.     NumFonts = 0;
  64.  
  65.     /* Create an instance of the call back function.  This allows
  66.       our program to refer to an exported function.  Otherwise the
  67.       Data segment will not be correct. */
  68.     EnumProc = (OLDFONTENUMPROC)MakeProcInstance((FARPROC) EnumerateFont, GetApplicationObject()->hInstance);
  69.  
  70.     /* Gather information about all fonts that are allowable in our window (DC) */
  71.     EnumFonts(TheDC, NULL, EnumProc, NULL);
  72.  
  73.     /* Free the instance of our call back function */
  74.     FreeProcInstance((FARPROC)EnumProc);
  75.     ReleaseDC(GetFocus(), TheDC);
  76.   };
  77.   FontUsers++;
  78. };
  79.  
  80. /* Release font information */
  81. void ReleaseFontInfo()
  82. {
  83.   FontUsers--;
  84.   if (FontUsers == 0) {
  85.     for( int I = 0; I < NumFonts; I++)
  86.       DeleteObject(FontInfo[I].Handle);
  87.   }
  88. };
  89.  
  90. /* Initialize object and collect font information */
  91. TFontWindow::TFontWindow( PTWindowsObject AParent, LPSTR ATitle ) :
  92.                 TBaseDemoWindow( AParent, ATitle )
  93. {
  94.   GetFontInfo();
  95.   Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  96.   FontsHeight = 0;
  97.   FontsWidth = 0;
  98.   for( int I = 0; I < NumFonts; I++) {
  99.     FontsHeight += FontInfo[I].Height;
  100.     if (FontsWidth < FontInfo[I].Width)
  101.       FontsWidth = FontInfo[I].Width;
  102.   };
  103.   Scroller = new TScroller(this, 1, 1, 0, 0);
  104. };
  105.  
  106. /* Draw each font name in it's font in the Display context.  Each
  107.   line is incremented by the height of the font */
  108.  
  109. void TFontWindow::Paint( HDC PaintDC, PAINTSTRUCT& )
  110. {
  111.   int I, Position;
  112.  
  113.   Position = 0;
  114.   for( I = 0; I < NumFonts; I++) {
  115.     SelectObject(PaintDC, FontInfo[I].Handle);
  116.     TextOut(PaintDC, 10, Position, FontInfo[I].Name, strlen(FontInfo[I].Name));
  117.     Position += FontInfo[I].Height;
  118.   }
  119. };
  120.  
  121. void TFontWindow::Destroy()
  122. {
  123.   TBaseDemoWindow::Destroy();
  124.   ReleaseFontInfo();
  125. };
  126.  
  127. void TFontWindow::WMSize( TMessage& Message )
  128. {
  129.   TBaseDemoWindow::WMSize(Message);
  130.   if (Scroller)
  131.     Scroller->SetRange( FontsWidth - Message.LP.Lo + 10,
  132.                         FontsHeight - Message.LP.Hi);
  133. };
  134.  
  135.