Ignore:
Timestamp:
May 9, 2020, 4:02:07 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Improved HighDPI branch. Imported new changes from trunk branch.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/highdpi/Packages/DpiControls/UDpiControls.pas

    r193 r210  
    131131    FParent: TDpiWinControl;
    132132    function GetAlign: TAlign;
     133    function GetAnchors: TAnchors;
    133134    function GetBoundsRect: TRect;
    134135    function GetClientHeight: Integer;
     
    141142    function GetShowHint: Boolean;
    142143    function GetVisible: Boolean;
     144    function IsAnchorsStored: Boolean;
    143145    procedure SetAlign(AValue: TAlign);
     146    procedure SetAnchors(AValue: TAnchors);
    144147    procedure SetBoundsRect(AValue: TRect);
    145148    procedure SetClientHeight(AValue: Integer);
     
    183186      X, Y: Integer); virtual;
    184187    procedure MouseMove(Shift: TShiftState; X, Y: Integer); virtual;
     188    procedure MouseLeave; virtual;
    185189  public
     190    function ScreenToClient(const APoint: TPoint): TPoint; virtual;
     191    function ClientToScreen(const APoint: TPoint): TPoint; virtual;
     192    procedure AddHandlerOnVisibleChanged(const OnVisibleChangedEvent: TNotifyEvent;
     193                                         AsFirst: boolean = false);
     194    procedure RemoveHandlerOnVisibleChanged(const OnVisibleChangedEvent: TNotifyEvent);
    186195    procedure ScreenChanged; virtual;
    187196    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); virtual;
     
    196205    property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
    197206    property Visible: Boolean read GetVisible write SetVisible;
     207    property Anchors: TAnchors read GetAnchors write SetAnchors stored IsAnchorsStored default [akLeft, akTop];
    198208  published
    199209    property ClientHeight: Integer read GetClientHeight write SetClientHeight;
     
    237247    function GetDpi: Integer; virtual;
    238248  public
    239     constructor Create;
     249    VclGraphicClass: TGraphicClass;
     250    constructor Create; virtual;
    240251    procedure LoadFromFile(const Filename: string); virtual;
     252    procedure SaveToFile(const Filename: string); virtual;
    241253    property Width: Integer read GetWidth write SetWidth;
    242254    property Height: Integer read GetHeight write SetHeight;
     
    261273  private
    262274    FFont: TDpiFont;
     275    FVclCanvas: TCanvas;
    263276    function GetBrush: TBrush;
    264277    function GetHandle: HDC;
     
    272285    procedure SetPen(AValue: TPen);
    273286    procedure SetPixel(X, Y: Integer; AValue: TColor);
     287    procedure SetVclCanvas(AValue: TCanvas);
    274288  protected
    275289    function GetVclCanvas: TCanvas; virtual;
    276290  public
    277     VclCanvas: TCanvas;
     291    property VclCanvas: TCanvas read FVclCanvas write SetVclCanvas;
    278292    procedure StretchDraw(const DestRect: TRect; SrcGraphic: TDpiGraphic); virtual;
    279293    procedure FrameRect(Rect: TRect);
     
    458472    function GetVclForm: TForm; virtual;
    459473    procedure UpdateVclControl; override;
     474    procedure AfterConstruction; override;
    460475  public
    461476    VclForm: TForm;
     
    465480    procedure BringToFront;
    466481    constructor Create(TheOwner: TComponent); override;
     482    constructor CreateNew(AOwner: TComponent; Num: Integer = 0); virtual;
    467483    destructor Destroy; override;
    468484  published
     
    547563    property BorderStyle: TBorderStyle read GetBorderStyle write SetBorderStyle default bsNone;
    548564    property Visible;
     565    property Anchors;
    549566  end;
    550567
     
    607624    procedure EndUpdate;
    608625    procedure SetSize(AWidth, AHeight: Integer);
    609     constructor Create;
     626    constructor Create; override;
    610627    destructor Destroy; override;
    611628    procedure Assign(Source: TPersistent); override;
     
    667684  TDpiScreen = class
    668685  private
     686    // TScreen
    669687    FDpi: Integer;
    670688    FActiveForm: TDpiForm;
     689    FForms: TDpiForms;
     690    procedure AddForm(AForm: TDpiForm);
    671691    function GetActiveForm: TDpiForm;
     692    function GetCursor: TCursor;
     693    function GetCursors(Index: Integer): HCURSOR;
    672694    function GetFormCount: Integer;
     695    function GetForms(Index: Integer): TDpiForm;
    673696    function GetHeight: Integer;
    674697    function GetWidth: Integer;
    675698    procedure SetActiveForm(AValue: TDpiForm);
     699    procedure SetCursor(AValue: TCursor);
     700    procedure SetCursors(Index: Integer; AValue: HCURSOR);
    676701    procedure SetDpi(AValue: Integer);
    677702    procedure UpdateForms;
    678703  public
    679     Forms: TDpiForms;
    680704    constructor Create;
    681705    destructor Destroy; override;
    682706    property FormCount: Integer read GetFormCount;
     707    property Forms[Index: Integer]: TDpiForm read GetForms;
    683708    property ActiveForm: TDpiForm read GetActiveForm write SetActiveForm;
     709    property Cursor: TCursor read GetCursor write SetCursor;
     710    property Cursors[Index: Integer]: HCURSOR read GetCursors write SetCursors;
    684711  published
    685712    property Dpi: Integer read FDpi write SetDpi;
     713    property PixelsPerInch: Integer read FDpi;
    686714    property Width: Integer read GetWidth;
    687715    property Height: Integer read GetHeight;
     
    696724  public
    697725    VclJpeg: TJPEGImage;
    698     procedure LoadFromFile(const Filename: string); override;
     726    constructor Create; override;
    699727  end;
    700728
     
    707735  public
    708736    VclPng: TPortableNetworkGraphic;
    709     procedure LoadFromFile(const Filename: string); override;
     737    constructor Create; override;
    710738  end;
    711739
     
    716744    FMainForm: TDpiForm;
    717745    FCreatingForm: TDpiForm;
     746    function GetActive: Boolean;
    718747    function GetShowMainForm: Boolean;
    719748    function GetTitle: string;
     
    725754    function GetVclApplication: TApplication; virtual;
    726755  public
     756    constructor Create(AOwner: TComponent); override;
    727757    procedure Run;
    728758    procedure Initialize;
     
    730760    procedure UpdateMainForm(AForm: TDpiForm);
    731761    procedure CreateForm(InstanceClass: TComponentClass; out Reference);
     762    function MessageBox(Text, Caption: PChar; Flags: Longint = MB_OK): Integer;
    732763    property MainForm: TDpiForm read GetMainForm write SetMainForm;
    733764    property ShowMainForm: Boolean read GetShowMainForm write SetShowMainForm default True;
    734765    property Title: string read GetTitle write SetTitle;
     766    property Active: Boolean read GetActive;
    735767  end;
    736768
     
    934966end;
    935967
     968function TDpiApplication.GetActive: Boolean;
     969begin
     970  Result := Application.Active;
     971end;
     972
    936973function TDpiApplication.GetMainForm: TDpiForm;
    937974begin
     
    952989begin
    953990  Result := Application;
     991end;
     992
     993function DpiFindApplicationComponent(const ComponentName: string): TComponent;
     994// Note: this function is used by TReader to auto rename forms to unique names.
     995begin
     996  Result := DpiApplication.FindComponent(ComponentName);
     997end;
     998
     999constructor TDpiApplication.Create(AOwner: TComponent);
     1000begin
     1001  RegisterFindGlobalComponentProc(@DpiFindApplicationComponent);
     1002  inherited;
    9541003end;
    9551004
     
    10211070end;
    10221071
     1072function TDpiApplication.MessageBox(Text, Caption: PChar; Flags: Longint
     1073  ): Integer;
     1074begin
     1075  Result := Application.MessageBox(Text, Caption, Flags);
     1076end;
     1077
    10231078{ TDpiJpegImage }
    10241079
     
    10341089end;
    10351090
    1036 procedure TDpiJpegImage.LoadFromFile(const Filename: string);
    1037 var
    1038   Bitmap: TJPEGImage;
    1039 begin
    1040   Bitmap := TJPEGImage.Create;
    1041   Bitmap.LoadFromFile(FileName);
    1042   Width := ScaleFromVcl(Bitmap.Width);
    1043   Height := ScaleFromVcl(Bitmap.Height);
    1044   if Self is TDpiBitmap then
    1045     TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap)
    1046     else raise Exception.Create('Unsupported class ' + Self.ClassName);
    1047   Bitmap.Free;
     1091constructor TDpiJpegImage.Create;
     1092begin
     1093  inherited;
     1094  VclGraphicClass := TJPEGImage;
    10481095end;
    10491096
     
    10611108end;
    10621109
    1063 procedure TDpiPortableNetworkGraphic.LoadFromFile(const Filename: string);
    1064 var
    1065   Bitmap: TPortableNetworkGraphic;
    1066 begin
    1067   Bitmap := TPortableNetworkGraphic.Create;
    1068   Bitmap.LoadFromFile(FileName);
    1069   Width := ScaleFromVcl(Bitmap.Width);
    1070   Height := ScaleFromVcl(Bitmap.Height);
    1071   if Self is TDpiBitmap then
    1072     TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap)
    1073     else raise Exception.Create('Unsupported class ' + Self.ClassName);
    1074   Bitmap.Free;
     1110constructor TDpiPortableNetworkGraphic.Create;
     1111begin
     1112  inherited;
     1113  VclGraphicClass := TPortableNetworkGraphic;
    10751114end;
    10761115
     
    12461285procedure TDpiGraphic.LoadFromFile(const Filename: string);
    12471286var
    1248   Bitmap: TBitmap;
    1249 begin
    1250   Bitmap := TBitmap.Create;
     1287  Bitmap: TGraphic;
     1288begin
     1289  Bitmap := VclGraphicClass.Create;
    12511290  Bitmap.LoadFromFile(FileName);
    1252   Width := ScaleFromVcl(Bitmap.Width);
    1253   Height := ScaleFromVcl(Bitmap.Height);
     1291  Width := Bitmap.Width;
     1292  Height := Bitmap.Height;
    12541293  if Self is TDpiBitmap then
    1255     TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap)
     1294    TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0,
     1295    TBitmap(GetVclGraphic).Width, TBitmap(GetVclGraphic).Height), Bitmap)
    12561296    else raise Exception.Create('Unsupported class ' + Self.ClassName);
     1297  Bitmap.Free;
     1298end;
     1299
     1300procedure TDpiGraphic.SaveToFile(const Filename: string);
     1301var
     1302  Bitmap: TGraphic;
     1303begin
     1304  Bitmap := VclGraphicClass.Create;
     1305  Bitmap.Width := Width;
     1306  Bitmap.Height := Height;
     1307  if Self is TDpiBitmap then begin
     1308    if Bitmap is TRasterImage then
     1309      (Bitmap as TRasterImage).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), TBitmap(GetVclGraphic))
     1310      else raise Exception.Create('Expected TRasterImage but got ' + Bitmap.ClassName);
     1311  end else raise Exception.Create('Unsupported class ' + Self.ClassName);
     1312  Bitmap.SaveToFile(FileName);
    12571313  Bitmap.Free;
    12581314end;
     
    13551411begin
    13561412  inherited;
     1413  VclGraphicClass := TBitmap;
    13571414end;
    13581415
     
    16011658  }
    16021659  GetVclCanvas.Pixels[ScaleToVcl(X), ScaleToVcl(Y)] := AValue;
     1660end;
     1661
     1662procedure TDpiCanvas.SetVclCanvas(AValue: TCanvas);
     1663begin
     1664  if FVclCanvas = AValue then Exit;
     1665  FVclCanvas := AValue;
     1666  FFont.VclFont := FVclCanvas.Font;
    16031667end;
    16041668
     
    18091873  if FSize = AValue then Exit;
    18101874  FSize := AValue;
     1875  GetVclFont.Size := AValue;
    18111876  DoChange;
    18121877end;
     
    20362101end;
    20372102
     2103procedure TDpiScreen.SetCursor(AValue: TCursor);
     2104begin
     2105  Screen.Cursor := AValue;
     2106end;
     2107
     2108procedure TDpiScreen.SetCursors(Index: Integer; AValue: HCURSOR);
     2109begin
     2110  Screen.Cursors[Index] := AValue;
     2111end;
     2112
    20382113function TDpiScreen.GetHeight: Integer;
    20392114begin
     
    20432118function TDpiScreen.GetFormCount: Integer;
    20442119begin
    2045   Result := Forms.Count;
     2120  Result := FForms.Count;
     2121end;
     2122
     2123function TDpiScreen.GetForms(Index: Integer): TDpiForm;
     2124begin
     2125  Result := FForms[Index];
     2126end;
     2127
     2128procedure TDpiScreen.AddForm(AForm: TDpiForm);
     2129begin
     2130  if AForm is TDpiForm then begin
     2131    FForms.Add(AForm);
     2132    //DpiApplication.UpdateVisible;
     2133  end;
    20462134end;
    20472135
     
    20492137begin
    20502138  Result := FActiveForm;
     2139end;
     2140
     2141function TDpiScreen.GetCursor: TCursor;
     2142begin
     2143  Result := Screen.Cursor;
     2144end;
     2145
     2146function TDpiScreen.GetCursors(Index: Integer): HCURSOR;
     2147begin
     2148  Result := Screen.Cursors[Index];
    20512149end;
    20522150
     
    20552153  I: Integer;
    20562154begin
    2057   for I := 0 to Forms.Count - 1 do
    2058     Forms[I].ScreenChanged;
     2155  for I := 0 to FForms.Count - 1 do
     2156    FForms[I].ScreenChanged;
    20592157end;
    20602158
    20612159constructor TDpiScreen.Create;
    20622160begin
    2063   Forms := TDpiForms.Create;
    2064   Forms.FreeObjects := False;
     2161  FForms := TDpiForms.Create;
     2162  FForms.FreeObjects := False;
    20652163  Dpi := 150;
    20662164end;
     
    20682166destructor TDpiScreen.Destroy;
    20692167begin
    2070   FreeAndNil(Forms);
     2168  FreeAndNil(FForms);
    20712169  inherited Destroy;
    20722170end;
     
    21582256procedure TDpiControl.MouseMove(Shift: TShiftState; X, Y: Integer);
    21592257begin
     2258end;
     2259
     2260procedure TDpiControl.MouseLeave;
     2261begin
     2262
     2263end;
     2264
     2265function TDpiControl.ScreenToClient(const APoint: TPoint): TPoint;
     2266begin
     2267  Result := ScalePointFromVcl(GetVclControl.ScreenToClient(ScalePointToVcl(APoint)));
     2268end;
     2269
     2270function TDpiControl.ClientToScreen(const APoint: TPoint): TPoint;
     2271begin
     2272  Result := ScalePointFromVcl(GetVclControl.ClientToScreen(ScalePointToVcl(APoint)));
     2273end;
     2274
     2275procedure TDpiControl.AddHandlerOnVisibleChanged(
     2276  const OnVisibleChangedEvent: TNotifyEvent; AsFirst: boolean);
     2277begin
     2278  GetVclControl.AddHandlerOnVisibleChanged(OnVisibleChangedEvent, AsFirst);
     2279end;
     2280
     2281procedure TDpiControl.RemoveHandlerOnVisibleChanged(
     2282  const OnVisibleChangedEvent: TNotifyEvent);
     2283begin
     2284  GetVclControl.RemoveHandlerOnVisibleChanged(OnVisibleChangedEvent);
    21602285end;
    21612286
     
    22722397end;
    22732398
     2399function TDpiControl.GetAnchors: TAnchors;
     2400begin
     2401  Result := GetVclControl.Anchors;
     2402end;
     2403
    22742404function TDpiControl.GetClientHeight: Integer;
    22752405begin
     
    23172447end;
    23182448
     2449function TDpiControl.IsAnchorsStored: Boolean;
     2450begin
     2451
     2452end;
     2453
    23192454procedure TDpiControl.SetAlign(AValue: TAlign);
    23202455begin
    23212456  GetVclControl.Align := AValue;
     2457end;
     2458
     2459procedure TDpiControl.SetAnchors(AValue: TAnchors);
     2460begin
     2461  GetVclControl.Anchors := AValue;
    23222462end;
    23232463
     
    26842824end;
    26852825
     2826procedure TDpiForm.AfterConstruction;
     2827begin
     2828  inherited;
     2829  DoOnCreate;
     2830end;
     2831
    26862832function TDpiForm.ShowModal: Integer;
    26872833begin
     
    27012847// Init the component with an IDE resource
    27022848constructor TDpiForm.Create(TheOwner: TComponent);
    2703 begin
    2704   inherited;
     2849var
     2850  C: TComponent;
     2851begin
     2852  //inherited;
    27052853  //DebugLn(['TDpiForm.Create ', DbgSName(TheOwner)]);
    27062854  GlobalNameSpace.BeginWrite;
    27072855  try
     2856    CreateNew(TheOwner, 1); // this calls BeginFormUpdate, which is ended in AfterConstruction
     2857    // Self
     2858    C := FindGlobalComponent('TListDlg');
    27082859    if (ClassType <> TDpiForm) and not (csDesigning in ComponentState) then begin
    27092860      if not InitResourceComponent(Self, TDataModule) then begin
     
    27162867  ScreenChanged;
    27172868  UpdateVclControl;
    2718   DoOnCreate;
     2869end;
     2870
     2871constructor TDpiForm.CreateNew(AOwner: TComponent; Num: Integer);
     2872begin
     2873  inherited Create(AOwner);
     2874  DpiScreen.AddForm(Self);
    27192875end;
    27202876
Note: See TracChangeset for help on using the changeset viewer.