2007-08-10 12:48:00 +00:00
|
|
|
{
|
|
|
|
:: X2UtPersistForm provides functions to read and write form settings.
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtPersistForm;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
Classes,
|
|
|
|
Forms,
|
|
|
|
Windows,
|
|
|
|
|
|
|
|
X2UtPersistIntf;
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TX2FormPosSettings = class(TPersistent)
|
|
|
|
private
|
|
|
|
FHeight: Integer;
|
|
|
|
FLeft: Integer;
|
|
|
|
FMaximized: Boolean;
|
|
|
|
FTop: Integer;
|
|
|
|
FWidth: Integer;
|
2010-04-13 10:04:05 +00:00
|
|
|
FBoundsSet: Boolean;
|
|
|
|
|
|
|
|
function GetBounds: TRect;
|
2007-08-10 12:48:00 +00:00
|
|
|
procedure SetBounds(const Value: TRect);
|
2010-04-13 10:04:05 +00:00
|
|
|
procedure SetHeight(const Value: Integer);
|
|
|
|
procedure SetLeft(const Value: Integer);
|
|
|
|
procedure SetMaximized(const Value: Boolean);
|
|
|
|
procedure SetTop(const Value: Integer);
|
|
|
|
procedure SetWidth(const Value: Integer);
|
2007-08-10 12:48:00 +00:00
|
|
|
protected
|
|
|
|
procedure AssignTo(Dest: TPersistent); override;
|
|
|
|
public
|
|
|
|
procedure Assign(Source: TPersistent); override;
|
|
|
|
|
|
|
|
property Bounds: TRect read GetBounds write SetBounds;
|
|
|
|
published
|
2010-04-13 10:04:05 +00:00
|
|
|
property Maximized: Boolean read FMaximized write SetMaximized;
|
|
|
|
property Left: Integer read FLeft write SetLeft;
|
|
|
|
property Top: Integer read FTop write SetTop;
|
|
|
|
property Width: Integer read FWidth write SetWidth;
|
|
|
|
property Height: Integer read FHeight write SetHeight;
|
2007-08-10 12:48:00 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure ReadFormPos(const AReader: IX2PersistReader; const AForm: TCustomForm);
|
|
|
|
procedure WriteFormPos(const AWriter: IX2PersistWriter; const AForm: TCustomForm);
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
MultiMon,
|
|
|
|
SysUtils,
|
|
|
|
Types,
|
|
|
|
|
|
|
|
X2UtMisc;
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TProtectedCustomForm = class(TCustomForm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure ReadFormPos(const AReader: IX2PersistReader; const AForm: TCustomForm);
|
|
|
|
var
|
|
|
|
formPos: TX2FormPosSettings;
|
|
|
|
|
|
|
|
begin
|
2010-04-13 10:04:05 +00:00
|
|
|
formPos := TX2FormPosSettings.Create;
|
2007-08-10 12:48:00 +00:00
|
|
|
try
|
|
|
|
formPos.Assign(AForm);
|
|
|
|
AReader.Read(formPos);
|
|
|
|
AForm.Assign(formPos);
|
|
|
|
finally
|
|
|
|
FreeAndNil(formPos);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure WriteFormPos(const AWriter: IX2PersistWriter; const AForm: TCustomForm);
|
|
|
|
var
|
|
|
|
formPos: TX2FormPosSettings;
|
|
|
|
|
|
|
|
begin
|
2010-04-13 10:04:05 +00:00
|
|
|
formPos := TX2FormPosSettings.Create;
|
2007-08-10 12:48:00 +00:00
|
|
|
try
|
|
|
|
formPos.Assign(AForm);
|
|
|
|
AWriter.Write(formPos);
|
|
|
|
finally
|
|
|
|
FreeAndNil(formPos);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2FormPosSettings }
|
|
|
|
procedure TX2FormPosSettings.Assign(Source: TPersistent);
|
|
|
|
var
|
|
|
|
sourceForm: TProtectedCustomForm;
|
|
|
|
placement: TWindowPlacement;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if Source is TCustomForm then
|
|
|
|
begin
|
|
|
|
sourceForm := TProtectedCustomForm(Source);
|
2010-04-13 10:04:05 +00:00
|
|
|
Maximized := (sourceForm.WindowState = wsMaximized);
|
2007-08-10 12:48:00 +00:00
|
|
|
|
|
|
|
FillChar(placement, SizeOf(TWindowPlacement), #0);
|
|
|
|
placement.length := SizeOf(TWindowPlacement);
|
|
|
|
|
|
|
|
{ Get the form's normal position independant of the maximized state }
|
|
|
|
if GetWindowPlacement(sourceForm.Handle, @placement) then
|
|
|
|
SetBounds(placement.rcNormalPosition);
|
|
|
|
end else
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.AssignTo(Dest: TPersistent);
|
|
|
|
var
|
|
|
|
destForm: TProtectedCustomForm;
|
|
|
|
boundsRect: TRect;
|
|
|
|
|
|
|
|
begin
|
2010-04-13 10:04:05 +00:00
|
|
|
if not FBoundsSet then
|
|
|
|
Exit;
|
|
|
|
|
2007-08-10 12:48:00 +00:00
|
|
|
if Dest is TCustomForm then
|
|
|
|
begin
|
|
|
|
destForm := TProtectedCustomForm(Dest);
|
|
|
|
boundsRect := Self.Bounds;
|
|
|
|
|
|
|
|
{ Make sure the window is at least partially visible }
|
|
|
|
if MonitorFromRect(@boundsRect, MONITOR_DEFAULTTONULL) <> 0 then
|
|
|
|
begin
|
|
|
|
if FMaximized then
|
|
|
|
begin
|
|
|
|
destForm.WindowState := wsMaximized;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
destForm.WindowState := wsNormal;
|
|
|
|
destForm.Position := poDesigned;
|
|
|
|
destForm.BoundsRect := boundsRect;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end else
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2010-04-13 10:04:05 +00:00
|
|
|
function TX2FormPosSettings.GetBounds: TRect;
|
2007-08-10 12:48:00 +00:00
|
|
|
begin
|
|
|
|
Result := Rect(FLeft, FTop, FLeft + FWidth, FTop + FHeight);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetBounds(const Value: TRect);
|
|
|
|
begin
|
2010-04-13 10:04:05 +00:00
|
|
|
Left := Value.Left;
|
|
|
|
Top := Value.Top;
|
|
|
|
Width := RectWidth(Value);
|
|
|
|
Height := RectHeight(Value);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetHeight(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FHeight := Value;
|
|
|
|
FBoundsSet := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetLeft(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FLeft := Value;
|
|
|
|
FBoundsSet := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetMaximized(const Value: Boolean);
|
|
|
|
begin
|
|
|
|
FMaximized := Value;
|
|
|
|
FBoundsSet := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetTop(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FTop := Value;
|
|
|
|
FBoundsSet := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2FormPosSettings.SetWidth(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FWidth := Value;
|
|
|
|
FBoundsSet := True;
|
2007-08-10 12:48:00 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|