FlashCanvas demo

Submitted by eLenka on 4 August, 2010 - 13:42
Flash Canvas property

The example of using the FlashCanvas property. The program window contains 2 controls: PaintBox and ShockWaveFlash. As in an usual PaintBox so FlashCanvas it is used the same procedure for drawing. You can compare images drawn by GDI functions and flash ones, and they both are correct.

Source archive: canvas.zip (449 KB)

 
Some sample source: 
procedure TTestForm.DemoDraw(C: TCanvas);
var
  I, il: Integer;
  AP: array [0..12] of TPoint;
  WP: array [0..2] of TPoint;
  R: TRect;
  RGN1, RGN2: THandle;
  VGr: array [0..1] of TTriVertex;
  GrR: TGradientRect;
  LF: TLogFont;
  FillBMP: TBitMap;
 
begin
 C.Pen.Style := psSolid;
 C.PenPos := Point (0,0);
 C.Brush.Style:=bsSolid;
 
 for il := 0 to CBDraw.Items.Count - 1 do
  if CBDraw.Checked[il] then
   case Longint(CBDraw.Items.Objects[il]) of
    0:begin   // LineTo
       C.Pen.Style := psSolid;
       C.Pen.Color := clBlue;
       C.MoveTo(10, 10);   // EMR_MOVETOEX
       C.LineTo(150, 120); // EMR_LINETO
       C.MoveTo(160, 10);
 
       C.LineTo(10, 120);
       C.MoveTo(60, 127);
       C.Pen.Style:= psDashDotDot;
       C.LineTo(286, 110);
       C.LineTo(20, 250);
       C.Pixels[120, 70]:=clBlue;
     end;
 
     1: begin   // Rectangle
       C.Pen.Color := clRed;
       C.Brush.Color := clSkyBlue;
//       C.Brush.Style := bsClear; // EMR_SETBKMODE
       C.Rectangle(10, 10, 100, 100);
       C.Pen.Style:= psDash;
       C.RoundRect(20, 120, 250, 200, 20, 60); // EMR_ROUNDRECT
 
     end;
 
     2: begin   // Ellipse
       C.Pen.Color := clRed;
       C.Pen.Style := psClear;
       C.Brush.Color := clGreen;
       C.Ellipse(50, 30, 150, 80);
       C.Pen.Style := psSolid;
     end;
 
     3: begin     // PolyLine
       C.Pen.Color := clRed;
 
       for I := 0 to 12 do
            AP[I]:= Point(10 + 25*i, 150 + 50 *(i mod 2));
       C.Polyline(AP);  // EMR_POLYLINE16
     end;
 
     4: begin     // PolyBezier
       if false then
       begin
         C.MoveTo(0, 24);   // EMR_MOVETOEX
         C.LineTo(438,24); // EMR_LINETO
         C.LineTo(438,333); // EMR_LINETO
         C.LineTo(0,333); // EMR_LINETO
         C.LineTo(0,24); // EMR_LINETO
         C.MoveTo(87, 82);   // EMR_MOVETOEX
         C.LineTo(87,273); // EMR_LINETO
         C.LineTo(358,273); // EMR_LINETO
         C.LineTo(358,82); // EMR_LINETO
         C.LineTo(87, 82);
       end else
       begin
       C.Pen.Color := clFuchsia;
         for I := 0 to 12 do AP[i]:=Point(10+25*i, 120 + 50 *(i mod 2)-5*i);
         C.Pen.Color := clOlive;
         C.PolyBezierTo(Slice(AP, 12));  //EMR_POLYBEZIER16
       end;
     end;
 
     5: begin      // Pie, Chord
       C.Brush.Color := clOlive;
       C.Brush.Style := bsSolid; // EMR_SETBKMODE
 
       // default = AD_COUNTERCLOCKWISE
       C.Pen.Color := clFuchsia;
       SetArcDirection(C.Handle, AD_CLOCKWISE	);  // EMR_SETARCDIRECTION
         C.Arc(30, 220, 50, 250, 10, 190, 30, 270); // EMR_ARC
       SetArcDirection(C.Handle, AD_COUNTERCLOCKWISE);  // EMR_SETARCDIRECTION
 
       C.Pie(100, 100, 300, 300, 100, 100, 300, 200);  // EMR_PIE
       C.Brush.Color := clTeal;
       C.Chord(175, 180, 270, 300, 100, 100, 350, 100); // EMR_CHORD
 
       C.Pen.Color := clSilver;
       C.Brush.Color := clBlue;
       C.Brush.Style := bsDiagCross; // EMR_SETBKMODE
       C.Chord(105, 180, 200, 260, 20, 100, 220, 200); // EMR_CHORD
  //     C.LineTo(220, 120);
 
  //     C.Arc(100, 200, 150, 300, 124, 149, 300, 300); // EMR_ARC
       C.Brush.Style := bsSolid;
     end;
 
     6: begin   // AngleArc
       C.Pen.Color := clOlive;
       SetArcDirection(C.Handle, AD_CLOCKWISE);  // EMR_SETARCDIRECTION
       AngleArc(C.Handle, 50, 260, 30, 110, 300); // EMR_ANGLEARC
       C.LineTo(300, 320);
     end;
 
     7: begin     // TextOut
      C.Brush.Color := clGray;
       C.Brush.Style :=bsSolid; // EMR_SETBKMODE
       C.Font.Color := clRed;  // EMR_SETTEXTCOLOR
       C.Font.Name := 'Arial';
       C.Font.Size := 20;
       C.Font.Style := [{fsBold,} fsUnderline];
       SetTextAlign(C.Handle, TA_CENTER);
