Ignore:
Timestamp:
May 7, 2020, 7:05:57 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Improved code in HighDPI branch.
File:
1 edited

Legend:

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

    r179 r193  
    2828  public
    2929    property OnPaint;
     30    procedure Paint; override;
     31  end;
     32
     33  { TControlEx }
     34
     35  TControlEx = class(TControl)
     36  public
    3037    property OnMouseDown;
    3138    property OnMouseUp;
    3239    property OnMouseMove;
    33     procedure Paint; override;
    3440  end;
    3541
     
    189195    property Parent: TDpiWinControl read FParent write SetParent;
    190196    property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
     197    property Visible: Boolean read GetVisible write SetVisible;
    191198  published
    192199    property ClientHeight: Integer read GetClientHeight write SetClientHeight;
     
    197204    property Width: Integer read FWidth write SetWidth;
    198205    property Height: Integer read FHeight write SetHeight;
    199     property Visible: Boolean read GetVisible write SetVisible;
    200206    property Caption: string read GetCaption write SetCaption;
    201207    property Enabled: Boolean read GetEnabled write SetEnabled;
     
    221227  TDpiGraphic = class(TPersistent)
    222228  protected
     229    FDpi: Integer;
    223230    function GetVclGraphic: TGraphic; virtual;
     231    function GetWidth: Integer; virtual; abstract;
     232    function GetHeight: Integer; virtual; abstract;
     233    procedure SetWidth(Value: Integer); virtual; abstract;
     234    procedure SetHeight(Value: Integer); virtual; abstract;
     235    procedure ScreenChanged; virtual;
     236    procedure SetDpi(AValue: Integer); virtual;
     237    function GetDpi: Integer; virtual;
    224238  public
    225     procedure LoadFromFile(const Filename: string);
     239    constructor Create;
     240    procedure LoadFromFile(const Filename: string); virtual;
     241    property Width: Integer read GetWidth write SetWidth;
     242    property Height: Integer read GetHeight write SetHeight;
     243    property Dpi: Integer read GetDpi write SetDpi;
    226244  end;
    227245
     
    260278    procedure StretchDraw(const DestRect: TRect; SrcGraphic: TDpiGraphic); virtual;
    261279    procedure FrameRect(Rect: TRect);
    262     procedure Rectangle(X1, Y1, X2, Y2: Integer);
     280    procedure Rectangle(X1, Y1, X2, Y2: Integer); overload;
     281    procedure Rectangle(const ARect: TRect); overload;
    263282    function TextWidth(Text: string): Integer;
    264283    function TextHeight(Text: string): Integer;
     
    291310    VclGraphicControl: TGraphicControl;
    292311    FCanvas: TDpiCanvas;
     312    function GetOnPaint: TNotifyEvent;
    293313    procedure SetCanvas(AValue: TDpiCanvas);
    294314    procedure PaintHandler(Sender: TObject);
     315    procedure SetOnPaint(AValue: TNotifyEvent);
    295316  protected
    296317    procedure Paint; virtual;
     
    298319    function GetVclGraphicControl: TGraphicControl; virtual;
    299320    procedure UpdateVclControl; override;
    300     property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
     321    procedure UpdateVclControlPrivate; virtual;
     322    property OnPaint: TNotifyEvent read GetOnPaint write SetOnPaint;
    301323  public
    302324    constructor Create(TheOwner: TComponent); override;
     
    481503    destructor Destroy; override;
    482504  published
     505    property Visible;
    483506  end;
    484507
     
    523546    property ExtendedSelect: Boolean read GetExtendedSelect write SetExtendedSelect default true;
    524547    property BorderStyle: TBorderStyle read GetBorderStyle write SetBorderStyle default bsNone;
     548    property Visible;
    525549  end;
    526550
     
    556580    property Kind: TScrollBarKind read GetKind write SetKind;
    557581    property OnChange: TNotifyEvent read GetOnChange write SetOnChange;
     582    property Visible;
    558583  end;
    559584
     
    563588  private
    564589    FCanvas: TDpiCanvas;
     590    FWidth: Integer;
     591    FHeight: Integer;
    565592    function GetCanvas: TDpiCanvas;
    566     function GetHeight: Integer;
    567593    function GetPixelFormat: TPixelFormat;
    568594    function GetScanLine(Row: Integer): Pointer;
    569     function GetWidth: Integer;
    570     procedure SetHeight(AValue: Integer);
    571595    procedure SetPixelFormat(AValue: TPixelFormat);
    572     procedure SetWidth(AValue: Integer);
    573596  protected
     597    function GetHeight: Integer; override;
     598    function GetWidth: Integer; override;
    574599    function GetVclBitmap: TCustomBitmap; virtual;
    575600    function GetVclRasterImage: TRasterImage; override;
     601    procedure SetHeight(AValue: Integer); override;
     602    procedure SetWidth(AValue: Integer); override;
     603    procedure ScreenChanged; override;
    576604  public
    577605    VclBitmap: TBitmap;
    578606    procedure BeginUpdate;
    579607    procedure EndUpdate;
    580     procedure SetSize(Width, Height: Integer);
     608    procedure SetSize(AWidth, AHeight: Integer);
    581609    constructor Create;
    582610    destructor Destroy; override;
     
    617645    property Stretch: Boolean read FStretch write SetStretch;
    618646    property Picture: TDpiPicture read FDpiPicture write SetPicture;
     647    property Visible;
    619648  end;
    620649
     
    623652  TDpiPaintBox = class(TDpiGraphicControl)
    624653  private
    625     function GetOnPaint: TNotifyEvent;
    626     procedure SetOnPaint(AValue: TNotifyEvent);
     654    procedure UpdateVclControlPrivate; override;
    627655  public
    628656    VclPaintBox: TPaintBox;
     
    631659    destructor Destroy; override;
    632660  published
     661    property OnPaint;
     662    property Visible;
    633663  end;
    634664
     
    666696  public
    667697    VclJpeg: TJPEGImage;
     698    procedure LoadFromFile(const Filename: string); override;
    668699  end;
    669700
     
    676707  public
    677708    VclPng: TPortableNetworkGraphic;
     709    procedure LoadFromFile(const Filename: string); override;
    678710  end;
    679711
     
    709741
    710742procedure Register;
    711 function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD): Boolean;
     743function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
     744function DpiBitBltCanvas(Dest: TDpiCanvas; X, Y, Width, Height: Integer; Src: TDpiCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
    712745function DpiCreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN;
    713746function ScaleToVcl(Value: Integer): Integer;
     
    735768end;
    736769
     770function DpiBitBltCanvas(Dest: TDpiCanvas; X, Y, Width, Height: Integer;
     771  Src: TDpiCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
     772begin
     773  Result := DpiBitBlt(Dest.Handle, X, Y, Width, Height, Src.Handle, XSrc, YSrc, Rop);
     774end;
     775
    737776function DpiCreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN;
    738777begin
     
    792831
    793832function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc,
    794   YSrc: Integer; Rop: DWORD): Boolean;
     833  YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
    795834begin
    796835  Result := BitBlt(DestDC, ScaleToVcl(X), ScaleToVcl(Y), ScaleToVcl(Width),
     
    9951034end;
    9961035
     1036procedure TDpiJpegImage.LoadFromFile(const Filename: string);
     1037var
     1038  Bitmap: TJPEGImage;
     1039begin
     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;
     1048end;
     1049
    9971050{ TDpiPortableNetworkGraphic }
    9981051
     
    10061059  if not Assigned(VclPng) then VclPng := TPortableNetworkGraphic.Create;
    10071060  Result := VclPng;
     1061end;
     1062
     1063procedure TDpiPortableNetworkGraphic.LoadFromFile(const Filename: string);
     1064var
     1065  Bitmap: TPortableNetworkGraphic;
     1066begin
     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;
    10081075end;
    10091076
     
    11571224end;
    11581225
     1226procedure TDpiGraphic.ScreenChanged;
     1227begin
     1228end;
     1229
     1230procedure TDpiGraphic.SetDpi(AValue: Integer);
     1231begin
     1232  FDpi := AValue;
     1233  ScreenChanged;
     1234end;
     1235
     1236function TDpiGraphic.GetDpi: Integer;
     1237begin
     1238  Result := FDpi;
     1239end;
     1240
     1241constructor TDpiGraphic.Create;
     1242begin
     1243  Dpi := DpiScreen.Dpi;
     1244end;
     1245
    11591246procedure TDpiGraphic.LoadFromFile(const Filename: string);
    1160 begin
    1161   GetVclGraphic.LoadFromFile(FileName);
     1247var
     1248  Bitmap: TBitmap;
     1249begin
     1250  Bitmap := TBitmap.Create;
     1251  Bitmap.LoadFromFile(FileName);
     1252  Width := ScaleFromVcl(Bitmap.Width);
     1253  Height := ScaleFromVcl(Bitmap.Height);
     1254  if Self is TDpiBitmap then
     1255    TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap)
     1256    else raise Exception.Create('Unsupported class ' + Self.ClassName);
     1257  Bitmap.Free;
    11621258end;
    11631259
     
    11661262function TDpiBitmap.GetHeight: Integer;
    11671263begin
    1168   Result := ScaleFromVcl(GetVclBitmap.Height);
     1264  Result := FHeight;
    11691265end;
    11701266
     
    11901286function TDpiBitmap.GetWidth: Integer;
    11911287begin
    1192   Result := ScaleFromVcl(GetVclBitmap.Width);
     1288  Result := FWidth;
    11931289end;
    11941290
    11951291procedure TDpiBitmap.SetHeight(AValue: Integer);
    11961292begin
     1293  FHeight := AValue;
    11971294  GetVclBitmap.Height := ScaleToVcl(AValue);
    11981295end;
     
    12051302procedure TDpiBitmap.SetWidth(AValue: Integer);
    12061303begin
     1304  FWidth := AValue;
    12071305  GetVclBitmap.Width := ScaleToVcl(AValue);
     1306end;
     1307
     1308procedure TDpiBitmap.ScreenChanged;
     1309var
     1310  Bitmap: TBitmap;
     1311  NewWidth: Integer;
     1312  NewHeight: Integer;
     1313begin
     1314  NewWidth := ScaleToVcl(Width);
     1315  NewHeight := ScaleToVcl(Height);
     1316  if Assigned(VclBitmap) and ((NewWidth <> VclBitmap.Width) or (NewHeight <> VclBitmap.Height)) then begin
     1317    // Rescale bitmap to new size
     1318    Bitmap := TBitmap.Create;
     1319    Bitmap.SetSize(NewWidth, NewHeight);
     1320    Bitmap.PixelFormat := VclBitmap.PixelFormat;
     1321    Bitmap.Canvas.StretchDraw(Bounds(0, 0, NewWidth, NewHeight), VclBitmap);
     1322    VclBitmap.Free;
     1323    VclBitmap := Bitmap;
     1324    Canvas.VclCanvas := VclBitmap.Canvas;
     1325  end;
    12081326end;
    12091327
     
    12271345end;
    12281346
    1229 procedure TDpiBitmap.SetSize(Width, Height: Integer);
    1230 begin
    1231   GetVclBitmap.SetSize(ScaleToVcl(Width), ScaleToVcl(Height));
     1347procedure TDpiBitmap.SetSize(AWidth, AHeight: Integer);
     1348begin
     1349  FWidth := AWidth;
     1350  FHeight := AHeight;
     1351  GetVclBitmap.SetSize(ScaleToVcl(AWidth), ScaleToVcl(AHeight));
    12321352end;
    12331353
     
    13721492{ TDpiPaintBox }
    13731493
    1374 function TDpiPaintBox.GetOnPaint: TNotifyEvent;
    1375 begin
    1376   Result := VclPaintBox.OnPaint;
    1377 end;
    1378 
    1379 procedure TDpiPaintBox.SetOnPaint(AValue: TNotifyEvent);
    1380 begin
    1381   VclPaintBox.OnPaint := AValue;
     1494procedure TDpiPaintBox.UpdateVclControlPrivate;
     1495begin
     1496  VclPaintBox.OnPaint := @PaintHandler;
     1497  VclPaintBox.OnMouseDown := @MouseDownHandler;
     1498  VclPaintBox.OnMouseUp := @MouseUpHandler;
     1499  VclPaintBox.OnMouseMove := @MouseMoveHandler;
    13821500end;
    13831501
     
    14161534end;
    14171535
    1418 
    14191536{ TDpiCanvas }
    14201537
     
    14711588
    14721589procedure TDpiCanvas.SetPixel(X, Y: Integer; AValue: TColor);
    1473 begin
     1590var
     1591  BrushStyle: TBrushStyle;
     1592  BrushColor: TColor;
     1593begin
     1594 { BrushStyle := GetVclCanvas.Brush.Style;
     1595  BrushColor := GetVclCanvas.Brush.Color;
     1596  GetVclCanvas.Brush.Color := AValue;
     1597  GetVclCanvas.Brush.Style := bsClear;
     1598  GetVclCanvas.FillRect(ScaleToVcl(X), ScaleToVcl(Y), ScaleToVcl(X + 1) - 1, ScaleToVcl(Y + 1) - 1);
     1599  GetVclCanvas.Brush.Style := BrushStyle;
     1600  GetVclCanvas.Brush.Color := BrushColor;
     1601  }
    14741602  GetVclCanvas.Pixels[ScaleToVcl(X), ScaleToVcl(Y)] := AValue;
    14751603end;
     
    14971625end;
    14981626
     1627procedure TDpiCanvas.Rectangle(const ARect: TRect);
     1628begin
     1629  Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
     1630end;
     1631
    14991632function TDpiCanvas.TextWidth(Text: string): Integer;
    15001633begin
     
    15501683  Source: TRect);
    15511684begin
    1552   GetVclCanvas.CopyRect(Dest, SrcCanvas.VclCanvas, Source);
     1685  GetVclCanvas.CopyRect(Dest, SrcCanvas.VclCanvas, ScaleRectToVcl(Source));
    15531686end;
    15541687
     
    15911724begin
    15921725  if not Assigned(VclGraphicControl) then begin
    1593     VclGraphicControl := TGraphicControlEx.Create(nil);
    1594     (VclGraphicControl as TGraphicControlEx).OnPaint := @PaintHandler;
     1726    VclGraphicControl := TGraphicControl.Create(nil);
    15951727  end;
    15961728  Result := VclGraphicControl;
     
    16001732begin
    16011733  inherited;
    1602   (GetVclGraphicControl as TGraphicControlEx).OnMouseDown := @MouseDownHandler;
    1603   (GetVclGraphicControl as TGraphicControlEx).OnMouseUp := @MouseUpHandler;
    1604   (GetVclGraphicControl as TGraphicControlEx).OnMouseMove := @MouseMoveHandler;
     1734  UpdateVclControlPrivate;
     1735end;
     1736
     1737procedure TDpiGraphicControl.UpdateVclControlPrivate;
     1738begin
     1739  TGraphicControlEx(GetVclGraphicControl).OnPaint := @PaintHandler;
     1740  TControlEx(GetVclControl).OnMouseDown := @MouseDownHandler;
     1741  TControlEx(GetVclControl).OnMouseUp := @MouseUpHandler;
     1742  TControlEx(GetVclControl).OnMouseMove := @MouseMoveHandler;
     1743  // Some VCL component event are not accessible on TGraphicControl level.
     1744  // Delegate this responsibility up
     1745  {
     1746  GetVclGraphicControl.OnPaint := @PaintHandler;
     1747  GetVclControl.OnMouseDown := @MouseDownHandler;
     1748  GetVclControl.OnMouseUp := @MouseUpHandler;
     1749  GetVclControl.OnMouseMove := @MouseMoveHandler;
     1750  }
     1751//  raise Exception.Create('Missing inicialization of private fields for ' + ClassName);
     1752end;
     1753
     1754function TDpiGraphicControl.GetOnPaint: TNotifyEvent;
     1755begin
     1756  Result := FOnPaint;
     1757end;
     1758
     1759procedure TDpiGraphicControl.SetOnPaint(AValue: TNotifyEvent);
     1760begin
     1761  FOnPaint := AValue;
    16051762end;
    16061763
     
    19062063  Forms := TDpiForms.Create;
    19072064  Forms.FreeObjects := False;
    1908   Dpi := 144;
     2065  Dpi := 150;
    19092066end;
    19102067
     
    19142071  inherited Destroy;
    19152072end;
    1916 
    1917 { TDpiWinControl }
    1918 
    19192073
    19202074{ TDpiButton }
     
    25132667begin
    25142668  if not Assigned(VclForm) then begin
    2515     VclForm := TFormEx.Create(nil);
     2669    VclForm := TFormEx.CreateNew(nil);
    25162670    (VclForm as TFormEx).OnMessage := @FormMessageHandler;
    25172671    //VclForm := TForm.Create(nil);
Note: See TracChangeset for help on using the changeset viewer.