Ignore:
Timestamp:
Oct 27, 2016, 3:00:47 PM (8 years ago)
Author:
chronos
Message:
  • Added: Remember position and size of main form after close of application.
  • Modified: Updated Common package to latest version.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UScaleDPI.pas

    r72 r73  
    88
    99uses
    10   Classes, Forms, Graphics, Controls, ComCtrls, LCLType;
     10  Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils, StdCtrls,
     11  Contnrs;
    1112
    1213type
     14
     15  { TControlDimension }
     16
     17  TControlDimension = class
     18    BoundsRect: TRect;
     19    FontHeight: Integer;
     20    Controls: TObjectList; // TList<TControlDimension>
     21    // Class specifics
     22    ButtonSize: TPoint; // TToolBar
     23    CoolBandWidth: Integer;
     24    ConstraintsMin: TPoint; // TForm
     25    ConstraintsMax: TPoint; // TForm
     26    constructor Create;
     27    destructor Destroy; override;
     28  end;
    1329
    1430  { TScaleDPI }
     
    1733  private
    1834    FAutoDetect: Boolean;
     35    FDesignDPI: TPoint;
     36    FDPI: TPoint;
    1937    procedure SetAutoDetect(AValue: Boolean);
     38    procedure SetDesignDPI(AValue: TPoint);
     39    procedure SetDPI(AValue: TPoint);
    2040  public
    21     DPI: TPoint;
    22     DesignDPI: TPoint;
     41    procedure StoreDimensions(Control: TControl; Dimensions: TControlDimension);
     42    procedure RestoreDimensions(Control: TControl; Dimensions: TControlDimension);
     43    procedure ScaleDimensions(Control: TControl; Dimensions: TControlDimension);
    2344    procedure ApplyToAll(FromDPI: TPoint);
    24     procedure ScaleDPI(Control: TControl; FromDPI: TPoint);
     45    procedure ScaleControl(Control: TControl; FromDPI: TPoint);
    2546    procedure ScaleImageList(ImgList: TImageList; FromDPI: TPoint);
    26     function ScaleXY(Size: TPoint; FromDPI: Integer): TPoint;
     47    function ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint;
     48    function ScaleRect(ARect: TRect; FromDPI: TPoint): TRect;
    2749    function ScaleX(Size: Integer; FromDPI: Integer): Integer;
    2850    function ScaleY(Size: Integer; FromDPI: Integer): Integer;
    2951    constructor Create(AOwner: TComponent); override;
     52    property DesignDPI: TPoint read FDesignDPI write SetDesignDPI;
     53    property DPI: TPoint read FDPI write SetDPI;
    3054  published
    3155    property AutoDetect: Boolean read FAutoDetect write SetAutoDetect;
     
    3458procedure Register;
    3559
     60
    3661implementation
    3762
     63resourcestring
     64  SWrongDPI = 'Wrong DPI [%d,%d]';
     65
    3866procedure Register;
    3967begin
    4068  RegisterComponents('Common', [TScaleDPI]);
     69end;
     70
     71{ TControlDimension }
     72
     73constructor TControlDimension.Create;
     74begin
     75  Controls := TObjectList.Create;
     76end;
     77
     78destructor TControlDimension.Destroy;
     79begin
     80  FreeAndNil(Controls);
     81  inherited Destroy;
    4182end;
    4283
     
    5091end;
    5192
     93procedure TScaleDPI.SetDesignDPI(AValue: TPoint);
     94begin
     95  if (FDesignDPI.X = AValue.X) and (FDesignDPI.Y = AValue.Y) then Exit;
     96  if (AValue.X <= 0) or (AValue.Y <= 0) then
     97    raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y]));
     98  FDesignDPI := AValue;
     99end;
     100
     101procedure TScaleDPI.SetDPI(AValue: TPoint);
     102begin
     103  if (FDPI.X = AValue.X) and (FDPI.Y = AValue.Y) then Exit;
     104  if (AValue.X <= 0) or (AValue.Y <= 0) then
     105    raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y]));
     106  FDPI := AValue;
     107end;
     108
     109procedure TScaleDPI.StoreDimensions(Control: TControl;
     110  Dimensions: TControlDimension);
     111var
     112  NewControl: TControlDimension;
     113  I: Integer;
     114begin
     115  Dimensions.BoundsRect := Control.BoundsRect;
     116  Dimensions.FontHeight := Control.Font.GetTextHeight('Hg');
     117  Dimensions.Controls.Clear;
     118  if Control is TToolBar then
     119    Dimensions.ButtonSize := Point(TToolBar(Control).ButtonWidth, TToolBar(Control).ButtonHeight);
     120  if Control is TForm then begin
     121    Dimensions.ConstraintsMin := Point(TForm(Control).Constraints.MinWidth,
     122      TForm(Control).Constraints.MinHeight);
     123    Dimensions.ConstraintsMax := Point(TForm(Control).Constraints.MaxWidth,
     124      TForm(Control).Constraints.MaxHeight);
     125  end;
     126  if Control is TWinControl then
     127  for I := 0 to TWinControl(Control).ControlCount - 1 do begin
     128    if TWinControl(Control).Controls[I] is TControl then
     129    // Do not scale docked forms twice
     130    if not (TWinControl(Control).Controls[I] is TForm) then begin
     131      NewControl := TControlDimension.Create;
     132      Dimensions.Controls.Add(NewControl);
     133      StoreDimensions(TWinControl(Control).Controls[I], NewControl);
     134    end;
     135  end;
     136end;
     137
     138procedure TScaleDPI.RestoreDimensions(Control: TControl;
     139  Dimensions: TControlDimension);
     140var
     141  I: Integer;
     142begin
     143  Control.BoundsRect := Dimensions.BoundsRect;
     144  Control.Font.Height := Dimensions.FontHeight;
     145  if Control is TToolBar then begin
     146    TToolBar(Control).ButtonWidth := Dimensions.ButtonSize.X;
     147    TToolBar(Control).ButtonHeight := Dimensions.ButtonSize.Y;
     148  end;
     149  if Control is TForm then begin
     150    TForm(Control).Constraints.MinWidth := Dimensions.ConstraintsMin.X;
     151    TForm(Control).Constraints.MinHeight := Dimensions.ConstraintsMin.Y;
     152    TForm(Control).Constraints.MaxWidth := Dimensions.ConstraintsMax.X;
     153    TForm(Control).Constraints.MaxHeight := Dimensions.ConstraintsMax.Y;
     154  end;
     155  if Control is TWinControl then
     156  for I := 0 to TWinControl(Control).ControlCount - 1 do begin
     157    if TWinControl(Control).Controls[I] is TControl then
     158    // Do not scale docked forms twice
     159    if not (TWinControl(Control).Controls[I] is TForm) then begin
     160      RestoreDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I]));
     161    end;
     162  end;
     163end;
     164
     165procedure TScaleDPI.ScaleDimensions(Control: TControl;
     166  Dimensions: TControlDimension);
     167var
     168  I: Integer;
     169begin
     170  Control.BoundsRect := ScaleRect(Dimensions.BoundsRect, DesignDPI);
     171  Control.Font.Height := ScaleY(Dimensions.FontHeight, DesignDPI.Y);
     172  if Control is TToolBar then begin
     173    TToolBar(Control).ButtonWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X);
     174    TToolBar(Control).ButtonHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y);
     175  end;
     176  if Control is TCoolBar then begin
     177    with TCoolBar(Control) do
     178    for I := 0 to Bands.Count - 1 do
     179    with TCoolBand(Bands[I]) do begin
     180      MinWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X);
     181      MinHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y);
     182      //Width := ScaleX(Dimensions.BoundsRect.Left -
     183    end;
     184  end;
     185  if Control is TForm then begin
     186    TForm(Control).Constraints.MinWidth := ScaleX(Dimensions.ConstraintsMin.X, DesignDPI.X);
     187    TForm(Control).Constraints.MaxWidth := ScaleX(Dimensions.ConstraintsMax.X, DesignDPI.X);
     188    TForm(Control).Constraints.MinHeight := ScaleY(Dimensions.ConstraintsMin.Y, DesignDPI.Y);
     189    TForm(Control).Constraints.MaxHeight := ScaleY(Dimensions.ConstraintsMax.Y, DesignDPI.Y);
     190  end;
     191  if Control is TWinControl then
     192  for I := 0 to TWinControl(Control).ControlCount - 1 do begin
     193    if TWinControl(Control).Controls[I] is TControl then
     194    // Do not scale docked forms twice
     195    if not (TWinControl(Control).Controls[I] is TForm) then begin
     196      ScaleDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I]));
     197    end;
     198  end;
     199end;
     200
    52201procedure TScaleDPI.ApplyToAll(FromDPI: TPoint);
    53202var
     
    55204begin
    56205  for I := 0 to Screen.FormCount - 1 do begin
    57     ScaleDPI(Screen.Forms[I], FromDPI);
     206    ScaleControl(Screen.Forms[I], FromDPI);
    58207  end;
    59208end;
     
    70219
    71220  SetLength(Temp, ImgList.Count);
    72   TempBmp := TBitmap.Create;
    73221  for I := 0 to ImgList.Count - 1 do
    74222  begin
     223    TempBmp := TBitmap.Create;
     224    TempBmp.PixelFormat := pf32bit;
    75225    ImgList.GetBitmap(I, TempBmp);
    76     //TempBmp.PixelFormat := pfDevice;
    77226    Temp[I] := TBitmap.Create;
    78227    Temp[I].SetSize(NewWidth, NewHeight);
     228    Temp[I].PixelFormat := pf32bit;
    79229    Temp[I].TransparentColor := TempBmp.TransparentColor;
    80230    //Temp[I].TransparentMode := TempBmp.TransparentMode;
     
    86236    if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue;
    87237    Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp);
    88   end;
    89   TempBmp.Free;
     238    TempBmp.Free;
     239  end;
    90240
    91241  ImgList.Clear;
     
    110260end;
    111261
    112 function TScaleDPI.ScaleXY(Size: TPoint; FromDPI: Integer): TPoint;
    113 begin
    114   Result.X := ScaleX(Size.X, FromDPI);
    115   Result.Y := ScaleY(Size.Y, FromDPI);
     262function TScaleDPI.ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint;
     263begin
     264  Result.X := ScaleX(APoint.X, FromDPI.X);
     265  Result.Y := ScaleY(APoint.Y, FromDPI.Y);
     266end;
     267
     268function TScaleDPI.ScaleRect(ARect: TRect; FromDPI: TPoint): TRect;
     269begin
     270  Result.TopLeft := ScalePoint(ARect.TopLeft, FromDPI);
     271  Result.BottomRight := ScalePoint(ARect.BottomRight, FromDPI);
    116272end;
    117273
     
    123279end;
    124280
    125 procedure TScaleDPI.ScaleDPI(Control: TControl; FromDPI: TPoint);
     281procedure TScaleDPI.ScaleControl(Control: TControl; FromDPI: TPoint);
    126282var
    127283  I: Integer;
    128284  WinControl: TWinControl;
    129285  ToolBarControl: TToolBar;
    130 begin
     286  OldAnchors: TAnchors;
     287  OldAutoSize: Boolean;
     288begin
     289  //if Control is TMemo then Exit;
     290  //if Control is TForm then
     291  //  Control.DisableAutoSizing;
    131292  with Control do begin
     293    //OldAutoSize := AutoSize;
     294    //AutoSize := False;
     295    //Anchors := [];
    132296    Left := ScaleX(Left, FromDPI.X);
    133297    Top := ScaleY(Top, FromDPI.Y);
     298    //if not (akRight in Anchors) then
    134299    Width := ScaleX(Width, FromDPI.X);
     300    //if not (akBottom in Anchors) then
    135301    Height := ScaleY(Height, FromDPI.Y);
    136302    {$IFDEF LCL Qt}
     
    139305      Font.Height := ScaleY(Font.GetTextHeight('Hg'), FromDPI.Y);
    140306    {$ENDIF}
     307    //Anchors := OldAnchors;
     308    //AutoSize := OldAutoSize;
    141309  end;
    142310
     
    149317  end;
    150318
     319  //if not (Control is TCustomPage) then
    151320  if Control is TWinControl then begin
    152321    WinControl := TWinControl(Control);
     
    154323      for I := 0 to WinControl.ControlCount - 1 do begin
    155324        if WinControl.Controls[I] is TControl then begin
    156           ScaleDPI(WinControl.Controls[I], FromDPI);
     325          ScaleControl(WinControl.Controls[I], FromDPI);
    157326        end;
    158327      end;
    159328    end;
    160329  end;
     330  //if Control is TForm then
     331  //  Control.EnableAutoSizing;
    161332end;
    162333
Note: See TracChangeset for help on using the changeset viewer.