2004. június 27., vasárnap

Form with custom caption bar


Problem/Question/Abstract:

How to remove system caption bar (title bar) or replace it by my own?

Answer:

Below you can find the source code of a unit that performs the task (note that it is not a form). All you need is to inherit your form from TDPCCForm instead of TForm. Property CaptionControl is accessible at run time and defines the control (TGraphicControl) which acts as a caption bar. Usually you assign CaptionControl once in OnCreate event handler of you form, but nothing prevents you from further changes of CaptionControl at run time. If you leave CaptionControl unassigned, the form will have no caption functionality at all.

unit DPCCForms;

interface

uses
  Windows, Messages, Classes, Controls, Forms;

type
  TDPCCForm = class(TForm)
  private
    { Private declarations }
    FCaptionControl: TGraphicControl;
    procedure WMNCHitTest(var AMessage: TWMNCHitTest); message WM_NCHITTEST;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public declarations }
    property CaptionControl: TGraphicControl
      read FCaptionControl write FCaptionControl default nil;
  end;

  {===============================================================}
implementation

{---------------------------------------------------------------}

procedure TDPCCForm.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do
  begin
    Style := (Style or WS_POPUP) and (not WS_DLGFRAME);
  end; {with}
end {--TDPCCForm.CreateParams--};

{---------------------------------------------------------------}

procedure TDPCCForm.WMNCHitTest(var AMessage: TWMNCHitTest);
begin
  inherited;
  if ((AMessage.Result = HTCLIENT) and
    Assigned(CaptionControl) and
    PtInRect(CaptionControl.BoundsRect,
    ScreenToClient(Point(AMessage.XPos,
    AMessage.YPos)))) then
  begin
    AMessage.Result := HTCAPTION;
  end; {if}
end; {--TDPCCForm.WMNCHitTest--}

end.

Nincsenek megjegyzések:

Megjegyzés küldése