views:

105

answers:

3

Using Delphi I would like to add another button to the border icon buttons; close, maximize, minimize. Any ideas on how to do this?

+4  A: 

This was easy to do prior to Windows Aero. You simply had to listen to the WM_NCPAINT and WM_NCACTIVATE messages to draw on top of the caption bar, and similarly you could use the other WM_NC* messages to respond to mouse clicks etc, in particular WM_NCHITTEST, WM_NCLBUTTONDOWN, and WM_NCLBUTTONUP.

For instance, to draw a string on the title bar, you only had to do

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  protected
    procedure WMNCPaint(var Msg: TWMNCPaint); message WM_NCPAINT;
    procedure WMNCActivate(var Msg: TWMNCActivate); message WM_NCACTIVATE;
  private
    procedure DrawOnCaption;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.WMNCActivate(var Msg: TWMNCActivate);
begin
  inherited;
  DrawOnCaption;
end;

procedure TForm1.WMNCPaint(var Msg: TWMNCPaint);
begin
  inherited;
  DrawOnCaption;
end;

procedure TForm1.DrawOnCaption;
var
  dc: HDC;
begin
  dc := GetWindowDC(Handle);
  try
    TextOut(dc, 20, 2, 'test', 4);
  finally
    ReleaseDC(Handle, dc);
  end;
end;

end.

Now, this doesn't work with Aero enabled. Still, there is a way to draw on the caption bar; I have done that, but it is far more complicated. See this article for a working example.

Andreas Rejbrand
+1  A: 

Yes, set the form's border style property to bsNone and implement your own title bar with all the buttons and custom behaviour you like.

Sam
+1  A: 

Chris Rolliston wrote a detailed blog about creating a custom title bar on Vista and Windows 7.

He also wrote a follow up article and posted example code on CodeCentral.

stukelly