Ignore:
Timestamp:
Sep 10, 2022, 6:54:43 PM (2 years ago)
Author:
chronos
Message:
  • Modified: CoolTranslator replaced by Common package.
  • Modified: Update common package.
File:
1 edited

Legend:

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

    r15 r25  
    33{ See: http://wiki.lazarus.freepascal.org/High_DPI }
    44
    5 {$mode delphi}{$H+}
    6 
    75interface
    86
    97uses
    10   Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils, StdCtrls,
    11   Contnrs;
     8  Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils,
     9  Generics.Collections;
    1210
    1311type
     12  TControlDimensions = class;
    1413
    1514  { TControlDimension }
     
    1817    BoundsRect: TRect;
    1918    FontHeight: Integer;
    20     Controls: TObjectList; // TList<TControlDimension>
     19    Controls: TControlDimensions;
    2120    // Class specifics
    2221    ButtonSize: TPoint; // TToolBar
     
    2625    constructor Create;
    2726    destructor Destroy; override;
     27  end;
     28
     29  TControlDimensions = class(TObjectList<TControlDimension>)
    2830  end;
    2931
     
    7375constructor TControlDimension.Create;
    7476begin
    75   Controls := TObjectList.Create;
     77  Controls := TControlDimensions.Create;
    7678end;
    7779
     
    7981begin
    8082  FreeAndNil(Controls);
    81   inherited Destroy;
     83  inherited;
    8284end;
    8385
     
    212214  TempBmp: TBitmap;
    213215  Temp: array of TBitmap;
    214   NewWidth, NewHeight: integer;
    215   I: Integer;
    216 begin
    217   NewWidth := ScaleX(ImgList.Width, FromDPI.X);
    218   NewHeight := ScaleY(ImgList.Height, FromDPI.Y);
    219 
    220   SetLength(Temp, ImgList.Count);
    221   for I := 0 to ImgList.Count - 1 do
    222   begin
    223     TempBmp := TBitmap.Create;
    224     TempBmp.PixelFormat := pf32bit;
    225     ImgList.GetBitmap(I, TempBmp);
    226     Temp[I] := TBitmap.Create;
    227     Temp[I].SetSize(NewWidth, NewHeight);
    228     Temp[I].PixelFormat := pf32bit;
    229     Temp[I].TransparentColor := TempBmp.TransparentColor;
    230     //Temp[I].TransparentMode := TempBmp.TransparentMode;
    231     Temp[I].Transparent := True;
    232     Temp[I].Canvas.Brush.Style := bsSolid;
    233     Temp[I].Canvas.Brush.Color := Temp[I].TransparentColor;
    234     Temp[I].Canvas.FillRect(0, 0, Temp[I].Width, Temp[I].Height);
    235 
    236     if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue;
    237     Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp);
    238     TempBmp.Free;
    239   end;
    240 
    241   ImgList.Clear;
    242   ImgList.Width := NewWidth;
    243   ImgList.Height := NewHeight;
    244 
    245   for I := 0 to High(Temp) do
    246   begin
    247     ImgList.Add(Temp[I], nil);
    248     Temp[i].Free;
     216  NewWidth: Integer;
     217  NewHeight: Integer;
     218  I: Integer;
     219begin
     220  ImgList.BeginUpdate;
     221  try
     222    NewWidth := ScaleX(ImgList.Width, FromDPI.X);
     223    NewHeight := ScaleY(ImgList.Height, FromDPI.Y);
     224
     225    Temp := nil;
     226    SetLength(Temp, ImgList.Count);
     227    for I := 0 to ImgList.Count - 1 do
     228    begin
     229      TempBmp := TBitmap.Create;
     230      try
     231        TempBmp.PixelFormat := pf32bit;
     232        ImgList.GetBitmap(I, TempBmp);
     233        Temp[I] := TBitmap.Create;
     234        Temp[I].SetSize(NewWidth, NewHeight);
     235        {$IFDEF UNIX}
     236        Temp[I].PixelFormat := pf24bit;
     237        {$ELSE}
     238        Temp[I].PixelFormat := pf32bit;
     239        {$ENDIF}
     240        Temp[I].TransparentColor := TempBmp.TransparentColor;
     241        //Temp[I].TransparentMode := TempBmp.TransparentMode;
     242        Temp[I].Transparent := True;
     243        Temp[I].Canvas.Brush.Style := bsSolid;
     244        Temp[I].Canvas.Brush.Color := Temp[I].TransparentColor;
     245        Temp[I].Canvas.FillRect(0, 0, Temp[I].Width, Temp[I].Height);
     246
     247        if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue;
     248        Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp);
     249      finally
     250        TempBmp.Free;
     251      end;
     252    end;
     253
     254    ImgList.Clear;
     255    ImgList.Width := NewWidth;
     256    ImgList.Height := NewHeight;
     257
     258    for I := 0 to High(Temp) do
     259    begin
     260      ImgList.Add(Temp[I], nil);
     261      Temp[i].Free;
     262    end;
     263  finally
     264    ImgList.EndUpdate;
    249265  end;
    250266end;
     
    284300  WinControl: TWinControl;
    285301  ToolBarControl: TToolBar;
    286   OldAnchors: TAnchors;
    287   OldAutoSize: Boolean;
    288 begin
     302  //OldAnchors: TAnchors;
     303  //OldAutoSize: Boolean;
     304begin
     305  //if not (Control is TCustomPage) then
     306  // Resize childs first
     307  if Control is TWinControl then begin
     308    WinControl := TWinControl(Control);
     309    if WinControl.ControlCount > 0 then begin
     310      for I := 0 to WinControl.ControlCount - 1 do begin
     311        if WinControl.Controls[I] is TControl then begin
     312          ScaleControl(WinControl.Controls[I], FromDPI);
     313        end;
     314      end;
     315    end;
     316  end;
     317
    289318  //if Control is TMemo then Exit;
    290319  //if Control is TForm then
     
    312341  with TCoolBar(Control) do begin
    313342    BeginUpdate;
    314     for I := 0 to Bands.Count - 1 do
    315       with Bands[I] do begin
    316         MinWidth := ScaleX(MinWidth, FromDPI.X);
    317         MinHeight := ScaleY(MinHeight, FromDPI.Y);
    318         Width := ScaleX(Width, FromDPI.X);
    319         //Control.Invalidate;
     343    try
     344      for I := 0 to Bands.Count - 1 do
     345        with Bands[I] do begin
     346          MinWidth := ScaleX(MinWidth, FromDPI.X);
     347          MinHeight := ScaleY(MinHeight, FromDPI.Y);
     348          // Workaround to bad band width auto sizing
     349          //Width := ScaleX(Width, FromDPI.X);
     350          Width := ScaleX(Control.Width + 28, FromDPI.X);
     351          //Control.Invalidate;
     352        end;
     353      // Workaround for bad autosizing of coolbar
     354      if AutoSize then begin
     355        AutoSize := False;
     356        Height := ScaleY(Height, FromDPI.Y);
     357        AutoSize := True;
    320358      end;
    321     EndUpdate;
     359    finally
     360      EndUpdate;
     361    end;
    322362  end;
    323363
     
    330370  end;
    331371
    332   //if not (Control is TCustomPage) then
    333   if Control is TWinControl then begin
    334     WinControl := TWinControl(Control);
    335     if WinControl.ControlCount > 0 then begin
    336       for I := 0 to WinControl.ControlCount - 1 do begin
    337         if WinControl.Controls[I] is TControl then begin
    338           ScaleControl(WinControl.Controls[I], FromDPI);
    339         end;
    340       end;
    341     end;
    342   end;
    343372  //if Control is TForm then
    344373  //  Control.EnableAutoSizing;
Note: See TracChangeset for help on using the changeset viewer.