(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTFormDivider;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, StdCtrls,AHMStdClass,Controls;

type
  TAHMDivBorder = (bdrSunken,bdrRaised,bdrEtched,frmSunken,frmRaised,frmEtched);
  TAHMFormDivider = class(TCustomLabel)
  private
    { Private declarations }
    FAboutStr : TAHMAboutSControlStr;
    AppHandle,MyOwnerhandle : Hwnd;
    MyOwner : TWincontrol;
    FAlignWidth : Boolean;
    FSideSpace : Integer;
    FBorderStyle : TAHMDivBorder;
    Procedure SetSideSpace(Value : Integer);
    Procedure SetAlignWidth(Value : Boolean);
    Procedure SetBorderStyle(Value : TAHMDivBorder);
  protected
    { Protected declarations }
    OldWndProc  : TFarProc;
    NewWndProc  : Pointer;
    procedure Loaded; override;
    procedure   HookWin;
    procedure   UnhookWin;
  public
    procedure Hooked(var AMsg: TMessage); virtual;
    destructor Destroy; override;
    Constructor Create(AOwner : TComponent); override;
    Procedure Paint; override;
  published
    property About : TAHMAboutSControlStr read FAboutStr write FAboutStr;
    Property AlignWidth : Boolean read FAlignWidth Write SetAlignWidth;
    Property BorderStyle : TAHMDivBorder read FBorderStyle write SetBorderStyle default bdrEtched;
    Property SideSpace : Integer read FSideSpace Write SetSideSpace default 20;
    Property Width;
    Property Height;
    Property Font;
    Property Caption;
  end;

implementation
uses Forms;

constructor TAHMFormDivider.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  Transparent :=True;
  AutoSize:=False;
  Alignment:=taCenter;
  FAlignWidth := True;
  FBorderStyle:=bdrEtched;
  FSideSpace:=20;
  with (AOwner as TWincontrol) do MyOwner := TWincontrol(AOwner);
  MyOwnerHandle := MyOwner.Handle;
  AppHandle := Application.Handle;
  HookWin;
end;

procedure TAHMFormDivider.HookWin;
begin
  MyOwnerHandle := MyOwner.Handle;
  OldWndProc := TFarProc(GetWindowLong(MyOwnerHandle, GWL_WNDPROC));
  NewWndProc := MakeObjectInstance(Hooked);
  SetWindowLong(MyOwnerHandle, GWL_WNDPROC, LongInt(NewWndProc));
end;

procedure TAHMFormDivider.UnhookWin;
begin
  SetWindowLong(MyOwnerHandle, GWL_WNDPROC, LongInt(OldWndProc));
  if assigned(NewWndProc) then FreeObjectInstance(NewWndProc);
  NewWndProc := nil;
end;

procedure TAHMFormDivider.Hooked(var AMsg: TMessage);
begin
  if (AMsg.Msg = WM_SIZE) then Invalidate;
  if (AMsg.Msg = WM_PAINT) then Invalidate;
  AMsg.Result:=CallWindowProc(OldWndProc,MyOwnerHandle, AMsg.Msg, AMsg.wParam, AMsg.lParam);
end;

procedure TAHMFormDivider.Loaded;
begin
  inherited loaded;
  If MyOwnerHandle <> MyOwner.Handle then
  begin
    UnhookWin;
    HookWin;
  end;
end;

destructor TAHMFormDivider.Destroy;
begin
  UnHookWin;
  inherited destroy;
end;

Procedure TAHMFormDivider.SetBorderStyle(Value : TAHMDivBorder);
begin
  FborderStyle:=Value;
  InValidate;
end;

Procedure TAHMFormDivider.SetAlignWidth(Value : Boolean);
begin
  FAlignWidth:=Value;
  Invalidate;
end;

Procedure TAHMFormDivider.SetSideSpace(Value : Integer);
begin
  FSideSpace:=Value;
  Invalidate;
end;

procedure TAHMFormDivider.Paint;
var LabelRect,ControlRect : TRect;
    ActualPos : LongInt;
    TextWidth: Longint;
    FCaption : String;
    BorderF,BorderSt : LongInt;
begin
  Inherited;
  if FBorderStyle in [frmSunken,frmEtched,frmRaised] then
    BorderF:=BF_BOTTOM or BF_TOP else BorderF:=BF_Bottom;
  FCaption:=' '+Caption+' ';
  If FAlignWidth then
  begin
    Left:=FSideSpace;
    Width:=(Owner as TWinControl).Width-9-2*FSideSpace;
  end;
  Canvas.Brush.Color:=clBtnFace;
  Canvas.Brush.Style:=bsClear;
  Canvas.FillRect(ClientRect);
  ControlRect:=ClientRect;
  Canvas.Font:=Self.Font;
  TextWidth:=Canvas.TextWidth(FCaption);
  ActualPos:=Round((ControlRect.Bottom-ControlRect.Top)/2);
  ControlRect.Top:=ActualPos-1;
  ControlRect.Bottom:=ActualPos+3;
  BorderSt:=Edge_Etched;
  Case FBorderStyle of
    bdrsunken,frmSunken : BorderSt:=Edge_Sunken;
    bdrraised,frmRaised : BorderSt:=Edge_Raised;
    bdretched,frmEtched : BorderSt:=Edge_Etched;
  end;
  Labelrect:=Rect(Round((ControlRect.Right-ControlRect.Left)/2)-
             Round(TextWidth/2),Clientrect.Top,Round((ControlRect.Right-ControlRect.Left)/2)+
             Round(TextWidth/2),Clientrect.Bottom);
  ControlRect.Right:=LabelRect.Left;
  Drawedge(Canvas.Handle,ControlRect,BorderSt,BorderF);
  ControlRect.Left:=LabelRect.Right;
  ControlRect.Right:=Width;
  Drawedge(Canvas.Handle,ControlRect,BorderSt,BorderF);
end;

end.
