unit Capbtn;interfaceuses
 SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
 Forms, Dialogs, Buttons;type
 TTitleBtnForm = class(TForm)
   procedure FormResize(Sender: TObject);
 private
   TitleButton : TRect;
   procedure DrawTitleButton;
   {Paint-related messages}
   procedure WMSetText(var Msg : TWMSetText); message WM_SETTEXT;
   procedure WMNCPaint(var Msg : TWMNCPaint); message WM_NCPAINT;
   procedure WMNCActivate(var Msg : TWMNCActivate); message WM_NCACTIVATE;
   {Mouse down-related messages}
   procedure WMNCHitTest(var Msg : TWMNCHitTest); message WM_NCHITTEST;
   procedure WMNCLButtonDown(var Msg : TWMNCLButtonDown); message WM_NCLBUTTONDOWN;
   function GetVerInfo : DWORD;
 end;var
 TitleBtnForm: TTitleBtnForm;const
 htTitleBtn = htSizeLast + 1;implementation
{$R *.DFM}procedure TTitleBtnForm.DrawTitleButton;
var
 bmap : TBitmap; {Bitmap to be drawn - 16 X 16 : 16 Colors}
 XFrame,  {X and Y size of Sizeable area of Frame}
 YFrame,
 XTtlBit, {X and Y size of Bitmaps in caption}
 YTtlBit  : Integer;
begin
 {Get size of form frame and bitmaps in title bar}
 XFrame  := GetSystemMetrics(SM_CXFRAME);
 YFrame  := GetSystemMetrics(SM_CYFRAME);
 XTtlBit := GetSystemMetrics(SM_CXSIZE);
 YTtlBit := GetSystemMetrics(SM_CYSIZE); {$IFNDEF WIN32}
   TitleButton := Bounds(Width - (3 * XTtlBit) - ((XTtlBit div 2) - 2),
                         YFrame - 1,
                         XTtlBit + 2,
                         YTtlBit + 2); {$ELSE}     {Delphi 2.0 positioning}
   if (GetVerInfo = VER_PLATFORM_WIN32_NT) then
     TitleButton := Bounds(Width - (3 * XTtlBit) - ((XTtlBit div 2) - 2),
                           YFrame - 1,
                           XTtlBit + 2,
                           YTtlBit + 2)
   else
     TitleButton := Bounds(Width - XFrame - 4*XTtlBit + 2,
                          XFrame + 2,
                          XTtlBit + 2,
                          YTtlBit + 2);
 {$ENDIF}
 Canvas.Handle := GetWindowDC(Self.Handle); {Get Device context for drawing}
 try
   {Draw a button face on the TRect}
   DrawButtonFace(Canvas, TitleButton, 1, bsAutoDetect, False, False, False);
   bmap := TBitmap.Create;
   bmap.LoadFromFile('help.bmp');
   with TitleButton do
     {$IFNDEF WIN32}
       Canvas.Draw(Left + 2, Top + 2, bmap);
     {$ELSE}
       if (GetVerInfo = VER_PLATFORM_WIN32_NT) then
         Canvas.Draw(Left + 2, Top + 2, bmap)
       else
         Canvas.StretchDraw(TitleButton, bmap);
     {$ENDIF} finally
   ReleaseDC(Self.Handle, Canvas.Handle);
   bmap.Free;
   Canvas.Handle := 0;
 end;
end;{Paint triggering events}
procedure TTitleBtnForm.WMNCActivate(var Msg : TWMNCActivate);
begin
 Inherited;
 DrawTitleButton;
end;procedure TTitleBtnForm.FormResize(Sender: TObject);
begin
 Perform(WM_NCACTIVATE, Word(Active), 0);
end;{Painting events}
procedure TTitleBtnForm.WMNCPaint(var Msg : TWMNCPaint);
begin
 Inherited;
 DrawTitleButton;
end;procedure TTitleBtnForm.WMSetText(var Msg : TWMSetText);
begin
 Inherited;
 DrawTitleButton;
end;{Mouse-related procedures}
procedure TTitleBtnForm.WMNCHitTest(var Msg : TWMNCHitTest);
begin
 Inherited;
 {Check to see if the mouse was clicked in the area of the button}
 with Msg do
   if PtInRect(TitleButton, Point(XPos - Left, YPos - Top)) then
     Result := htTitleBtn;
end;procedure TTitleBtnForm.WMNCLButtonDown(var Msg : TWMNCLButtonDown);
begin
 inherited;
 if (Msg.HitTest = htTitleBtn) then
   ShowMessage('You pressed the new button');
end;function TTitleBtnForm.GetVerInfo : DWORD;
var
verInfo : TOSVERSIONINFO;
begin
 verInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
 if GetVersionEx(verInfo) then
   Result := verInfo.dwPlatformID;
   {Returns:
     VER_PLATFORM_WIN32s             Win32s on Windows 3.1
     VER_PLATFORM_WIN32_WINDOWS        Win32 on Windows 95
     VER_PLATFORM_WIN32_NT           Windows NT }
end;end.

解决方案 »

  1.   

    好长的代码啊~~告诉你一个简单的,去掉标题栏,然后加上image,自己画吧。
    还有最大最小化按钮,自己处理消息。
      

  2.   

    不用想了,就像上面说的那不是重画出来的!如果是,那标题栏应该是窗体类,但不是!现在很多漂亮界面都是用图像做出来的!别的不说了,提醒你个要注意的事!如果拦截了WM_NCHITTEST消息进行客户区和标题栏的消息转换,放在“标题栏”上的Image等控件的OnClick会失效,所以要进行坐标判断,或直接在Image下面多放个Panel控件!
      

  3.   

    Form的BorderStyle属性为 bsNone!procedure TMain.HitTest(var Msg: TWmNcHitTest);
    var
      pt:TPoint;
    begin
    if (WindowState<>wsNormal) then
    begin
      inherited;
      exit;
    end;
    pt:=Point(Msg.xPos,Msg.yPos);
    pt:=ScreenToClient(pt);
    if (pt.x<5) and (pt.y<5) then Msg.Result:=htTopLeft
    else if (pt.x>width-5) and (pt.y<5) then Msg.Result:=htTopRight
    else if (pt.x>width-5) and (pt.y>height-5) then Msg.Result:=htBottomRight
    else if (pt.x<5) and (pt.y>height-5) then Msg.Result:=htBottomLeft
    else if (pt.x<5) then Msg.Result:=htLeft
    else if (pt.y<5) then Msg.Result:=htTop
    else if (pt.x>width-5) then Msg.Result:=htRight
    else if (pt.y>height-5) then Msg.Result:=htBottom
    else inherited;
    if Htclient=Msg.result then Msg.result:=htcaption;
    end;procedure TMain.WndProc(var Msg:TMessage);
    begin
    inherited WndProc(Msg);
    if Msg.Msg=WM_ACTIVATE then
      begin
      case Msg.WParamLo of
        WA_ACTIVE,WA_CLICKACTIVE:
          begin
            MainTitle.Font.Color:=$000000;
          end;
        WA_INACTIVE:
          begin
            MainTitle.Font.Color:=$778899;
          end;
        end;
      end;
    end;