2008. április 15., kedd

Display text diagonally


Problem/Question/Abstract:

Display text diagonally

Answer:

To display text diagonally (or by any other degree), you need to create a font. The font may be created using the function CreateFontIndirect. The parameter's record member .lfOrientation specifies the angle in 0.1 degrees, e.g. 450 equals 45 degrees.

When the font handle is no longer needed, it should be deleted with DeleteObject().

The following function writes a sort of watermark on a DC and uses the API function TextOut for this:


procedure Draw_Watermark_on_DC(const aDC: hDC; const x, y: integer);
var
  plf: TLOGFONT;
  hfnt, hfntPrev: HFONT;
const
  txt1: PChar = 'Created with the demo version of'#0;
  txt2: PChar = '      pasDOC'#0;
  WaterMarkWidth = 300;
  WaterMarkHeight = 300;
begin
  // Specify a font typeface name and weight.
  ZeroMemory(@plf, sizeof(plf));
  lstrcpy(plf.lfFaceName, 'Arial');
  plf.lfHeight := 30;
  plf.lfEscapement := 0;
  plf.lfOrientation := 450;
  plf.lfWeight := FW_NORMAL;
  plf.lfCharset := ANSI_CHARSET;
  plf.lfOutPrecision := OUT_TT_PRECIS;
  plf.lfQuality := PROOF_QUALITY;
  hfnt := CreateFontIndirect(plf);

  // Draw the rotated string
  SetBkMode(aDC, TRANSPARENT);
  hfntPrev := SelectObject(aDC, hfnt);
  Windows.TextOut(aDC, x, y + WaterMarkHeight - 25, txt1, strlen(txt1));
  Windows.TextOut(aDC, x + plf.lfHeight * 3, y + WaterMarkHeight - 25, txt2,
    strlen(txt2));
  SelectObject(aDC, hfntPrev);
  DeleteObject(hfnt);
end;

Nincsenek megjegyzések:

Megjegyzés küldése