1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Draw Custom Text on a Form's Caption Bar

By Zarko Gajic, About.com

Draw on Caption Bar

By design, a Delphi form's "Caption" property is drawn by Windows, on the Caption Bar of a window next to the system menu.

If you want to add some custom text on the caption bar of a form, without changing the Caption property of the form you need to handle one special Windows message: WM_NCPAINT.

The WM_NCPAINT message is sent to a window when it needs to repaint its frame. An application can intercept this message and paint its own custom window frame.

Note that you also need to handle the WM_NCACTIVATE message that is sent to a window when it gets activated or deactivated. Without handling the WM_NCACTIVATE the custom caption text would disappear when our form looses the focus.

type
   TCustomCaptionForm = class(TForm)
   private
     procedure WMNCPaint(var Msg: TWMNCPaint) ; message WM_NCPAINT;
     procedure WMNCACTIVATE(var Msg: TWMNCActivate) ; message WM_NCACTIVATE;
     procedure DrawCaptionText() ;
   end;

...

implementation


procedure TCustomCaptionForm .DrawCaptionText;
const
   captionText = 'delphi.about.com';
var
   canvas: TCanvas;
begin
   canvas := TCanvas.Create;
   try
     canvas.Handle := GetWindowDC(Self.Handle) ;
     with canvas do
     begin
       Brush.Style := bsClear;
       Font.Color := clMaroon;
       TextOut(Self.Width - 110, 6, captionText) ;
     end;
   finally
     ReleaseDC(Self.Handle, canvas.Handle) ;
     canvas.Free;
   end;
end;

procedure TCustomCaptionForm.WMNCACTIVATE(var Msg: TWMNCActivate) ;
begin
   inherited;
   DrawCaptionText;
end;

procedure TCustomCaptionForm.WMNCPaint(var Msg: TWMNCPaint) ;
begin
   inherited;
   DrawCaptionText;
end;

Delphi tips navigator:
» How to Create the !DocType and ?XML Elements using TXmlDocument Delphi component
« Execute a Custom Action on the Form's Help button Click

More Delphi Programming Quick Tips

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. How to Draw Custom Text on a Delphi Form's Caption Bar

©2008 About.com, a part of The New York Times Company.

All rights reserved.