Ignore:
Timestamp:
May 26, 2019, 8:29:38 PM (5 years ago)
Author:
chronos
Message:
  • Added: Update DpiForm position according real form position.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • DpiControls/UDpiControls.pas

    r533 r534  
    4646  TDpiControl = class(TComponent)
    4747  private
    48     FCaption: string;
    4948    FFont: TDpiFont;
    5049    FHeight: Integer;
    5150    FLeft: Integer;
     51    FOnChangeBounds: TNotifyEvent;
     52    FOnResize: TNotifyEvent;
    5253    FTop: Integer;
    5354    FVisible: Boolean;
     
    5556    FParent: TDpiWinControl;
    5657    procedure SetFont(AValue: TDpiFont);
     58    procedure SetOnChangeBounds(AValue: TNotifyEvent);
     59    procedure SetOnResize(AValue: TNotifyEvent);
     60    procedure VclFormResize(Sender: TObject);
     61    procedure VclChangeBounds(Sender: TObject);
     62    procedure DoFormResize;
     63    procedure DoChangeBounds;
    5764  protected
    5865    procedure UpdateBounds; virtual;
    5966    procedure FontChanged(Sender: TObject); virtual;
     67    function GetCaption: string; virtual;
    6068    procedure SetParent(AValue: TDpiWinControl); virtual;
    6169    procedure SetCaption(AValue: string); virtual;
     
    6674    procedure SetWidth(AValue: Integer); virtual;
    6775    function GetVclControl: TControl; virtual;
     76    procedure UpdateVclControl; virtual;
    6877  public
    6978    procedure ScreenChanged; virtual;
     
    7180    procedure Show;
    7281    procedure Hide;
    73     function Scale(Value: Integer): Integer;
    7482    constructor Create(TheOwner: TComponent); override;
    7583    destructor Destroy; override;
     
    8189    property Height: Integer read FHeight write SetHeight;
    8290    property Visible: Boolean read FVisible write SetVisible;
    83     property Caption: string read FCaption write SetCaption;
     91    property Caption: string read GetCaption write SetCaption;
    8492    property Font: TDpiFont read FFont write SetFont;
     93    property OnResize: TNotifyEvent read FOnResize write SetOnResize;
     94    property OnChangeBounds: TNotifyEvent read FOnChangeBounds write SetOnChangeBounds;
    8595  end;
    8696
     
    105115  TDpiForm = class(TDpiWinControl)
    106116  private
    107     FOnShow: TNotifyEvent;
     117    function GetOnCreate: TNotifyEvent;
     118    function GetOnDestroy: TNotifyEvent;
     119    function GetOnHide: TNotifyEvent;
     120    function GetOnShow: TNotifyEvent;
     121    procedure SetOnCreate(AValue: TNotifyEvent);
     122    procedure SetOnDestroy(AValue: TNotifyEvent);
     123    procedure SetOnHide(AValue: TNotifyEvent);
    108124    procedure SetOnShow(AValue: TNotifyEvent);
     125    procedure DoOnCreate;
    109126  protected
    110127    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
     
    116133    destructor Destroy; override;
    117134  published
    118     property OnShow: TNotifyEvent read FOnShow write SetOnShow;
     135    property OnShow: TNotifyEvent read GetOnShow write SetOnShow;
     136    property OnHide: TNotifyEvent read GetOnHide write SetOnHide;
     137    property OnCreate: TNotifyEvent read GetOnCreate write SetOnCreate;
     138    property OnDestroy: TNotifyEvent read GetOnDestroy write SetOnDestroy;
    119139  end;
    120140
     
    158178  private
    159179    FDpi: Integer;
     180    function GetHeight: Integer;
     181    function GetWidth: Integer;
    160182    procedure SetDpi(AValue: Integer);
    161183    procedure UpdateForms;
     
    166188  published
    167189    property Dpi: Integer read FDpi write SetDpi;
     190    property Width: Integer read GetWidth;
     191    property Height: Integer read GetHeight;
    168192  end;
    169193
     
    189213end;
    190214
     215function ScaleToVcl(Value: Integer): Integer;
     216begin
     217  Result := Round(Value * DpiScreen.Dpi / 96);
     218end;
     219
     220function ScaleFromVcl(Value: Integer): Integer;
     221begin
     222  Result := Round(Value * 96 / DpiScreen.Dpi);
     223end;
     224
     225
    191226{ TDpiImage }
    192227
     
    290325end;
    291326
     327function TDpiScreen.GetWidth: Integer;
     328begin
     329  Result := ScaleFromVcl(Screen.Width);
     330end;
     331
     332function TDpiScreen.GetHeight: Integer;
     333begin
     334  Result := ScaleFromVcl(Screen.Height);
     335end;
     336
    292337procedure TDpiScreen.UpdateForms;
    293338var
     
    354399  if FVisible = AValue then Exit;
    355400  FVisible := AValue;
    356   GetVclControl.Visible := AValue;;
     401  GetVclControl.Visible := AValue;
    357402end;
    358403
     
    367412begin
    368413  Result := nil;
     414end;
     415
     416procedure TDpiControl.UpdateVclControl;
     417begin
     418  GetVclControl.OnResize := @VclFormResize;
     419  GetVclControl.OnChangeBounds := @VclChangeBounds;
    369420end;
    370421
     
    394445end;
    395446
    396 function TDpiControl.Scale(Value: Integer): Integer;
    397 begin
    398   Scale := Round(Value * DpiScreen.Dpi / 96);
    399 end;
    400 
    401447constructor TDpiControl.Create(TheOwner: TComponent);
    402448begin
     
    421467procedure TDpiControl.SetCaption(AValue: string);
    422468begin
    423   if FCaption = AValue then Exit;
    424   FCaption := AValue;
    425469  GetVclControl.Caption := AValue;
    426470end;
     
    448492end;
    449493
     494procedure TDpiControl.SetOnChangeBounds(AValue: TNotifyEvent);
     495begin
     496  if FOnChangeBounds = AValue then Exit;
     497  FOnChangeBounds := AValue;
     498end;
     499
     500procedure TDpiControl.SetOnResize(AValue: TNotifyEvent);
     501begin
     502  if FOnResize = AValue then Exit;
     503  FOnResize := AValue;
     504end;
     505
     506procedure TDpiControl.VclFormResize(Sender: TObject);
     507begin
     508  SetBounds(ScaleFromVcl(GetVclControl.Left), ScaleFromVcl(GetVclControl.Top),
     509    ScaleFromVcl(GetVclControl.Width), ScaleFromVcl(GetVclControl.Height));
     510  DoFormResize;
     511end;
     512
     513procedure TDpiControl.VclChangeBounds(Sender: TObject);
     514begin
     515  SetBounds(ScaleFromVcl(GetVclControl.Left), ScaleFromVcl(GetVclControl.Top),
     516    ScaleFromVcl(GetVclControl.Width), ScaleFromVcl(GetVclControl.Height));
     517  DoChangeBounds;
     518end;
     519
     520procedure TDpiControl.DoFormResize;
     521begin
     522  if Assigned(FOnResize) then FOnResize(Self);
     523end;
     524
     525procedure TDpiControl.DoChangeBounds;
     526begin
     527  if Assigned(FOnChangeBounds) then FOnChangeBounds(Self);
     528end;
     529
     530function TDpiControl.GetCaption: string;
     531begin
     532  Result := GetVclControl.Caption;
     533end;
     534
    450535procedure TDpiControl.FontChanged(Sender: TObject);
    451536begin
    452   GetVclControl.Font.Size := Scale(Font.Size);
     537  GetVclControl.Font.Size := ScaleToVcl(Font.Size);
    453538end;
    454539
    455540procedure TDpiControl.UpdateBounds;
    456541begin
    457   GetVclControl.SetBounds(Scale(Left), Scale(Top), Scale(Width), Scale(Height));
     542  GetVclControl.SetBounds(ScaleToVcl(Left), ScaleToVcl(Top), ScaleToVcl(Width), ScaleToVcl(Height));
    458543end;
    459544
     
    492577
    493578{ TDpiForm }
     579
     580function TDpiForm.GetOnCreate: TNotifyEvent;
     581begin
     582  Result := VclForm.OnCreate;
     583end;
     584
     585function TDpiForm.GetOnDestroy: TNotifyEvent;
     586begin
     587  Result := VclForm.OnDestroy;
     588end;
     589
     590function TDpiForm.GetOnHide: TNotifyEvent;
     591begin
     592  Result := VclForm.OnHide;
     593end;
     594
     595function TDpiForm.GetOnShow: TNotifyEvent;
     596begin
     597  Result := VclForm.OnShow;
     598end;
     599
     600procedure TDpiForm.SetOnCreate(AValue: TNotifyEvent);
     601begin
     602  VclForm.OnCreate := AValue;
     603end;
     604
     605procedure TDpiForm.SetOnDestroy(AValue: TNotifyEvent);
     606begin
     607  VclForm.OnDestroy := AValue;
     608end;
     609
     610procedure TDpiForm.SetOnHide(AValue: TNotifyEvent);
     611begin
     612  VclForm.OnHide := AValue;
     613end;
     614
     615procedure TDpiForm.SetOnShow(AValue: TNotifyEvent);
     616begin
     617  VclForm.OnShow := AValue;
     618end;
     619
     620procedure TDpiForm.DoOnCreate;
     621begin
     622  if Assigned(VclForm.OnCreate) then
     623    VclForm.OnCreate(Self);
     624end;
    494625
    495626// This method is called by TWriter to retrieve the child components to write
     
    517648begin
    518649  Result := VclForm;
    519 end;
    520 
    521 procedure TDpiForm.SetOnShow(AValue: TNotifyEvent);
    522 begin
    523   if FOnShow = AValue then Exit;
    524   FOnShow := AValue;
    525   VclForm.OnShow := AValue;
    526650end;
    527651
     
    544668    GlobalNameSpace.EndWrite;
    545669  end;
     670  UpdateVclControl;
     671  DoOnCreate;
    546672end;
    547673
Note: See TracChangeset for help on using the changeset viewer.