(************************************************************************
*    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 AHMTControlManager;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  AHMSystemConst,AHMSystemClass,AHMTStoreData,ShellApi,AHMTHookClass;
type
  TAHMControlManager = class;
  TAHMManagedControl = class;
  TAHMControlProperties = (DesignPreview,pDialog,pSizeable,pBorder,pCaption,pMinimize,pMaximize,pSystemMenu,
                        pClientEdge,pDialogEdge,pStaticEdge,pToolWindow,pContextHelp,pAcceptFiles,
                        pWindowEdge);
  TAHMControlPropertySet = Set of TAHMControlProperties;
  TAHMCtrlGetConstraints = procedure(Sender:TObject;var MinWidth,MaxWidth,MinHeight,MaxHeight : LongInt) of object;
  TAHMOnControlDropFile=procedure(Sender:TObject;X,Y : Integer;Files : TStringlist;FileCount : Longint) of object;
  TAHMControlGetFile=procedure(Sender:TObject;FileAndPath : String) of object;
  TAHMControlOnSysMemLow = procedure(Sender : TObject;CompactRatio : LongInt) of Object;
  TAHMControlDisplayChange=procedure(Sender : TObject;BitPerPixel : Integer;ScreenWidth,ScreenHeight : Word) of Object;
  TAHMControlConstraints = class(TGraphicsObject)
  private
    FMaxWidth: LongInt;
    FMinWidth: LongInt;
    FMaxHeight: LongInt;
    FMinHeight: LongInt;
    procedure SetMaxHeight(const Value: LongInt);
    procedure SetMaxWidth(const Value: LongInt);
    procedure SetMinHeight(const Value: LongInt);
    procedure SetMinWidth(const Value: LongInt);
  protected
  public
  published
    property MinWidth : LongInt read FMinWidth write SetMinWidth default 0;
    property MinHeight : LongInt read FMinHeight write SetMinHeight default 0;
    property MaxWidth : LongInt read FMaxWidth write SetMaxWidth default 0;
    property MaxHeight : LongInt read FMaxHeight write SetMaxHeight default 0;
  end;
  TAHMSysControlStore  = class(TPersistent)
  private
    FVisible: Boolean;
    FSize: Boolean;
    FLocation: Boolean;
    FStoreKey: String;
    ControlManager : TAHMManagedControl;
    procedure SetLocation(const Value: Boolean);
    procedure SetSize(const Value: Boolean);
    procedure SetStoreKey(const Value: String);
    procedure SetVisible(const Value: Boolean);
  protected
    Procedure SaveToStore;
    Procedure LoadFromStore;
  public
    constructor Create(AOwner : TAHMManagedControl);
    Destructor Destroy; override;
  published
    property StoreKey : String read FStoreKey write SetStoreKey;
    property Size : Boolean read FSize write SetSize default False;
    property Location : Boolean read FLocation write SetLocation default False;
    property Visible : Boolean read FVisible write SetVisible default false;
  end;
  TAHMControlMessage = procedure(Sender : TObject; var WndMessage : TMessage) of object;
  TAHMManagedControls = class;
  TAHMManagedControl = class(TCollectionItem)
  private
    FAHMControlManager : TAHMControlManager;
    FApplicationHandle,FControlhandle : Hwnd;
    FMessage : TAHMControlMessage;
    FOldControlProperties,FControlProperties : TAHMControlPropertySet;
    FConstraints: TAHMControlConstraints;
    FOnGetConstraints: TAHMCtrlGetConstraints;
    FOnSysMemLow: TAHMControlOnSysMemLow;
    FOnDropFiles: TAHMOnControlDropFile;
    FOnEachDroppedFile: TAHMControlGetFile;
    FOnMove: TNotifyevent;
    FOnDisplayChange: TAHMControlDisplayChange;
    FControlStore: TAHMSysControlStore;
    FControl: TWinControl;
    FOnEnter: TNotifyevent;
    FOnExit: TNotifyevent;
    FOnSizing: TNotifyevent;
    FHookClass : TAHMHookClass;
    procedure SetConstraints(Sender : TObject);
    function GetControlProperties: TAHMControlPropertySet;
    procedure SetControlProperties(const Value: TAHMControlPropertySet);
    procedure SetControl(const Value: TWinControl);
    procedure ReloadHook(Sender : TObject);
    procedure SetOldControlProperties;
  protected
    function GetDisplayName: string; override;
    property Owner : TAHMControlManager read FAHMControlManager;
    property DisplayName : String read GetDisplayName;
    property ControlHandle : HWnd read FControlHandle;
    property ApplicationHandle : HWnd read FApplicationHandle;
    procedure Hooked(var AMsg : TMessage;var Handled : Boolean);
    Procedure LoadStore;
    Procedure SaveStore;
  public
    constructor Create(Collection: TCollection); override;
    procedure Assign(Source: TPersistent); override;
    Destructor Destroy; override;
    Procedure Update;
  published
    property Control : TWinControl read FControl write SetControl;
    property ControlStore : TAHMSysControlStore read FControlStore write FControlStore;
    property Constraints : TAHMControlConstraints read FConstraints write FConstraints;
    property OnDropFiles : TAHMOnControlDropFile read FOnDropFiles write FOnDropFiles;
    property OnEachDroppedFile : TAHMControlGetFile read FOnEachDroppedFile write FOnEachDroppedFile;
    property OnSysMemLow : TAHMControlOnSysMemLow read FOnSysMemLow write FOnSysMemLow;
    property OnMessage : TAHMControlMessage read FMessage write FMessage;
    property OnGetConstraints : TAHMCtrlGetConstraints read FOnGetConstraints write FOnGetConstraints;
    property OnDisplayChange : TAHMControlDisplayChange read FOnDisplayChange write FOnDisplayChange;
    property OnMove : TNotifyevent read FOnMove write FOnMove;
    property Properties : TAHMControlPropertySet read FControlProperties write SetControlProperties default [];
    property OnExit : TNotifyevent read FOnExit write FOnExit;
    property OnEnter : TNotifyevent read FOnEnter write FOnEnter;
    property OnSizing : TNotifyevent read FOnSizing write FOnSizing;
  end;
  TAHMManagedControls = class(TCollection)
  private
    FAHMControlManager : TAHMControlManager;
    function GetAHMManagedControl(Index: Integer): TAHMManagedControl;
    procedure SetAHMManagedControl(Index: Integer; Value: TAHMManagedControl);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor Create(AOwner: TAHMControlManager);
    function Add : TAHMManagedControl;
   Function Delete(Index :Integer) :Boolean;
    property Owner : TAHMControlManager read FAHMControlManager;
    property Items[Index: Integer]: TAHMManagedControl
             read GetAHMManagedControl write SetAHMManagedControl; default;
  end;
  TAHMControlManager = class(TAHMSystemsComponent)
  private
    FAHMManagedControls : TAHMManagedControls;
    FOnEnter: TNotifyevent;
    FOnExit: TNotifyevent;
    FStore: TAHMStoreData;
    procedure SetAHMManagedControls(Value: TAHMManagedControls);
    procedure SetStore(const Value: TAHMStoreData);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Loaded; override;
    destructor Destroy; override;
  published
    property Controls: TAHMManagedControls read FAHMManagedControls write SetAHMManagedControls;
    property Store : TAHMStoreData read FStore write SetStore;    
    property OnExit : TNotifyevent read FOnExit write FOnExit;
    property OnEnter : TNotifyevent read FOnEnter write FOnEnter;
  end;

implementation

procedure TAHMControlManager.Loaded;
Var I : Integer;
begin
  inherited Loaded;
  For I:=0 to Controls.Count-1 do
  begin
    Controls[I].SetControlProperties(Controls[I].FControlProperties);
    if assigned(FStore) then
    if FStore.ReadStr(Controls[I].FControlStore.FStoreKey,'')=Controls[I].FControlStore.FStoreKey then Controls[I].FControlStore.LoadFromStore;
    If (Controls[I].FControl<>nil) and (Controls[I].FControl.Handle<>Controls[I].COntrolHandle) then
    begin
      Controls[I].FHookClass.UpdateHooks;
      Controls[I].SetControlProperties(Controls[I].FControlProperties);
    end;
  end;
end;


destructor TAHMManagedControl.Destroy;
begin
  HookList.Remove(FAHMControlManager);
  FControlStore.Free;
  FConstraints.OnChange:=nil;
  FConstraints.Free;
  inherited Destroy;
end;

Procedure TAHMManagedControl.Hooked(var AMsg : TMessage;var Handled:Boolean);
Var FFileCount,I : Integer;
    FFileList : TStringList;
    FDropPoint : TPoint;
    FBuffer : array[0..255] of Char;
    FStructSize : LongInt;
begin
  try
  if assigned(FMessage) then FMessage(self,AMsg);
  case AMsg.Msg of
    WM_COMPACTING : if assigned(FOnSysMemLow) then FOnSysMemLow(self,TWMCompacting(AMsg).CompactRatio);
    WM_DROPFILES:
    with TWMDROPFILES(AMsg) do
    if not(csDesigning in FAHMControlManager.Componentstate) then
    begin
      FFileCount:=DragQueryFile(Drop, $FFFFFFFF, nil, 0);
      if FFileCount>0 then
      begin
        DragQueryPoint(Drop,FDropPoint);
        FFileList:=TStringList.Create;
        try
          for I:=0 to (FFileCount-1) do
          begin
            FStructSize:=DragQueryFile(Drop, i, nil, 0);
            if FStructSize<255 then
            begin
              DragQueryFile(Drop, I,FBuffer,FStructSize + 1);
              FFileList.Add(UPPERCASE(StrPas(FBuffer)));
            end;
          end;
          if assigned(FOnDropFiles) then
          FOnDropFiles(FControl,FDropPoint.X, FDropPoint.Y, FFileList,FFileCount);
          for I:=0 to FFileCount-1 do
            if assigned(FOnEachDroppedFile) then FOnEachDroppedFile(FControl,FFilelist[I]);
        finally
          FFileList.Free;
        end;
      end;
    end;
    WM_MOVE : if Assigned(FonMove) then FOnMove(FControl);
    WM_DISPLAYCHANGE : if Assigned(FOnDisplayChange) then FOnDisplayChange(Self,
                                                                    AMsg.wparam,
                                                                    LoWord(AMsg.lparam),
                                                                    HiWord(AMsg.lParam));


    WM_KILLFOCUS :
    begin
      if assigned(FAHMControlManager.FOnExit) then FAHMControlManager.FOnExit(FControl);
      if assigned(FOnExit) then FOnExit(FControl);
    end;
    WM_DESTROY  : SaveStore;
    WM_SETFOCUS :
    begin
      if assigned(FAHMControlManager.FOnEnter) then FAHMControlManager.FOnEnter(FControl);
      if assigned(FOnEnter) then FOnEnter(FControl);
    end;
    WM_CLOSE : SaveStore;
    WM_HSCROLL,
    WM_VSCROLL:if ControlHandle<>0 then InvalidateRect(ControlHandle, nil, False);
    WM_SIZE:
    begin
      if ControlHandle<>0 then InvalidateRect(ControlHandle, nil, True);
      if assigned(FOnSizing) then FOnSizing(FControl);
    end;
    WM_PALETTECHANGED: if ControlHandle<>0 then InvalidateRect(ControlHandle, nil, False);
    WM_GETMINMAXINFO :
    Begin
      With PMinMaxInfo(Amsg.lParam)^ do
      Begin
        if assigned(FOnGetConstraints) then
           FOnGetConstraints(Self,FConstraints.FMinWidth,FConstraints.FMaxWidth,FConstraints.FMinHeight,FConstraints.FMaxHeight);
        if (FConstraints.FMinWidth>0) Then ptMinTrackSize.X := FConstraints.FMinWidth;
        if (FConstraints.FMaxWidth>0) Then ptMaxTrackSize.X := FConstraints.FMaxWidth;
        if (FConstraints.FMinHeight>0) Then ptMinTrackSize.Y := FConstraints.FMinHeight;
        if (FConstraints.FMaxHeight>0) Then ptMaxTrackSize.Y := FConstraints.FMaxHeight;
      end;
    end;
  end;
  except
    Application.HandleException(Self);
  end;
end;

{ TAHMControlConstraints }

procedure TAHMControlConstraints.SetMaxHeight(const Value: LongInt);
begin
  FMaxHeight := Value;
  Changed;
end;

procedure TAHMControlConstraints.SetMaxWidth(const Value: LongInt);
begin
  FMaxWidth := Value;
  Changed;
end;

procedure TAHMControlConstraints.SetMinHeight(const Value: LongInt);
begin
  FMinHeight := Value;
  Changed;
end;

procedure TAHMControlConstraints.SetMinWidth(const Value: LongInt);
begin
  FMinWidth := Value;
  Changed;
end;

{ TAHMSysControlStore }

constructor TAHMSysControlStore.Create(AOwner: TAHMManagedControl);
begin
  inherited Create;
  ControlManager:=AOwner;
end;

destructor TAHMSysControlStore.Destroy;
begin
  inherited Destroy;
end;

procedure TAHMSysControlStore.LoadFromStore;
begin
  if not(csDesigning in ControlManager.FAHMControlManager.Componentstate) then
  if Assigned(ControlManager.FAHMControlManager.FStore) and Assigned(ControlManager.FControl) then
  with ControlManager.FAHMControlManager.FStore do
  begin
    if FSize then
    begin
      ControlManager.FControl.Width:=ReadInt(FStoreKey+ControlManager.FControl.Name+ahmscpropwidth,ControlManager.FControl.Width);
      ControlManager.FControl.Height:=ReadInt(FStoreKey+ControlManager.FControl.Name+ahmscpropheight,ControlManager.FControl.Height);
    end;
    if FLocation then
    begin
      ControlManager.FControl.Top:=ReadInt(FStoreKey+ControlManager.FControl.Name+ahmscproptop,ControlManager.FControl.Top);
      ControlManager.FControl.Left:=ReadInt(FStoreKey+ControlManager.FControl.Name+ahmscpropleft,ControlManager.FControl.Top);
    end;
    if FVisible then
     ControlManager.FControl.Visible:=ReadBool(FStoreKey+ControlManager.FControl.Name+ahmscpropvisible,ControlManager.FControl.Visible);
  end;
end;

procedure TAHMSysControlStore.SaveToStore;
begin
  if not(csDesigning in ControlManager.FAHMControlManager.Componentstate) then
  if Assigned(ControlManager.FAHMControlManager.FStore) and Assigned(ControlManager.FControl) then
  with ControlManager.FAHMControlManager.FStore do
  begin
    if FSize then
    begin
      WriteInt(FStoreKey+ControlManager.FControl.Name+ahmscpropwidth,ControlManager.FControl.Width);
      WriteInt(FStoreKey+ControlManager.FControl.Name+ahmscpropheight,ControlManager.FControl.Height);
    end;
    if FLocation then
    begin
      WriteInt(FStoreKey+ControlManager.FControl.Name+ahmscproptop,ControlManager.FControl.Top);
      WriteInt(FStoreKey+ControlManager.FControl.Name+ahmscpropleft,ControlManager.FControl.Left);
    end;
    if FVisible then
    WriteBool(FStoreKey+ControlManager.FControl.Name+ahmscpropvisible,ControlManager.FControl.Visible);
    WriteStr(FStorekey,FStoreKey);
  end;
end;

procedure TAHMSysControlStore.SetLocation(const Value: Boolean);
begin
  FLocation:=Value;
  if Value then SaveToStore;
end;

procedure TAHMSysControlStore.SetSize(const Value: Boolean);
begin
  FSize:=Value;
  if Value then SaveToStore;
end;

procedure TAHMSysControlStore.SetStoreKey(const Value: String);
begin
  FStoreKey := Value;
end;

procedure TAHMSysControlStore.SetVisible(const Value: Boolean);
begin
  FVisible := Value;
  if Value then SaveToStore;
end;

procedure TAHMManagedControl.LoadStore;
begin
  FControlStore.LoadFromStore;
end;


procedure TAHMManagedControl.SaveStore;
begin
  if FControlStore<>nil then FControlStore.SaveToStore;
end;

function TAHMManagedControl.GetControlProperties: TAHMControlPropertySet;
Var NStyle,NExStyle: LongInt;
    CHandle : THandle;
begin
  if FControl=nil then exit;
  CHandle:=FControl.Handle;
  result:=[];
  NStyle:=GetWindowLong(CHandle, GWL_STYLE);
  NExStyle:=GetWindowLong(CHandle, GWL_EXSTYLE);
  if (NStyle and WS_DlgFrame)<>0 then Include(Result,pDialog);
  if (NStyle and WS_ThickFrame)<>0 then Include(Result,pSizeable);
  if (NStyle and WS_Caption)<>0 then Include(Result,pCaption);
  if (NStyle and WS_Border)<>0 then Include(Result,pBorder);
  if (NStyle and WS_MINIMIZEBOX)<>0 then Include(Result,pMinimize);
  if (NStyle and WS_MAXIMIZEBOX)<>0 then Include(Result,pMaximize);
  if (NStyle and WS_SYSMENU)<>0 then Include(Result,pSystemMenu);
  if (NExStyle and WS_EX_ClientEdge)<>0 then Include(Result,pClientEdge);
  if (NExStyle and WS_EX_ContextHelp)<>0 then Include(Result,pContextHelp);
  if (NExStyle and WS_EX_DlgModalFrame)<>0 then Include(Result,pDialogEdge);
  if (NExStyle and WS_EX_StaticEdge)<>0 then Include(Result,pStaticEdge);
  if (NExStyle and WS_EX_Toolwindow)<>0 then Include(Result,pToolWindow);
  if (NExStyle and WS_EX_WindowEdge)<>0 then Include(Result,pWindowEdge);
  if (NExStyle and WS_EX_AcceptFiles)<>0 then Include(Result,pAcceptFiles);
end;


procedure TAHMManagedControl.SetControlProperties(
  const Value: TAHMControlPropertySet);
Var NStyle,NExStyle: LongInt;
    CHandle : THandle;
begin
  if FControl=nil then
  begin
    FControlProperties:=Value;
    exit;
  end;
  CHandle:=FControl.Handle;
  if (CHandle=0) then Exit;
  NStyle:=GetWindowLong(CHandle, GWL_STYLE);
  NExStyle:=GetWindowLong(CHandle, GWL_EXSTYLE);
  NExStyle:=NExStyle or WS_EX_CONTROLPARENT;
  if (pCaption in Value) then NStyle:=NStyle or WS_Caption else NStyle:=NStyle and not WS_Caption;
  if (pBorder in Value) then NStyle:=NStyle or WS_Border else NStyle:=NStyle and not WS_Border;
  if (pDialog in Value) then NStyle:=NStyle or WS_DlgFrame else NStyle:=NStyle and not WS_DlgFrame;
  if (pMinimize in Value) then NStyle:=NStyle or WS_MinimizeBox else NStyle:=NStyle and not WS_MinimizeBox;
  if (pMaximize in Value) then NStyle:=NStyle or WS_MaximizeBox else NStyle:=NStyle and not WS_MaximizeBox;
  if (pSystemMenu in Value) then NStyle:=NStyle or WS_Sysmenu else NStyle:=NStyle and not WS_Sysmenu;
  if (pSizeable in Value) then NStyle:=NStyle or WS_Thickframe else NStyle:=NStyle and not WS_ThickFrame;
  if (pClientEdge in Value) then NExStyle:=NExStyle or WS_EX_CLIENTEDGE else NExStyle:=NExStyle and not WS_EX_CLiENTEDGE;
  if (pContextHelp in Value) then NExStyle:=NExStyle or WS_EX_ContextHelp else NExStyle:=NExStyle and not WS_EX_ContextHelp;
  if (pDialogEdge in Value) then NExStyle:=NExStyle or WS_EX_DLGMODALFRAME else NExStyle:=NExStyle and not WS_EX_DLGMODALFRAME;
  if (pStaticEdge in Value) then NExStyle:=NExStyle or WS_EX_STATICEDGE else NExStyle:=NExStyle and not WS_EX_STATICEDGE;
  if (pToolWindow in Value) then NExStyle:=NExStyle or WS_EX_TOOLWINDOW else NExStyle:=NExStyle and not WS_EX_TOOLWINDOW;
  if (pWindowEdge in Value) then NExStyle:=NExStyle or WS_EX_WindowEdge else NExStyle:=NExStyle and not WS_EX_WindowEdge;
  if (pAcceptFiles in Value) then NExStyle:=NExStyle or WS_EX_AcceptFiles else NExStyle:=NExStyle and not WS_EX_AcceptFiles;
  if (csDesigning in FAHMControlManager.Componentstate) then
  if not(DesignPreview in Value) and (DesignPreview in FControlProperties) then SetOldControlProperties;
  FControlProperties:=Value;
  if not(csdesigning in FAHMControlManager.componentstate) or ((csdesigning in FAHMControlManager.componentstate) and (DesignPreview in Value)) then
  begin
    SetWindowLong(CHandle, GWL_EXSTYLE,NExStyle);
    SetWindowLong(CHandle, GWL_STYLE,NStyle);
    SetWindowPos(CHandle, 0, 0,0,0,0,SWP_DRAWFRAME or SWP_FRAMECHANGED or SWP_NOACTIVATE or
      SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
    SendMessage(CHandle,WM_PAINT,GetDC(CHandle),0);
  end;
  if (pAcceptFiles in FControlProperties) then DragAcceptFiles(FControl.Handle,True) else DragAcceptFiles(FControl.Handle,False);
end;

procedure TAHMManagedControl.SetOldControlProperties;
Var NStyle,NExStyle: LongInt;
    CHandle : THandle;
begin

  CHandle:=FControl.Handle;
  if (CHandle=0) then Exit;
  NStyle:=GetWindowLong(CHandle, GWL_STYLE);
  NExStyle:=GetWindowLong(CHandle, GWL_EXSTYLE);
  NExStyle:=NExStyle or WS_EX_CONTROLPARENT;
  if (pCaption in FOldControlProperties) then NStyle:=NStyle or WS_Caption else NStyle:=NStyle and not WS_Caption;
  if (pBorder in FOldControlProperties) then NStyle:=NStyle or WS_Border else NStyle:=NStyle and not WS_Border;
  if (pDialog in FOldControlProperties) then NStyle:=NStyle or WS_DlgFrame else NStyle:=NStyle and not WS_DlgFrame;
  if (pMinimize in FOldControlProperties) then NStyle:=NStyle or WS_MinimizeBox else NStyle:=NStyle and not WS_MinimizeBox;
  if (pMaximize in FOldControlProperties) then NStyle:=NStyle or WS_MaximizeBox else NStyle:=NStyle and not WS_MaximizeBox;
  if (pSystemMenu in FOldControlProperties) then NStyle:=NStyle or WS_Sysmenu else NStyle:=NStyle and not WS_Sysmenu;
  if (pSizeable in FOldControlProperties) then NStyle:=NStyle or WS_Thickframe else NStyle:=NStyle and not WS_Thickframe;
  if (pClientEdge in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_CLIENTEDGE else NExStyle:=NExStyle and not WS_EX_CLiENTEDGE;
  if (pContextHelp in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_ContextHelp else NExStyle:=NExStyle and not WS_EX_ContextHelp;
  if (pDialogEdge in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_DLGMODALFRAME else NExStyle:=NExStyle and not WS_EX_DLGMODALFRAME;
  if (pStaticEdge in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_STATICEDGE else NExStyle:=NExStyle and not WS_EX_STATICEDGE;
  if (pToolWindow in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_TOOLWINDOW else NExStyle:=NExStyle and not WS_EX_TOOLWINDOW;
  if (pWindowEdge in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_WindowEdge else NExStyle:=NExStyle and not WS_EX_WindowEdge;
  if (pAcceptFiles in FOldControlProperties) then NExStyle:=NExStyle or WS_EX_AcceptFiles else NExStyle:=NExStyle and not WS_EX_AcceptFiles;
  SetWindowLong(CHandle, GWL_EXSTYLE,NExStyle);
  SetWindowLong(CHandle, GWL_STYLE,NStyle);
  SetWindowPos(CHandle, 0, 0,0,0,0,SWP_DRAWFRAME or SWP_FRAMECHANGED or SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
  SendMessage(CHandle,WM_PAINT,GetDC(CHandle),0);
end;


procedure TAHMManagedControl.Update;
begin
  if not(csDestroying in FAHMControlManager.Componentstate) then
  SetControlProperties(FControlProperties);
end;

procedure TAHMManagedControl.SetConstraints(Sender: TObject);
begin
  if FControl=nil then exit;
  Postmessage(FControl.Handle,WM_SIZE,0,0);
end;

procedure TAHMManagedControl.SetControl(const Value: TWinControl);
begin
  if (FControl<>nil) then HookList.Remove(FControl);
  FControl := Value;
  if FControl<>nil then
  begin
    FOldControlProperties:=GetControlProperties;
    if (csDesigning in FAHMControlManager.componentstate) and not(csLoading in FAHMControlManager.Componentstate) then
    FControlProperties:=GetControlProperties;
    FControlHandle := FControl.Handle;
    FHookClass:=TAHMHookClass(HookList.Add(FAHMControlManager,FControl,false));
    FHookClass.BeforeMessage:=Hooked;
    FHookClass.OnReloadHook:=ReloadHook;
  end;
end;

constructor TAHMManagedControl.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FAHMControlManager :=(((Collection as TAHMManagedControls).GetOwner) as TAHMControlManager);
  FControlStore:=TAHMSysControlStore.Create(Self);
  FConstraints := TAHMControlConstraints.Create;
  FConstraints.OnChange:=SetConstraints;
end;

function TAHMManagedControl.GetDisplayName: string;
begin
  if FControl<>nil then
    Result:=FControl.Name
  else
    Result:=inherited GetDisplayName;
end;

function TAHMManagedControls.GetOwner: TPersistent;
begin
  Result := FAHMControlManager;
end;

procedure TAHMManagedControl.Assign(Source: TPersistent);
begin
  if Source is TAHMManagedControl then
  begin
    Exit;
  end;
  inherited Assign(Source);
end;

constructor TAHMManagedControls.Create(AOwner: TAHMControlManager);
begin
  inherited Create(TAHMManagedControl);
  FAHMControlManager := AOwner;
end;

function TAHMManagedControls.Add: TAHMManagedControl;
begin
  Result := TAHMManagedControl(inherited Add);
end;

function TAHMManagedControls.GetAHMManagedControl(Index: Integer): TAHMManagedControl;
begin
  Result := TAHMManagedControl(inherited GetItem(Index));
end;

procedure TAHMManagedControls.SetAHMManagedControl(Index: Integer; Value: TAHMManagedControl);
begin
  inherited SetItem(Index, Value);
end;

constructor TAHMControlManager.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FAHMManagedControls:=TAHMManagedControls.Create(self);
end;

destructor TAHMControlManager.Destroy;
begin
  FAHMManagedControls.Free;
  inherited Destroy;
end;

procedure TAHMControlManager.SetAHMManagedControls(Value: TAHMManagedControls);
begin
  FAHMManagedControls.Assign(Value);
end;


function TAHMManagedControls.Delete(Index: Integer): Boolean;
begin
  result := true;
  Try
    Items[Index].free;
  Except
    result := false;
    Application.handleexception(self);
  End;
end;

procedure TAHMControlManager.Notification(AComponent: TComponent;
  Operation: TOperation);
var I : Integer;
begin
  if (FAHMManagedControls<>nil) then
  For I:=0 to FAHMManagedControls.Count-1 do
  begin
  if Controls[I].FControlStore<>nil then
  begin
    FAHMManagedControls[I].FControlStore.SaveToStore;
  end;
  if (AComponent=Controls[I].FControl) and (Operation=opRemove) then
  begin
    if FAHMManagedControls[I].FControl.Parent<>nil then
    FAHMManagedControls[I].SetOldControlProperties;
    FAHMManagedControls[I].FControl:=nil;
  end;
  if (AComponent=FAHMManagedControls[I].FControl) and (Operation=opInsert) then FAHMManagedControls[I].SetControl(FAHMManagedControls[I].FControl);
  end;
  if (AComponent=FStore) and (Operation=opRemove) then FStore:=nil;
  inherited Notification(AComponent, Operation);
end;

procedure TAHMControlManager.SetStore(const Value: TAHMStoreData);
begin
  FStore := Value;
  if Value <> nil then Value.FreeNotification(self);
end;

procedure TAHMManagedControl.ReloadHook(Sender: TObject);
begin
  SetControlProperties(FControlProperties);
end;


end.
