Changeset 534


Ignore:
Timestamp:
May 26, 2019, 8:29:38 PM (6 years ago)
Author:
chronos
Message:
  • Added: Update DpiForm position according real form position.
Location:
DpiControls
Files:
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • DpiControls/Demo/DpiComponentsDemo.lpi

    r533 r534  
    4848      </Unit1>
    4949      <Unit2>
    50         <Filename Value="UDpiForm.pas"/>
     50        <Filename Value="UDpiFormMain.pas"/>
    5151        <IsPartOfProject Value="True"/>
    52         <ComponentName Value="DpiForm1"/>
     52        <ComponentName Value="DpiFormMain"/>
    5353        <HasResources Value="True"/>
    5454      </Unit2>
  • DpiControls/Demo/DpiComponentsDemo.lpr

    r533 r534  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UFormMain, UDpiForm
     10  Forms, UFormMain, UDpiFormMain
    1111  { you can add units after this };
    1212
  • DpiControls/Demo/UDpiFormMain.lfm

    r533 r534  
    1 object DpiForm1: TDpiForm1
     1object DpiFormMain: TDpiFormMain
    22  Top = 504
    33  Left = 865
    44  Width = 0
    55  Height = 0
    6   Visible = False
    7   OnShow = DpiForm1Show
     6  Visible = True
     7  OnCreate = DpiFormMainCreate
    88  object DpiButton1: TDpiButton
    9     Top = 154
    10     Left = 20
     9    Top = 128
     10    Left = 8
    1111    Width = 150
    1212    Height = 40
     
    1414    Caption = 'Test'
    1515    OnClick = DpiButton1Click
    16     left = 88
    17     top = 88
    1816  end
    1917end
  • DpiControls/Demo/UDpiFormMain.pas

    r533 r534  
    1 unit UDpiForm;
     1unit UDpiFormMain;
    22
    33{$mode objfpc}{$H+}
     
    1010type
    1111
    12   { TDpiForm1 }
     12  { TDpiFormMain }
    1313
    14   TDpiForm1 = class(TDpiForm)
     14  TDpiFormMain = class(TDpiForm)
    1515    DpiButton1: TDpiButton;
    1616    procedure DpiButton1Click(Sender: TObject);
    17     procedure DpiForm1Show(Sender: TObject);
     17    procedure DpiFormMainCreate(Sender: TObject);
    1818  private
    1919
     
    2323
    2424var
    25   DpiForm1: TDpiForm1;
     25  DpiFormMain: TDpiFormMain;
    2626
    2727implementation
     
    2929{$R *.lfm}
    3030
    31 { TDpiForm1 }
     31{ TDpiFormMain }
    3232
    33 procedure TDpiForm1.DpiForm1Show(Sender: TObject);
     33procedure TDpiFormMain.DpiFormMainCreate(Sender: TObject);
    3434var
    3535  DpiButton: TDpiButton;
    3636  DpiImage: TDpiImage;
    3737begin
    38   DpiButton := TDpiButton.Create(DpiForm1);
    39   DpiButton.Parent := DpiForm1;
     38  DpiButton := TDpiButton.Create(DpiFormMain);
     39  DpiButton.Parent := Self;
    4040  DpiButton.SetBounds(10, 10, 100, 30);
    4141  DpiButton.Caption := 'Click me';
     
    4343  DpiButton1.Parent := Self;
    4444
    45   DpiImage := TDpiImage.Create(DpiForm1);
    46   DpiImage.Parent := DpiForm1;
     45  DpiImage := TDpiImage.Create(DpiFormMain);
     46  DpiImage.Parent := Self;
    4747  DpiImage.SetBounds(150, 10, 100, 100);
    4848  DpiImage.Visible := True;
     
    5151end;
    5252
    53 procedure TDpiForm1.DpiButton1Click(Sender: TObject);
     53procedure TDpiFormMain.DpiButton1Click(Sender: TObject);
    5454begin
    5555  ShowMessage('Hello');
  • DpiControls/Demo/UFormMain.lfm

    r533 r534  
    11object FormMain: TFormMain
    2   Left = 57
    3   Height = 126
    4   Top = 44
     2  Left = 127
     3  Height = 168
     4  Top = 66
    55  Width = 472
    66  Caption = 'DpiControls demo'
    7   ClientHeight = 126
     7  ClientHeight = 168
    88  ClientWidth = 472
    99  DesignTimePPI = 144
     
    2929    ParentColor = False
    3030  end
     31  object ButtonNewDpiForm: TButton
     32    Left = 19
     33    Height = 38
     34    Top = 117
     35    Width = 193
     36    Caption = 'New DpiForm'
     37    OnClick = ButtonNewDpiFormClick
     38    TabOrder = 1
     39  end
    3140end
  • DpiControls/Demo/UFormMain.pas

    r533 r534  
    77uses
    88  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
    9   ExtCtrls, UDpiForm, UDpiControls;
     9  ExtCtrls, UDpiControls, UDpiFormMain;
    1010
    1111type
     
    1414
    1515  TFormMain = class(TForm)
     16    ButtonNewDpiForm: TButton;
    1617    Label1: TLabel;
    1718    TrackBar1: TTrackBar;
     19    procedure ButtonNewDpiFormClick(Sender: TObject);
    1820    procedure FormShow(Sender: TObject);
    1921    procedure TrackBar1Change(Sender: TObject);
     
    3739  DpiScreen.Dpi := 96 * 2;
    3840  TrackBar1.Position := DpiScreen.Dpi;
     41  ButtonNewDpiFormClick(nil);
     42end;
    3943
    40   DpiForm1 := TDpiForm1.Create(nil);
    41   DpiForm1.Caption := DpiForm1.Name;
    42   DpiForm1.SetBounds(100, 100, 400, 200);
    43   DpiForm1.Show;
    44 
    45   DpiScreen.Forms.Add(DpiForm1);
     44procedure TFormMain.ButtonNewDpiFormClick(Sender: TObject);
     45var
     46  DpiForm: TDpiForm;
     47begin
     48  DpiForm := TDpiFormMain.Create(nil);
     49  DpiForm.Caption := DpiForm.Name;
     50  DpiForm.SetBounds(100, 100, 400, 200);
     51  DpiForm.Show;
     52  DpiScreen.Forms.Add(DpiForm);
    4653end;
    4754
  • 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.