Delphi中Form窗体的标题被设计成绘制在系统菜单的旁边,如果你想要在标题栏绘制自定义文本又不想改变Caption属性,你需要处理特定的Windows消息:WM_NCPAINT.。
WM_NCPAINT消息在需要重绘边框时发送到窗口,应用程序可以利用该消息绘制自己的窗口边框。
注意,同时你也要处理窗口激活或失去焦点的WM_NCACTIVATE消息,如果不处理,当窗口失去焦点时,自定义绘制的文本会消失。
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;