home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Using Text and Fonts / Creating a Private Font Collection / GDITEST51.dpr
Encoding:
Text File  |  2003-10-15  |  6.2 KB  |  197 lines

  1. program GDITEST51;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10. Procedure OnPaint(DC: HDC);
  11. var
  12.   graphics : TGPGraphics;
  13.  
  14.   pointF: TGPPointF;
  15.   solidBrush: TGPSolidBrush;
  16.  
  17.   count: Integer;
  18.   found: Integer;
  19.   familyName: String;
  20.   familyNameAndStyle: String;
  21.   pFontFamily: array of TGPFontFamily;
  22.   privateFontCollection: TGPPrivateFontCollection;
  23.  
  24.   j: Integer;
  25.   Font: TGPFont;
  26. begin
  27.   graphics := TGPGraphics.Create(DC);
  28.  
  29.   PointF := MakePoint(10.0, 0.0);
  30.   solidBrush:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 0));
  31.  
  32.   found := 0;
  33.   privateFontCollection := TGPPrivateFontCollection.Create;
  34.  
  35.   // Add three font files to the private collection.
  36.   privateFontCollection.AddFontFile('c:\Windows\Fonts\Arial.ttf');
  37.   privateFontCollection.AddFontFile('c:\Windows\Fonts\CourBI.ttf');
  38.   privateFontCollection.AddFontFile('c:\Windows\Fonts\TimesBd.ttf');
  39.  
  40.   // How many font families are in the private collection?
  41.   count := privateFontCollection.GetFamilyCount;
  42.  
  43.   // Allocate a buffer to hold the array of FontFamily
  44.   // objects returned by GetFamilies.
  45.   SetLength(pFontFamily, count);
  46.   for j := 0 to count - 1 do
  47.     pFontFamily[j] := TGPFontFamily.Create;
  48.  
  49.   // Get the array of FontFamily objects.
  50.   privateFontCollection.GetFamilies(count, pFontFamily, found);
  51.  
  52.   // Display the name of each font family in the private collection
  53.   // along with the available styles for that font family.
  54.   for j := 0 to count - 1 do
  55.   begin
  56.     // Get the font family name.
  57.     pFontFamily[j].GetFamilyName(familyName);
  58.  
  59.     // Is the regular style available?
  60.     if (pFontFamily[j].IsStyleAvailable(FontStyleRegular)) then
  61.     begin
  62.       familyNameAndStyle := familyName + ' Regular';
  63.       Font:= TGPFont.Create(familyName, 16, FontStyleRegular, UnitPixel, privateFontCollection);
  64.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  65.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  66.       Font.Free;
  67.     end;
  68.  
  69.     // Is the bold style available?
  70.     if(pFontFamily[j].IsStyleAvailable(FontStyleBold)) then
  71.     begin
  72.       familyNameAndStyle := familyName + ' Bold';
  73.       Font := TGPFont.Create(familyName, 16, FontStyleBold, UnitPixel, privateFontCollection);
  74.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  75.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  76.       Font.Free;
  77.     end;
  78.  
  79.     // Is the italic style available?
  80.     if(pFontFamily[j].IsStyleAvailable(FontStyleItalic)) then
  81.     begin
  82.       familyNameAndStyle := familyName + ' Italic';
  83.       Font := TGPFont.Create(familyName, 16, FontStyleItalic, UnitPixel, privateFontCollection);
  84.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  85.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  86.       Font.Free;
  87.     end;
  88.  
  89.     // Is the bold italic style available?
  90.     if(pFontFamily[j].IsStyleAvailable(FontStyleBoldItalic)) then
  91.     begin
  92.       familyNameAndStyle := familyName + ' BoldItalic';
  93.       Font := TGPFont.Create(familyName, 16, FontStyleBoldItalic, UnitPixel, privateFontCollection);
  94.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  95.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  96.       Font.Free;
  97.     end;
  98.  
  99.     // Is the underline style available?
  100.     if(pFontFamily[j].IsStyleAvailable(FontStyleUnderline)) then
  101.     begin
  102.       familyNameAndStyle := familyName + ' Underline';
  103.       Font := TGPFont.Create(familyName, 16, FontStyleUnderline, UnitPixel, privateFontCollection);
  104.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  105.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  106.       Font.Free;
  107.     end;
  108.  
  109.    // Is the strikeout style available?
  110.     if(pFontFamily[j].IsStyleAvailable(FontStyleStrikeout)) then
  111.     begin
  112.       familyNameAndStyle := familyName + ' Strikeout';
  113.       Font := TGPFont.Create(familyName, 16, FontStyleStrikeout, UnitPixel, privateFontCollection);
  114.       graphics.DrawString(familyNameAndStyle, -1, Font, pointF, solidBrush);
  115.       pointF.Y := pointF.Y + Font.GetHeight(0.0);
  116.       Font.Free;
  117.     end;
  118.  
  119.     // Separate the families with white space.
  120.     pointF.Y := pointF.Y + 10.0;
  121.   end;
  122.   for j := 0 to count - 1 do
  123.     pFontFamily[j].Free;
  124.   Finalize(pFontFamily);
  125.  
  126.   solidBrush.Free;
  127.   privateFontCollection.Free;
  128.   graphics.Free;
  129. end;
  130.  
  131.  
  132. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  133. var
  134.   Handle: HDC;
  135.   ps: PAINTSTRUCT;
  136. begin
  137.   case message of
  138.     WM_PAINT:
  139.       begin
  140.         Handle := BeginPaint(Wnd, ps);
  141.         OnPaint(Handle);
  142.         EndPaint(Wnd, ps);
  143.         result := 0;
  144.       end;
  145.  
  146.     WM_DESTROY:
  147.       begin
  148.         PostQuitMessage(0);
  149.         result := 0;
  150.       end;
  151.  
  152.    else
  153.       result := DefWindowProc(Wnd, message, wParam, lParam);
  154.    end;
  155. end;
  156.  
  157. var
  158.   hWnd     : THandle;
  159.   Msg      : TMsg;
  160.   wndClass : TWndClass;
  161. begin
  162.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  163.    wndClass.lpfnWndProc    := @WndProc;
  164.    wndClass.cbClsExtra     := 0;
  165.    wndClass.cbWndExtra     := 0;
  166.    wndClass.hInstance      := hInstance;
  167.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  168.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  169.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  170.    wndClass.lpszMenuName   := nil;
  171.    wndClass.lpszClassName  := 'GettingStarted';
  172.  
  173.    RegisterClass(wndClass);
  174.  
  175.    hWnd := CreateWindow(
  176.       'GettingStarted',       // window class name
  177.       'Creating a Private Font Collection',       // window caption
  178.       WS_OVERLAPPEDWINDOW,    // window style
  179.       Integer(CW_USEDEFAULT), // initial x position
  180.       Integer(CW_USEDEFAULT), // initial y position
  181.       Integer(CW_USEDEFAULT), // initial x size
  182.       Integer(CW_USEDEFAULT), // initial y size
  183.       0,                      // parent window handle
  184.       0,                      // window menu handle
  185.       hInstance,              // program instance handle
  186.       nil);                   // creation parameters
  187.  
  188.    ShowWindow(hWnd, SW_SHOW);
  189.    UpdateWindow(hWnd);
  190.  
  191.    while(GetMessage(msg, 0, 0, 0)) do
  192.    begin
  193.       TranslateMessage(msg);
  194.       DispatchMessage(msg);
  195.    end;
  196. end.
  197.