try
//画按钮
DrawButtonFace(Canvas, CaptionBtn, 1, bsAutoDetect, False, False, False);
//在按钮中定义一个范围用来输出按钮标题信息
R := Bounds(Width - xFrame - 5 * xSize + 2,
yFrame + 3, xSize +10, ySize - 7);
with CaptionBtn do
Canvas.TextRect(R, R.Left + 2, R.Top , '你好');
finally
ReleaseDC(Self.Handle, Canvas.Handle);
Canvas.Handle := 0;
end;
end;
//接收到Paint消息时重画按钮
procedure TForm1.WMNCPaint(var Msg : TWMNCPaint);
begin
inherited;
DrawCaptButton;
end;
//激活时重画
procedure TForm1.WMNCActivate(var Msg : TWMNCActivate);
begin
inherited;
DrawCaptButton;
end;
//接收到WM_SETTEXT消息时重画按钮
procedure TForm1.WMSetText(var Msg : TWMSetText);
begin
inherited;
DrawCaptButton;
end;
//捕获鼠标消息,检查是否按了按钮
procedure TForm1.WMNCHitTest(var Msg : TWMNCHitTest);
begin
inherited;
with Msg do
if PtInRect(CaptionBtn, Point(XPos - Left, YPos - Top)) then
Result := htCaptionBtn;
end;
//鼠标按下的相应操作,用户可添加各种代码,这里只显示一个对话框作例子
procedure TForm1.WMNCLButtonDown(var Msg : TWMNCLButtonDown);
begin
inherited;
if (Msg.HitTest = htCaptionBtn) then
ShowMessage('你好,你按的是标题栏上的按钮!');
end;
//窗体改变大小时强制重画标题栏
procedure TForm1.FormResize(Sender: TObject);
begin
//Force a redraw of caption bar if form is resized
Perform(WM_NCACTIVATE, Word(Active), 0);
end;