Mega Code Archive

 
Categories / Delphi / Examples
 

Playing with fonts

This tip will show you how to take your TForm.Canvas and draw some angled text on it while preserving the original font and brush values for other operations. Working with fonts is not very difficult once you understand what you are doing. There are few fields to set, but a lot of reward in the outcome. This tip will show you how to take your TForm.Canvas and draw some angled text on it while preserving the original font and brush values for other operations. To do this we will be using the TLogFont record structure with the GetObject() and CreateFontIndirect() WIN32 API calls. We will also be using the DrawText() WIN32 API call to get our text formatted properly on its output device. There are many manipulations you can perform on this routine, so have fun playing. [CODE - an image is on my webpage] procedure TForm1.FormPaint(Sender: TObject); var LogFont : TLogFont; tmpCanvas : TCanvas; tmpRect : TRect; x1,x2, y1,y2 : Integer; begin tmpCanvas := TCanvas.Create; tmpCanvas.Handle := GetWindowDc(Handle); try GetObject(Canvas.Font.Handle, SizeOf(LogFont), @LogFont); LogFont.lfEscapement := 90 * 10; LogFont.lfOrientation := 90 * 10; LogFont.lfOutPrecision := OUT_TT_ONLY_PRECIS; LogFont.lfFaceName := 'Arial'; LogFont.lfHeight := 15; LogFont.lfWeight := FW_BOLD; LogFont.lfQuality := PROOF_QUALITY; tmpCanvas.Font.Handle := CreateFontIndirect(LogFont); tmpCanvas.Font.Color := clWhite; tmpCanvas.Brush.Color := clNavy; x1 := GetSystemMetrics(SM_CXEDGE)+ GetSystemMetrics(SM_CXBORDER); x2 := 20; y1 := GetSystemMetrics(SM_CYCAPTION)+ GetSystemMetrics(SM_CYEDGE)+ GetSystemMetrics(SM_CYBORDER)+ 1; y2 := Height- GetSystemMetrics(SM_CYEDGE)- GetSystemMetrics(SM_CYBORDER); tmpRect := Rect(x1,y1,x2,y2); tmpCanvas.FillRect(tmpRect); DrawText(tmpCanvas.Handle, 'Lou''s Delphi Tip of the Day', -1, tmpRect, DT_BOTTOM or DT_SINGLELINE); finally tmpCanvas.Free; end; end;