uses Direct2D, TypInfo;

procedure TForm1.FormCreate(Sender: TObject);
var
  f: TTextFormats;
begin
  for f := Low(TTextFormats) to High(TTextFormats) do
    CheckListBox1.Items.Add(GetEnumName(TypeInfo(TTextFormats), Ord(f)));

  CheckListBox1.Align := alRight;
  Edit1.Align := alBottom;
  Edit1.OnChange := CheckListBox1.OnClick;
end;

procedure TForm1.CheckListBox1Click(Sender: TObject);
begin
  Repaint;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  fmt: TTextFormat;
  i: Integer;
  R: TRect;
  txt: string;
begin
  for i := 0 to CheckListBox1.Count - 1 do
    if CheckListBox1.Checked[i] then Include(fmt, TTextFormats(i));

  txt := Edit1.Text;

  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  cvs.BeginDraw;

  cvs.TextOut(20, 20, txt); //普通文本

  cvs.Pen.Color := clGray;
  cvs.Brush.Color := clYellow;
  cvs.Font.Size := 24;
  cvs.Font.Color := clRed;
  cvs.Font.Style := [fsBold, fsItalic];
  R := Rect(20, 40, 256, 200);
  cvs.Rectangle(R);
  cvs.TextRect(R, txt, fmt); //矩形范围中的文本

  cvs.EndDraw;
  cvs.Free;
end;


效果图:



posted on 2011-04-01 20:57  万一  阅读(2041)  评论(0编辑  收藏  举报