home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Special / Using CharacterRanges / GDITEST17.dpr
Encoding:
Text File  |  2003-10-15  |  6.3 KB  |  189 lines

  1. program GDITEST17;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10.  
  11. // The following example defines three ranges of character positions within a
  12. // string and sets those ranges in a StringFormat object. Next, the
  13. // TGraphics.MeasureCharacterRanges method is used to get the three regions of
  14. // the display that are occupied by the characters that are specified by the
  15. // ranges. This is done for three different layout rectangles to show how the
  16. // regions change according to the layout of the string. Also, on the third
  17. // repetition, the string format flags are changed so that the regions measured
  18. // will include trailing spaces.
  19.  
  20. Procedure OnPaint(DC: HDC);
  21. var
  22.   graphics : TGPGraphics;
  23.   blackPen: TGPPen;
  24.  
  25.   blueBrush: TGPSolidBrush;
  26.   redBrush: TGPSolidBrush;
  27.  
  28.   layoutRect_A: TGPRectF;
  29.   layoutRect_B: TGPRectF;
  30.   layoutRect_C: TGPRectF;
  31.  
  32.   charRanges: array[0..2] of TCharacterRange;
  33.   myFont: TGPFont;
  34.   strFormat: TGPStringFormat;
  35.  
  36.   // Other variables
  37.   pCharRangeRegions : array of TGPRegion; // pointer to CharacterRange regions
  38.   i: Integer;                           // loop counter
  39.   count: Integer;                       // number of character ranges set
  40.   string_: string;
  41.  
  42. begin
  43.   graphics := TGPGraphics.Create(dc);
  44.  
  45.    // Brushes and pens used for drawing and painting
  46.   blueBrush:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 255));
  47.   redBrush:= TGPSolidBrush.Create(MakeColor(100, 255, 0, 0));
  48.   blackPen := TGPPen.Create(MakeColor(255, 0, 0, 0));
  49.  
  50.    // Layout rectangles used for drawing strings
  51.   layoutRect_A := MakeRect(20.0, 20.0, 130.0, 130.0);
  52.   layoutRect_B := MakeRect(160.0, 20.0, 165.0, 130.0);
  53.   layoutRect_C := MakeRect(335.0, 20.0, 165.0, 130.0);
  54.  
  55.    // Three different ranges of character positions within the string
  56.    charRanges[0] := MakeCharacterRange(3, 5);
  57.    charRanges[1] := MakeCharacterRange(15, 2);
  58.    charRanges[2] := MakeCharacterRange(30, 15);
  59.  
  60.    // Font and string format to apply to string when drawing
  61.    myFont := TGPFont.Create('Times New Roman', 16.0);
  62.    strFormat:= TGPStringFormat.Create;
  63.  
  64.    string_ := 'The quick, brown fox easily jumps over the lazy dog.';
  65.  
  66.    // Set three ranges of character positions.
  67.    strFormat.SetMeasurableCharacterRanges(3, @charRanges);
  68.  
  69.    // Get the number of ranges that have been set, and allocate memory to 
  70.    // store the regions that correspond to the ranges.
  71.    count := strFormat.GetMeasurableCharacterRangeCount;
  72.    //pCharRangeRegions = new Region[count];
  73.    SetLength(pCharRangeRegions,count);
  74.    if count > 0 then
  75.    for i := 0 to count-1 do pCharRangeRegions[i] := TGPRegion.Create;
  76.  
  77.  
  78.    // Get the regions that correspond to the ranges within the string when
  79.    // layout rectangle A is used. Then draw the string, and show the regions.
  80.    graphics.MeasureCharacterRanges(string_, -1,
  81.       myFont, layoutRect_A, strFormat, count, pCharRangeRegions);
  82.    graphics.DrawString(string_, -1,
  83.       myFont, layoutRect_A, strFormat, blueBrush);
  84.    graphics.DrawRectangle(blackPen, layoutRect_A);
  85.    for  i := 0 to count - 1 do
  86.      graphics.FillRegion(redBrush, pCharRangeRegions[i]);
  87.  
  88.    // Get the regions that correspond to the ranges within the string when
  89.    // layout rectangle B is used. Then draw the string, and show the regions.
  90.    graphics.MeasureCharacterRanges(string_, -1,
  91.       myFont, layoutRect_B, strFormat, count, pCharRangeRegions);
  92.    graphics.DrawString(string_, -1,
  93.       myFont, layoutRect_B, strFormat, blueBrush);
  94.    graphics.DrawRectangle(blackPen, layoutRect_B);
  95.    for  i := 0 to count - 1 do
  96.       graphics.FillRegion(redBrush, pCharRangeRegions[i]);
  97.  
  98.    // Get the regions that correspond to the ranges within the string when
  99.    // layout rectangle C is used. Set trailing spaces to be included in the
  100.    // regions. Then draw the string, and show the regions.
  101.    strFormat.SetFormatFlags(Integer(StringFormatFlagsMeasureTrailingSpaces));
  102.    graphics.MeasureCharacterRanges(string_, -1,
  103.       myFont, layoutRect_C, strFormat, count, pCharRangeRegions);
  104.    graphics.DrawString(string_, -1,
  105.       myFont, layoutRect_C, strFormat, blueBrush);
  106.    graphics.DrawRectangle(blackPen, layoutRect_C);
  107.    for  i := 0 to count - 1 do
  108.       graphics.FillRegion(redBrush, pCharRangeRegions[i]);
  109.  
  110.   if count > 0 then
  111.     for i := 0 to count-1 do pCharRangeRegions[i].Free;
  112.   SetLength(pCharRangeRegions, 0);
  113.   Finalize(pCharRangeRegions);
  114.  
  115.   graphics.Free;
  116.   blueBrush.free;
  117.   redBrush.Free;
  118.   blackPen.Free;
  119.   myFont.free;
  120. end;
  121.  
  122.  
  123.  
  124. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  125. var
  126.   Handle: HDC;
  127.   ps: PAINTSTRUCT;
  128. begin
  129.   case message of
  130.     WM_PAINT:
  131.       begin
  132.         Handle := BeginPaint(Wnd, ps);
  133.         OnPaint(Handle);
  134.         EndPaint(Wnd, ps);
  135.         result := 0;
  136.       end;
  137.  
  138.     WM_DESTROY:
  139.       begin
  140.         PostQuitMessage(0);
  141.         result := 0;
  142.       end;
  143.  
  144.    else
  145.       result := DefWindowProc(Wnd, message, wParam, lParam);
  146.    end;
  147. end;
  148.  
  149. var
  150.   hWnd     : THandle;
  151.   Msg      : TMsg;
  152.   wndClass : TWndClass;
  153. begin
  154.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  155.    wndClass.lpfnWndProc    := @WndProc;
  156.    wndClass.cbClsExtra     := 0;
  157.    wndClass.cbWndExtra     := 0;
  158.    wndClass.hInstance      := hInstance;
  159.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  160.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  161.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  162.    wndClass.lpszMenuName   := nil;
  163.    wndClass.lpszClassName  := 'GettingStarted';
  164.  
  165.    RegisterClass(wndClass);
  166.  
  167.    hWnd := CreateWindow(
  168.       'GettingStarted',       // window class name
  169.       'Using CharacterRanges',// window caption
  170.       WS_OVERLAPPEDWINDOW,    // window style
  171.       Integer(CW_USEDEFAULT), // initial x position
  172.       Integer(CW_USEDEFAULT), // initial y position
  173.       Integer(CW_USEDEFAULT), // initial x size
  174.       Integer(CW_USEDEFAULT), // initial y size
  175.       0,                      // parent window handle
  176.       0,                      // window menu handle
  177.       hInstance,              // program instance handle
  178.       nil);                   // creation parameters
  179.  
  180.    ShowWindow(hWnd, SW_SHOW);
  181.    UpdateWindow(hWnd);
  182.  
  183.    while(GetMessage(msg, 0, 0, 0)) do
  184.    begin
  185.       TranslateMessage(msg);
  186.       DispatchMessage(msg);
  187.    end;
  188. end.
  189.