//       C.TextOut(150, 50, 'SAMPLE text 1');   // EMR_EXTTEXTOUTW
       C.TextRect(Rect(10, 20, 250, 70), 130, 35, 'SAMPLE text 1');
 
       C.Brush.Color := clYellow;
       C.Font.Color := clGreen;
       C.Font.Style := [];
       GetObject(C.Font.Handle, SizeOf(LF), @LF);
       LF.lfEscapement := 700;
       Lf.lfHeight:= - 44;
       LF.lfWidth := 20;                // <----- WIDTH ------
       LF.lfCharSet := 178;
       DeleteObject(SelectObject(C.Handle, CreateFontIndirect(LF)));
       SetTextAlign(C.Handle, TA_Bottom {TA_Right + TA_Bottom});
       C.TextOut(70, 330,'Angle SAMPLE');
       C.MoveTo(68, 330); C.LineTo(73, 330);
       C.MoveTo(70, 328); C.LineTo(70, 333);
 
       SetTextAlign(C.Handle, TA_LEFT);
       C.Font.Color := clPurple;  // EMR_SETTEXTCOLOR
       C.Font.Name := 'Times New Roman';
       C.Font.Size := 18;
       C.Brush.Style := bsClear; // EMR_SETBKMODE
       C.Font.Color := clSilver;
       C.TextOut(30, 10, 'CharSpacing sample');
       SetTextCharacterExtra(C.Handle, 1);  // <<--- SPASING -----
       C.Font.Color := clRed;
       C.TextOut(30, 10, 'CharSpacing sample');    // EMR_EXTTEXTOUTW
 
       C.Brush.Style :=bsClear; // EMR_SETBKMODE
       C.Font.Color := clGray;  // EMR_SETTEXTCOLOR
       R := Rect(30, 100, 270, 150);
       C.RoundRect(R.Left, R.Top, R.Right, R.Bottom, 16, 16); // EMR_ROUNDRECT
 
       // EMR_INTERSECTCLIPRECT + EMR_EXTTEXTOUTW  + EMR_EXTSELECTCLIPRGN
       GetObject(C.Font.Handle, SizeOf(LF), @LF);
       LF.lfWidth := 20;                // <----- WIDTH ------
       SetTextCharacterExtra(C.Handle, -5);  // <<--- SPASING -----
       DeleteObject(SelectObject(C.Handle, CreateFontIndirect(LF)));
       DrawText(C.Handle, 'Width sample', -1, R, DT_CENTER {+ DT_BOTTOM}+DT_VCenter + DT_SINGLELINE);
 
       C.Font.Color := $000080;
       C.Font.size := 12;
       SetTextCharacterExtra(C.Handle, 10);  // <<---
       R := Rect(70, 200, 200, 320);
       DrawText(C.Handle, 'Multiline text Multiline text...'#13'multiline!', -1, R, DT_Center + DT_WORDBREAK);
 
     end;
 
     8: with IDelphi.Picture do      // BitBlt
     begin
       C.StretchDraw(Rect(170, 180, 230, 220), Bitmap);  // EMR_STRETCHBLT
       BitBlt(C.Handle, 170, 200, 30, 30, Bitmap.Canvas.Handle, 0, 0, SRCCOPY); // EMR_BITBLT
       MaskBlt(C.Handle, 170, 230, 47, 60, Bitmap.Canvas.Handle,
                0, 0, IMask.Picture.Bitmap.Handle, 0, 0, $ccaa0000); // EMR_MASKBLT
     end;
 
     9:begin                       // Fills
        C.Pen.Style := psDashDotDot;
        C.Brush.Color := clMedGray;
        C.Brush.Style :=  bsHorizontal;
        C.Ellipse(150, 130, 250, 180);
 
//        C.Pen.Style :=  psSolid;
        C.Brush.Style :=  bsFDiagonal; // bsHorizontal;
        C.Brush.Color := clRed;
        C.FillRect(Rect(120, 20, 250, 90));
        C.Rectangle(0, 0, 100, 100);
 
        C.Brush.Color := clGreen;
        C.Brush.Style :=  bsDiagCross;
        C.Ellipse(50, 80, 150, 130);
 
        FillBMP := TBitMap.Create;
        With FillBMP do
          begin
            PixelFormat  := pf24bit;
            Width := 3;
            Height := 3;
            SetPixel(Canvas.Handle, 0, 0, $FF0000);
            SetPixel(Canvas.Handle, 0, 1, $FF0000);
            SetPixel(Canvas.Handle, 1, 1, $0000FF);
            SetPixel(Canvas.Handle, 1, 0, $0000FF);
          end;
         C.Brush.Bitmap := FillBMP;
         C.Rectangle(30, 150, 150, 200);
         C.Brush.Bitmap := nil;
         FillBMP.Free;
        C.Brush.Style :=  bsSolid;
       end;
     10: begin                    // Regions
       C.Brush.Color := clSkyBlue;
       C.Brush.Style := bsSolid;
(*
       RGN1 := CreateRectRgn(50, 20, 200, 300);
       RGN2 := CreateRectRgn(100, 50, 150, 200);
       CombineRgn(RGN1, RGN1, RGN2, RGN_AND);
*)
       RGN1 := CreateEllipticRgn(50, 40, 200, 100);
       RGN2 := CreateRectRgn(100, 20, 150, 200);
       CombineRgn(RGN1, RGN1, RGN2, RGN_XOR);
 
       FillRGN(C.Handle, RGN1, C.Brush.Handle);  // EMR_FILLRGN
//       FrameRgn(C.Handle, RGN1, C.Brush.Handle, 1, 1); // EMR_FRAMERGN
//       InvertRgn(C.Handle, RGN1);              // EMR_INVERTRGN
 
       DeleteObject(RGN2);
       DeleteObject(RGN1);
     end;
     11: begin                   // GradientFill
{
      R = color shl 8 and $FF00;
      G = color and $FF00;
      B = color shr 8 and $FF00;
}
        With VGr[0] do
        begin
         x := 10; y := 200; Red := 0; Green := $FF00; Blue := 0;
        end;
 
       With VGr[1] do
        begin
         x := 110; y := 350; Red := $FF00; Green := 0; Blue := 0;
        end;
 
       GrR.UpperLeft := 0; GrR.LowerRight := 1;
       GradientFill(C.Handle, @VGr, 2, @GrR, 1, GRADIENT_FILL_RECT_h);
 
       With VGr[0] do
        begin
         x := 150; y := 200; Red := 0; Green := $FF00; Blue := 0;
        end;
 
       With VGr[1] do
        begin
         x := 250; y := 350; Red := $0; Green := 0; Blue := $FF00;
        end;
       GradientFill(C.Handle, @VGr, 2, @GrR, 1, GRADIENT_FILL_RECT_V);
     end;
     12: begin           // Path
 
       C.Font.Size := 42;
       C.Font.Name := 'Times New Roman';
       C.Font.Style := [fsBold];
       GetObject(C.Font.Handle, SizeOf(LF), @LF);
       LF.lfEscapement := -200;
       DeleteObject(SelectObject(C.Handle, CreateFontIndirect(LF)));
 
       C.Brush.Color := clRed;
//       C.Brush.Style := bsFDiagonal;
       C.Brush.Style := bsSolid;
       C.Pen.Style:=psClear;
 
       BeginPath(C.Handle);
 
       C.Brush.Style := bsFDiagonal;
       C.Brush.Style := bsSolid;
       C.TextOut(20, 20, 'IT PATH!');
       C.RoundRect(10, 100, 100, 150, 20, 20);
       C.Ellipse(120, 100, 210, 150);
 
       for I := 0 to 10 do
       begin
         AP[i]:=Point(10+6*(5-i)*(5-i), 170+5*(i-3)*(i-3));
       end;
       AP[11]:=Point(80, 260); AP[12] := Point(150, 230); AP[12]:=AP[0];
 
       C.PolyBezier(AP);  //EMR_POLYBEZIER16
 
       EndPath(C.Handle);
 
       FillPath(C.Handle);
       C.Pen.Style:=psSolid;
 
(*
      for I := 0 to 12 do AP[i]:=Point(10+AP[0].X+Abs(AP[0].X-AP[i].X), AP[i].Y);
       BeginPath(C.Handle);
       C.PolyBezier(AP);  //EMR_POLYBEZIER16
       EndPath(C.Handle);
       C.Brush.Style:=bsSolid;
       FillPath(C.Handle);
*)
     end;
     13: begin // normal Metafile
       if C is TFlashCanvas then
         TFlashCanvas(C).Draw(0, 10, MF.Picture.Metafile)
         else
       C.Draw(0, 10, MF.Picture.Metafile);
     end;
     14: begin // Stretch Metafile
       i := Round( 400/MF.Picture.Width*MF.Picture.Height);
       if C is TFlashCanvas then
         TFlashCanvas(C).StretchDraw(Rect(0, 10, 400, i), MF.Picture.Metafile)
         else
       C.StretchDraw(Rect(0, 10, 400, i), MF.Picture.Metafile);
     end;
 
   end;
 
end;