Mega Code Archive

 
Categories / Delphi / Forms
 

How to Draw Custom Text on a Delphi Forms Caption Bar

Title: How to Draw Custom Text on a Delphi Form's 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;